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

export interface IPieceCategory {
    id: string; // Keep as string for internal mapping if needed, or convert to Sub-document
    name: string;
    organizationId?: string;
    weight: number;
    area?: number; // Área em m² para cálculo alternativo
    historicalYield: number;
    historicalDft: number;
    efficiency: number;
}

const PieceCategorySchema: Schema = new Schema({
    name: { type: String, required: true },
    weight: { type: Number, required: true },
    area: { type: Number }, // Área em m² (opcional)
    historicalYield: { type: Number, required: true },
    historicalDft: { type: Number, required: true },
    efficiency: { type: Number, required: true },
});

export interface IYieldStudy extends Document {
    name: string;
    organizationId?: string;
    dataSheetId: mongoose.Types.ObjectId;
    targetDft: number;
    dilutionPercent: number;
    categories: IPieceCategory[];
    totalWeight: number;
    estimatedPaintVolume: number;
    estimatedReducerVolume: number;
    estimatedPaintVolumeByArea?: number; // Cálculo por área (m²)
    estimatedReducerVolumeByArea?: number; // Cálculo por área (m²)
    averageComplexity: number;
}

const YieldStudySchema: Schema = new Schema({
    name: { type: String, required: true },
    organizationId: { type: String, index: true },
    dataSheetId: { type: Schema.Types.ObjectId, ref: 'TechnicalDataSheet', required: true },
    targetDft: { type: Number, required: true },
    dilutionPercent: { type: Number, default: 0 },
    categories: [PieceCategorySchema],
    totalWeight: { type: Number },
    estimatedPaintVolume: { type: Number },
    estimatedReducerVolume: { type: Number },
    estimatedPaintVolumeByArea: { type: Number }, // Cálculo por área
    estimatedReducerVolumeByArea: { type: Number }, // Cálculo por área
    averageComplexity: { type: Number },
}, { timestamps: true });

export default mongoose.models.YieldStudy || mongoose.model<IYieldStudy>('YieldStudy', YieldStudySchema);
