/**
 * Sales Dashboard Model
 * Configurable dashboards with widget layouts and filters
 * for pipeline, performance, commission, and quota visualisations.
 */

import mongoose, { Schema, Document, Types } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type DashboardType = 'pipeline' | 'performance' | 'commission' | 'quota-attainment' | 'activity' | 'forecast' | 'custom';

export type DashboardLayout = 'grid' | 'tabbed' | 'scrollable';

export interface IDashboardWidget extends Document {
  id: string;
  type: string;
  title: string;
  dataSource: string;
  metric: string;
  size?: string;
  order?: number;
  config?: Record<string, unknown>;
}

export interface IDashboardFilters extends Document {
  dateRange?: {
    start?: string;
    end?: string;
    preset?: string;
  };
  repIds?: string[];
  teamIds?: string[];
  productIds?: string[];
  icpIds?: string[];
  status?: string[];
}

export interface ISalesDashboard extends Document {
  companyId: string;
  name: string;
  description?: string;
  type: DashboardType;
  layout: DashboardLayout;
  isDefault: boolean;
  widgets: IDashboardWidget[];
  filters?: IDashboardFilters;

  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// SUB-SCHEMAS
// ============================================

const DashboardWidgetSchema = new Schema<IDashboardWidget>(
  {
    id: { type: String, required: true },
    type: { type: String, required: true },
    title: { type: String, required: true },
    dataSource: { type: String, required: true },
    metric: { type: String, required: true },
    size: { type: String },
    order: { type: Number, default: 0 },
    config: { type: Schema.Types.Mixed },
  },
  { _id: false }
);

const DashboardFiltersSchema = new Schema<IDashboardFilters>(
  {
    dateRange: {
      start: { type: String },
      end: { type: String },
      preset: { type: String },
    },
    repIds: { type: [String], default: [] },
    teamIds: { type: [String], default: [] },
    productIds: { type: [String], default: [] },
    icpIds: { type: [String], default: [] },
    status: { type: [String], default: [] },
  },
  { _id: false }
);

// ============================================
// MAIN SCHEMA
// ============================================

const SalesDashboardSchema = new Schema<ISalesDashboard>(
  {
    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 },
    type: {
      type: String,
      enum: ['pipeline', 'performance', 'commission', 'quota-attainment', 'activity', 'forecast', 'custom'],
      default: 'pipeline',
    },
    layout: {
      type: String,
      enum: ['grid', 'tabbed', 'scrollable'],
      default: 'grid',
    },
    isDefault: { type: Boolean, default: false },
    widgets: { type: [DashboardWidgetSchema], default: [] },
    filters: { type: DashboardFiltersSchema, default: {} },
  },
  { timestamps: true }
);

// Indexes
SalesDashboardSchema.index({ companyId: 1, type: 1 });
SalesDashboardSchema.index({ companyId: 1, isDefault: 1 });

export const SalesDashboard = mongoose.models.SalesDashboard || mongoose.model<ISalesDashboard>('SalesDashboard', SalesDashboardSchema);