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

export interface IApplicationRecord extends Document {
    organizationId?: string;
    createdBy?: string;
    projectId: mongoose.Types.ObjectId;
    coatStage: string;
    pieceDescription?: string | null;
    date?: Date | null;
    operator?: string | null;
    realWeight?: number | null;
    volumeUsed?: number | null;
    areaPainted?: number | null;
    wetThicknessAvg?: number | null;
    dryThicknessCalc?: number | null;
    method?: string | null;
    diluentUsed?: number | null;
    notes?: string | null;
    items?: {
        partId: mongoose.Types.ObjectId;
        quantity: number;
    }[];
}

const ApplicationRecordSchema: Schema = new Schema({
    organizationId: { type: String, index: true },
    createdBy: { type: String, index: true },
    projectId: { type: Schema.Types.ObjectId, ref: 'Project', required: true },
    coatStage: { type: String, required: true },
    pieceDescription: { type: String }, // Can be auto-generated or manual name for the Batch
    date: { type: Date },
    operator: { type: String },
    realWeight: { type: Number },
    volumeUsed: { type: Number },
    areaPainted: { type: Number },
    wetThicknessAvg: { type: Number },
    dryThicknessCalc: { type: Number },
    method: { type: String },
    diluentUsed: { type: Number },
    notes: { type: String },
    items: [{
        partId: { type: Schema.Types.ObjectId, ref: 'Part' },
        quantity: { type: Number, required: true }
    }]
}, { timestamps: true });

export default mongoose.models.ApplicationRecord || mongoose.model<IApplicationRecord>('ApplicationRecord', ApplicationRecordSchema);
