import mongoose, { Schema } from "mongoose";
import { OrderItemType } from "../../../types/OrdersType/OrderItemType/OrderItemType";
import { OrdersType } from "../../../types/OrdersType/OrdersType";

const OrderItemSchema = new Schema(
    {
        OrderID:
        {
            type: String,
            required: true,
        },
        CustomerID:
        {
            type: String,
            required: true
        },
        ItemOrderID:
        {
            type: String,
            // unique: true,
            required: true
        },
        ItemID:
        {
            type: String,
            required: true
        },
        ReturnableContainer:
        {
            type: Number,
            required: true
        },
        QuantityOrdered:
        {
            type: Number,
            required: true
        },
        OrderItemStatus:
        {
            type: Number,
            required: true
        },
        OrderItemPaymentMode:
        {
            type: String,
            required: false
        },
        OrderAcceptedShopID:
        {
            type: String,
            required: false
        },
        //OrderDeliveryPersonID will indicate the ID of the DeliveryPerson assigned to the specific order
        //unique=true because 2 orders cannot have the same DeliveryPersonID
        OrderDeliveryPersonID:
        {
            type: String,
            required: false,
        },
        OrderDeliveredTimeStamp:
        {
            type: String,
            required: false
        },
        //Area of the pincode where the order is expected
        OrderAreaPincode:
        {
            type: String,
            required: true,
        },
    }
);

OrderItemSchema.statics.fetchItemsList = async function (customerID: String): Promise<OrderItemType[] | null> {
    let itemsList: OrderItemType[] | null = null;
    try {
        itemsList = await this.find({ CustomerID: customerID }).exec();
    }
    catch (e) {
        console.log("Error fetching items list from OrderItemsSchema:", e);
    }
    // console.log("returning itemsList from fetchItemsList:", itemsList);
    return itemsList;
}


OrderItemSchema.statics.fetchByOrderAreaPincode = async function (orderAreaPincode: String): Promise<OrdersType[] | null> {
    console.log("inside fetchByOrderAreaPincode method");
    console.log("orderAreaPincode:", orderAreaPincode);
    let ordersList: OrdersType[] | null = null;
    try {
        ordersList = await this.find({
            OrderAreaPincode: orderAreaPincode,
        }).exec();
    }
    catch (e) {
        console.log("Error fetching record by OrderAreaPincode:", e);
    }
    console.log("returning ordersList from fetchByOrderAreaPincode:", ordersList);
    return ordersList;
}

OrderItemSchema.statics.fetchByOrderIDAndItemOrderID = async function (orderID: String, itemOrderID: String): Promise<OrderItemType | null> {
    console.log("inside fetchByOrderID method");
    console.log("orderID:", orderID);
    let orderItem: OrderItemType | null = null;
    try {
        orderItem = await this.findOne({ OrderID: orderID, ItemOrderID: itemOrderID }).exec();
        console.log("orderItem:", orderItem);
    }
    catch (e) {
        console.log("Error fetching record using orderID:", e);
    }
    return orderItem;
}

OrderItemSchema.statics.fetchOrdersCompletedByShopID = async function (shopID: String): Promise<OrderItemType[] | null> {
    console.log("inside fetchByAcceptedShopID method");
    console.log("shopID:", shopID);
    let orderItemList: OrderItemType[] | null = null;
    try {
        orderItemList = await this.find({
            OrderAcceptedShopID: shopID,
            OrderItemStatus: 3
        }).exec()
        console.log("orderItemList:", orderItemList);
    }
    catch (e) {
        console.log("Error fetching record from OrderItemSchema:", e);
    }
    return orderItemList;
}

OrderItemSchema.statics.fetchAvailableOrders = async function (orderAreaPincode: String, shopID: String): Promise<OrderItemType[] | null> {
    console.log("Inside fetchAvailableOrders method");
    console.log("orderAreaPincode:", orderAreaPincode);
    let orderItemsList: OrderItemType[] | null = null;
    try {
        // Build the query
        const query: any = {
            OrderAreaPincode: orderAreaPincode,
            $or: [
                { OrderAcceptedShopID: null }, // Match records with OrderAcceptedShopID as null
                ...(shopID && shopID.length > 0
                    ? [{ OrderAcceptedShopID: { $in: shopID } }] // Match records with specific ShopIDs
                    : []), // If no ShopIDs are passed, only match null values
            ],
        };

        // Execute the query
        orderItemsList = await this.find(query).exec();
    } catch (error) {
        console.log("Error fetching records from OrderItemSchema:", error);
    }
    console.log("returning orderItemsList from fetchAvailableOrders:", orderItemsList);
    return orderItemsList;
}

OrderItemSchema.statics.fetchByDeliveryPersonID = async function (deliveryPersonID: String): Promise<OrderItemType[] | null> {
    console.log("Inside fetchByDeliveryPersonID method");
    console.log("deliveryPersonID:", deliveryPersonID);
    let orderItem: OrderItemType[] | null = null;
    try {
        orderItem = await this.find({
            OrderDeliveryPersonID: deliveryPersonID,
            OrderItemStatus: 2
        }).exec();
    } catch (error) {
        console.log("Error fetching records from OrderItemSchema:", error);
    }
    console.log("returning orderItem from fetchByDeliveryPersonID:", orderItem);
    return orderItem;
}

module.exports = mongoose.model("OrderItem", OrderItemSchema)