import mongoose, { Schema, Document } from 'mongoose';

export interface IWorkOrderDocument extends Document {
  _id: any;
  woCode: string;
  ticketId?: any;
  projectId: any;
  assetId: any;
  contractId?: any;
  workOrderType: 'PM' | 'CM' | 'Cleaning' | 'Inspection' | 'Upgrade';
  title: string;
  description?: string;
  status: 'Draft' | 'Approved' | 'InProgress' | 'Completed' | 'Cancelled' | 'In Progress' | 'Verified';
  priority: 'Low' | 'Medium' | 'High';
  scheduledStart?: Date;
  scheduledEnd?: Date;
  actualStart?: Date;
  actualEnd?: Date;
  assignedTechnicianId?: any;
  supervisorId?: any;
  costLabor: number;
  costMaterial: number;
  costExternal: number;
  totalCost: number;
  checklistProgress?: number;

  // Legacy fields for UI
  siteId?: string;
  assignedTo?: string;
  type?: string;
  scheduledDate?: string;

  createdAt: Date;
  updatedAt: Date;
}

const workOrderSchema = new Schema<IWorkOrderDocument>(
  {
    _id: { type: Schema.Types.Mixed, default: () => new mongoose.Types.ObjectId().toString() }, // Support both string demo IDs and auto-gen string IDs
    woCode: { type: String, required: true, unique: true },
    ticketId: { type: Schema.Types.Mixed, ref: 'Ticket' },
    projectId: { type: Schema.Types.Mixed, ref: 'Project', required: true },
    assetId: { type: Schema.Types.Mixed, ref: 'Asset' },
    contractId: { type: Schema.Types.Mixed, ref: 'Contract' },
    workOrderType: {
      type: String,
      required: true,
      enum: ['PM', 'CM', 'Cleaning', 'Inspection', 'Upgrade'],
    },
    title: { type: String, required: true },
    description: String,
    status: {
      type: String,
      enum: ['Draft', 'Approved', 'InProgress', 'Completed', 'Cancelled', 'In Progress', 'Verified'],
      default: 'Draft',
    },
    priority: {
      type: String,
      enum: ['Low', 'Medium', 'High'],
      default: 'Medium',
    },
    scheduledStart: Date,
    scheduledEnd: Date,
    actualStart: Date,
    actualEnd: Date,
    assignedTechnicianId: { type: Schema.Types.Mixed, ref: 'User' },
    supervisorId: { type: Schema.Types.Mixed, ref: 'User' },
    costLabor: { type: Number, default: 0 },
    costMaterial: { type: Number, default: 0 },
    costExternal: { type: Number, default: 0 },
    totalCost: { type: Number, default: 0 },
    checklistProgress: { type: Number, default: 0, min: 0, max: 100 },

    // Legacy support
    siteId: String,
    assignedTo: String,
    type: String,
    scheduledDate: String,
  },
  {
    timestamps: true,
    collection: 'workOrders',
    strict: false,
  }
);

workOrderSchema.pre('save', function (next) {
  this.totalCost = (this.costLabor || 0) + (this.costMaterial || 0) + (this.costExternal || 0);
  next();
});

workOrderSchema.index({ projectId: 1 });
workOrderSchema.index({ assetId: 1 });
workOrderSchema.index({ status: 1 });
workOrderSchema.index({ ticketId: 1 });
workOrderSchema.index({ assignedTechnicianId: 1 });

export const WorkOrder = mongoose.model<IWorkOrderDocument>('WorkOrder', workOrderSchema);
