/**
 * Commission Model
 * Tracks individual commission records for sales reps,
 * including splits, disputes, and payout details.
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type CommissionType = 'deal' | 'recurring' | 'bonus' | 'override' | 'split' | 'accelerator' | 'milestone';

export type CommissionStatus = 'pending' | 'approved' | 'paid' | 'disputed' | 'cancelled';

export interface ICommissionSplit extends Document {
  repId: string;
  repName: string;
  splitPercentage: number;
  splitAmount: number;
}

export interface ICommission extends Document {
  companyId: string;
  planId?: string;
  repId: string;
  repName?: string;
  dealId?: string;
  type: CommissionType;
  status: CommissionStatus;
  dealName?: string;
  dealValue: number;
  commissionRate?: number;
  commissionAmount: number;
  splitWith: ICommissionSplit[];
  dealClosedDate?: string;
  commissionEarnedDate?: string;
  payoutDate?: string;
  approvedBy?: string;
  approvedAt?: Date;
  disputeReason?: string;
  notes?: string;
  tags: string[];

  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// SUB-SCHEMAS
// ============================================

const CommissionSplitSchema = new Schema<ICommissionSplit>(
  {
    repId: { type: String, required: true },
    repName: { type: String, trim: true },
    splitPercentage: { type: Number, default: 0 },
    splitAmount: { type: Number, default: 0 },
  },
  { _id: false }
);

// ============================================
// MAIN SCHEMA
// ============================================

const CommissionSchema = new Schema<ICommission>(
  {
    companyId: {
      type: String,
      required: [true, 'Company ID is required'],
      index: true,
    },
    planId: { type: String },
    repId: {
      type: String,
      required: [true, 'Rep ID is required'],
    },
    repName: { type: String, trim: true },
    dealId: { type: String },
    type: {
      type: String,
      enum: ['deal', 'recurring', 'bonus', 'override', 'split', 'accelerator', 'milestone'],
      default: 'deal',
    },
    status: {
      type: String,
      enum: ['pending', 'approved', 'paid', 'disputed', 'cancelled'],
      default: 'pending',
    },
    dealName: { type: String, trim: true },
    dealValue: {
      type: Number,
      required: [true, 'Deal value is required'],
    },
    commissionRate: { type: Number },
    commissionAmount: {
      type: Number,
      required: [true, 'Commission amount is required'],
    },
    splitWith: { type: [CommissionSplitSchema], default: [] },
    dealClosedDate: { type: String },
    commissionEarnedDate: { type: String },
    payoutDate: { type: String },
    approvedBy: { type: String },
    approvedAt: { type: Date },
    disputeReason: { type: String, trim: true },
    notes: { type: String, trim: true },
    tags: { type: [String], default: [] },
  },
  { timestamps: true }
);

// Indexes
CommissionSchema.index({ companyId: 1, status: 1 });
CommissionSchema.index({ companyId: 1, repId: 1 });
CommissionSchema.index({ companyId: 1, type: 1 });

export const Commission = mongoose.models.Commission || mongoose.model<ICommission>('Commission', CommissionSchema);