/**
 * Marketing Calendar & Timing Models
 *
 * MarketingCalendarEvent — scheduled marketing events, campaigns, and milestones
 * SeasonalPlan — seasonal and quarterly marketing planning
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// MARKETING CALENDAR EVENT
// ============================================

export type CalendarEventType = 'campaign' | 'content' | 'event' | 'product-launch' | 'seasonal' | 'promotion' | 'milestone' | 'other';
export type CalendarPriority = 'critical' | 'high' | 'medium' | 'low';
export type CalendarRecurrence = 'none' | 'daily' | 'weekly' | 'biweekly' | 'monthly' | 'quarterly' | 'annually';
export type CalendarEventStatus = 'draft' | 'scheduled' | 'in-progress' | 'completed' | 'cancelled';

export interface IMarketingCalendarEvent extends Document {
  companyId: string;
  title: string;
  description?: string;
  eventType: CalendarEventType;
  priority: CalendarPriority;
  status: CalendarEventStatus;
  startDate: string;
  endDate?: string;
  recurrence: CalendarRecurrence;
  budgetAllocated?: number;
  budgetActual?: number;
  channels: string[];
  targetAudience?: string;
  goals?: string[];
  kpis?: string[];
  seasonalTag?: string;
  quarter?: string;
  linkedCampaignIds?: string[];
  linkedContentIds?: string[];
  tags?: string[];
  aiGenerated?: boolean;
  aiStatus?: 'idle' | 'processing' | 'done' | 'error';
  notes?: string;
  createdAt: Date;
  updatedAt: Date;
}

const MarketingCalendarEventSchema = new Schema<IMarketingCalendarEvent>(
  {
    companyId: {
      type: String,
      required: [true, 'Company ID is required'],
      index: true,
    },
    title: {
      type: String,
      required: [true, 'Title is required'],
      trim: true,
      maxlength: [200, 'Title cannot exceed 200 characters'],
    },
    description: {
      type: String,
      trim: true,
    },
    eventType: {
      type: String,
      enum: ['campaign', 'content', 'event', 'product-launch', 'seasonal', 'promotion', 'milestone', 'other'],
      default: 'campaign',
    },
    priority: {
      type: String,
      enum: ['critical', 'high', 'medium', 'low'],
      default: 'medium',
    },
    status: {
      type: String,
      enum: ['draft', 'scheduled', 'in-progress', 'completed', 'cancelled'],
      default: 'draft',
    },
    startDate: {
      type: String,
      required: [true, 'Start date is required'],
    },
    endDate: {
      type: String,
    },
    recurrence: {
      type: String,
      enum: ['none', 'daily', 'weekly', 'biweekly', 'monthly', 'quarterly', 'annually'],
      default: 'none',
    },
    budgetAllocated: {
      type: Number,
      default: 0,
    },
    budgetActual: {
      type: Number,
      default: 0,
    },
    channels: {
      type: [String],
      default: [],
    },
    targetAudience: {
      type: String,
      trim: true,
    },
    goals: {
      type: [String],
      default: [],
    },
    kpis: {
      type: [String],
      default: [],
    },
    seasonalTag: {
      type: String,
      trim: true,
    },
    quarter: {
      type: String,
      trim: true,
    },
    linkedCampaignIds: {
      type: [String],
      default: [],
    },
    linkedContentIds: {
      type: [String],
      default: [],
    },
    tags: {
      type: [String],
      default: [],
    },
    aiGenerated: {
      type: Boolean,
      default: false,
    },
    aiStatus: {
      type: String,
      enum: ['idle', 'processing', 'done', 'error'],
      default: 'idle',
    },
    notes: {
      type: String,
      trim: true,
    },
  },
  { timestamps: true }
);

MarketingCalendarEventSchema.index({ companyId: 1, status: 1 });
MarketingCalendarEventSchema.index({ companyId: 1, startDate: 1 });

export const MarketingCalendarEvent = mongoose.model<IMarketingCalendarEvent>(
  'MarketingCalendarEvent',
  MarketingCalendarEventSchema
);

// ============================================
// SEASONAL PLAN
// ============================================

export type SeasonType = 'spring' | 'summer' | 'autumn' | 'winter' | 'q1' | 'q2' | 'q3' | 'q4' | 'holiday' | 'custom';
export type SeasonalPlanStatus = 'draft' | 'active' | 'completed' | 'archived';

export interface ISeasonalPlan extends Document {
  companyId: string;
  name: string;
  description?: string;
  season: SeasonType;
  year: number;
  themes?: string[];
  targetChannels?: string[];
  budgetAllocation?: Map<string, number>;
  keyDates?: string[];
  goals?: string[];
  status: SeasonalPlanStatus;
  aiGenerated?: boolean;
  aiStatus?: 'idle' | 'processing' | 'done' | 'error';
  tags?: string[];
  createdAt: Date;
  updatedAt: Date;
}

const SeasonalPlanSchema = new Schema<ISeasonalPlan>(
  {
    companyId: {
      type: String,
      required: [true, 'Company ID is required'],
      index: true,
    },
    name: {
      type: String,
      required: [true, 'Name is required'],
      trim: true,
      maxlength: [200, 'Name cannot exceed 200 characters'],
    },
    description: {
      type: String,
      trim: true,
      maxlength: [1000, 'Description cannot exceed 1000 characters'],
    },
    season: {
      type: String,
      enum: ['spring', 'summer', 'autumn', 'winter', 'q1', 'q2', 'q3', 'q4', 'holiday', 'custom'],
      required: [true, 'Season is required'],
    },
    year: {
      type: Number,
      required: [true, 'Year is required'],
      min: [2000, 'Year must be a valid 4-digit year (2000–2100)'],
      max: [2100, 'Year must be a valid 4-digit year (2000–2100)'],
    },
    themes: {
      type: [String],
      default: [],
    },
    targetChannels: {
      type: [String],
      default: [],
    },
    budgetAllocation: {
      type: Map,
      of: Number,
      default: {},
    },
    keyDates: {
      type: [String],
      default: [],
    },
    goals: {
      type: [String],
      default: [],
    },
    status: {
      type: String,
      enum: ['draft', 'active', 'completed', 'archived'],
      default: 'draft',
    },
    aiGenerated: {
      type: Boolean,
      default: false,
    },
    aiStatus: {
      type: String,
      enum: ['idle', 'processing', 'done', 'error'],
      default: 'idle',
    },
    tags: {
      type: [String],
      default: [],
    },
  },
  { timestamps: true }
);

SeasonalPlanSchema.index({ companyId: 1, season: 1 });
SeasonalPlanSchema.index({ companyId: 1, year: 1 });

export const SeasonalPlan = mongoose.model<ISeasonalPlan>('SeasonalPlan', SeasonalPlanSchema);