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

export interface IInstrument extends Document {
    organizationId: string;
    name: string;
    type: string; // Ex: Medidor de Camada, Termo-higrômetro
    manufacturer?: string;
    modelName?: string;
    serialNumber: string;
    calibrationDate?: Date;
    calibrationExpirationDate?: Date;
    certificateUrl?: string; // URL do PDF
    status: 'active' | 'inactive' | 'maintenance' | 'expired';
    notes?: string;
    createdAt: Date;
    updatedAt: Date;
}

const InstrumentSchema: Schema = new Schema({
    organizationId: { type: String, required: true, index: true },
    name: { type: String, required: true },
    type: { type: String, required: true },
    manufacturer: { type: String },
    modelName: { type: String },
    serialNumber: { type: String, required: true },
    calibrationDate: { type: Date },
    calibrationExpirationDate: { type: Date },
    certificateUrl: { type: String },
    status: {
        type: String,
        enum: ['active', 'inactive', 'maintenance', 'expired'],
        default: 'active'
    },
    notes: { type: String }
}, { timestamps: true });

// Index para evitar duplicidade de número de série dentro da mesma organização
InstrumentSchema.index({ organizationId: 1, serialNumber: 1 }, { unique: true });

export default mongoose.models.Instrument || mongoose.model<IInstrument>('Instrument', InstrumentSchema);
