/**
 * Sales Quota Model
 * Defines individual sales quotas with targets, tracking, and attainment
 * for reps and teams.
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type QuotaPeriod = 'monthly' | 'quarterly' | 'semi-annual' | 'annual' | 'custom';

export type QuotaType = 'revenue' | 'deals_count' | 'arr' | 'mrr' | 'units' | 'activity';

export type QuotaStatus = 'on-track' | 'at-risk' | 'behind' | 'achieved' | 'exceeded';

export interface ISalesQuota extends Document {
  companyId: string;
  name: string;
  description?: string;
  period: QuotaPeriod;
  periodStart?: string;
  periodEnd?: string;
  quotaType: QuotaType;
  targetAmount: number;
  stretchTarget?: number;
  minimumTarget?: number;
  assignedTo: string;
  assignedToName?: string;
  teamId?: string;
  teamName?: string;
  currentAmount: number;
  percentageAchieved: number;
  status: QuotaStatus;
  productIds: string[];
  icpIds: string[];
  weightInTeamQuota?: number;
  bonusMultiplier?: number;
  isActive: boolean;
  notes?: string;
  tags: string[];

  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const SalesQuotaSchema = new Schema<ISalesQuota>(
  {
    companyId: {
      type: String,
      required: [true, 'Company ID is required'],
      index: true,
    },
    name: {
      type: String,
      required: [true, 'Name is required'],
      trim: true,
    },
    description: { type: String, trim: true },
    period: {
      type: String,
      enum: ['monthly', 'quarterly', 'semi-annual', 'annual', 'custom'],
      default: 'monthly',
    },
    periodStart: { type: String },
    periodEnd: { type: String },
    quotaType: {
      type: String,
      enum: ['revenue', 'deals_count', 'arr', 'mrr', 'units', 'activity'],
      default: 'revenue',
    },
    targetAmount: {
      type: Number,
      required: [true, 'Target amount is required'],
    },
    stretchTarget: { type: Number },
    minimumTarget: { type: Number },
    assignedTo: {
      type: String,
      required: [true, 'Assigned to is required'],
    },
    assignedToName: { type: String, trim: true },
    teamId: { type: String },
    teamName: { type: String, trim: true },
    currentAmount: { type: Number, default: 0 },
    percentageAchieved: { type: Number, default: 0 },
    status: {
      type: String,
      enum: ['on-track', 'at-risk', 'behind', 'achieved', 'exceeded'],
      default: 'on-track',
    },
    productIds: { type: [String], default: [] },
    icpIds: { type: [String], default: [] },
    weightInTeamQuota: { type: Number },
    bonusMultiplier: { type: Number },
    isActive: { type: Boolean, default: true },
    notes: { type: String, trim: true },
    tags: { type: [String], default: [] },
  },
  { timestamps: true }
);

// Indexes
SalesQuotaSchema.index({ companyId: 1, assignedTo: 1 });
SalesQuotaSchema.index({ companyId: 1, period: 1 });
SalesQuotaSchema.index({ companyId: 1, status: 1 });
SalesQuotaSchema.index({ companyId: 1, quotaType: 1 });

export const SalesQuota = mongoose.models.SalesQuota || mongoose.model<ISalesQuota>('SalesQuota', SalesQuotaSchema);