import mongoose, { Schema, Document } from 'mongoose';

export interface IOrder extends Document {
    tableId?: number; // Optional as it could be a takeaway/retail order
    tableName?: string;
    items: {
        productId: number;
        name: string;
        price: number;
        qty: number;
        modifiers?: string[];
    }[];
    subtotal: number;
    tax: number;
    serviceCharge: number;
    total: number;
    paymentMethod: string; // 'CASH', 'CARD', 'ONLINE'
    status: string; // 'PAID', 'PENDING'
    createdAt: Date;
}

const OrderSchema: Schema = new Schema({
    tableId: { type: Number },
    tableName: { type: String },
    items: [
        {
            productId: { type: Number, required: true },
            name: { type: String, required: true },
            price: { type: Number, required: true },
            qty: { type: Number, required: true },
            modifiers: [{ type: String }]
        }
    ],
    subtotal: { type: Number, required: true },
    tax: { type: Number, required: true },
    serviceCharge: { type: Number, required: true },
    total: { type: Number, required: true },
    paymentMethod: { type: String, required: true },
    status: { type: String, default: 'PENDING' }
}, {
    timestamps: true
});

export default mongoose.models.Order || mongoose.model<IOrder>('Order', OrderSchema);
