import mongoose, { Schema, Document } from 'mongoose';

export interface IFinancialDataDocument extends Document {
  // _id is handled by Mongoose
  projectId: mongoose.Types.ObjectId;
  month: string; // Format: "YYYY-MM"
  revenue: number;
  opex: number;
  netProfit: number;
  cumulativeROI: number;
  createdAt: Date;
  updatedAt: Date;
}

const financialDataSchema = new Schema<IFinancialDataDocument>(
  {
    projectId: { type: Schema.Types.ObjectId, ref: 'Project', required: true },
    month: { type: String, required: true },
    revenue: { type: Number, default: 0 },
    opex: { type: Number, default: 0 },
    netProfit: { type: Number, default: 0 },
    cumulativeROI: { type: Number, default: 0 },
  },
  {
    timestamps: true,
  }
);

financialDataSchema.index({ projectId: 1, month: 1 }, { unique: true });
financialDataSchema.index({ month: -1 });

export const FinancialData = mongoose.model<IFinancialDataDocument>('FinancialData', financialDataSchema);
