import mongoose, { Schema, Document } from 'mongoose';

export interface IInventoryTransactionDocument extends Document {
    itemId: mongoose.Types.ObjectId;
    type: 'In' | 'Out';
    quantity: number;
    unitCost?: number;
    notes?: string;
    performedBy?: mongoose.Types.ObjectId; // User ID
    timestamp: Date;
}

const inventoryTransactionSchema = new Schema<IInventoryTransactionDocument>(
    {
        itemId: { type: Schema.Types.ObjectId, ref: 'InventoryItem', required: true },
        type: { type: String, enum: ['In', 'Out'], required: true },
        quantity: { type: Number, required: true },
        unitCost: Number,
        notes: String,
        performedBy: { type: Schema.Types.ObjectId, ref: 'User' },
        timestamp: { type: Date, default: Date.now },
    },
    {
        timestamps: false
    }
);

inventoryTransactionSchema.index({ itemId: 1 });
inventoryTransactionSchema.index({ timestamp: -1 });

export const InventoryTransaction = mongoose.model<IInventoryTransactionDocument>('InventoryTransaction', inventoryTransactionSchema);
