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

export interface IPaintingScheme extends Document {
    projectId: mongoose.Types.ObjectId;
    name: string;
    type?: string | null;
    coat?: string | null;
    solidsVolume?: number | null;
    yieldTheoretical?: number | null;
    epsMin?: number | null;
    epsMax?: number | null;
    dilution?: number | null;
    manufacturer?: string | null;
    color?: string | null;
    notes?: string | null;
    organizationId?: string;
    // Consumption Planning
    paintConsumption?: number | null;
    thinnerConsumption?: number | null;
    paintId?: mongoose.Types.ObjectId | null; // Ref to TechnicalDataSheet
    thinnerId?: mongoose.Types.ObjectId | null; // Ref to TechnicalDataSheet
    preferredStockItemId?: mongoose.Types.ObjectId | null; // Ref to StockItem (Suggested Batch)
}

const PaintingSchemeSchema: Schema = new Schema({
    organizationId: { type: String, index: true },
    projectId: { type: Schema.Types.ObjectId, ref: 'Project', required: true },
    name: { type: String, required: true },
    type: { type: String },
    coat: { type: String },
    solidsVolume: { type: Number },
    yieldTheoretical: { type: Number },
    epsMin: { type: Number },
    epsMax: { type: Number },
    dilution: { type: Number },
    manufacturer: { type: String },
    color: { type: String },
    notes: { type: String },
    // Consumption Planning
    paintConsumption: { type: Number },
    thinnerConsumption: { type: Number },
    paintId: { type: Schema.Types.ObjectId, ref: 'TechnicalDataSheet' },
    thinnerId: { type: Schema.Types.ObjectId, ref: 'TechnicalDataSheet' },
    preferredStockItemId: { type: Schema.Types.ObjectId, ref: 'StockItem' }
}, { strict: false });

console.log("✅✅✅ PAINTING SCHEME MODEL (WITH CONSUMPTION) LOADED ✅✅✅");

// Force model recompilation to ensure schema updates are applied
if (mongoose.models.PaintingScheme) {
    delete mongoose.models.PaintingScheme;
}

export default mongoose.model<IPaintingScheme>('PaintingScheme', PaintingSchemeSchema);
