import mongoose, { Schema, Document } from 'mongoose';

export interface IReportDocument extends Document {
  // _id is handled by Mongoose
  title: string;
  type: 'Daily' | 'Monthly' | 'Audit' | 'Financial' | 'Custom';
  generatedDate: Date;
  createdBy: any;
  format: 'PDF' | 'Excel' | 'Word';
  size: string;
  status: 'Ready' | 'Processing' | 'Failed';
  downloadUrl?: string;
  projectId?: any;
  createdAt: Date;
  updatedAt: Date;
}

const reportSchema = new Schema<IReportDocument>(
  {
    title: { type: String, required: true },
    type: {
      type: String,
      enum: ['Daily', 'Monthly', 'Audit', 'Financial', 'Custom'],
      required: true,
    },
    generatedDate: { type: Date, default: Date.now },
    createdBy: { type: Schema.Types.Mixed, ref: 'User', required: true },
    format: {
      type: String,
      enum: ['PDF', 'Excel', 'Word'],
      default: 'PDF',
    },
    size: { type: String, default: '0 MB' },
    status: {
      type: String,
      enum: ['Ready', 'Processing', 'Failed'],
      default: 'Processing',
    },
    downloadUrl: String,
    projectId: { type: Schema.Types.Mixed, ref: 'Project' },
  },
  {
    timestamps: true,
  }
);

reportSchema.index({ createdBy: 1 });
reportSchema.index({ type: 1 });
reportSchema.index({ generatedDate: -1 });
reportSchema.index({ status: 1 });

export const Report = mongoose.model<IReportDocument>('Report', reportSchema);
