/**
 * AdAIRecommendation Model
 * AI-generated recommendations for ad optimization
 */

import mongoose, { Document, Schema } from 'mongoose';

// ============================================
// TYPES
// ============================================

export type RecommendationCategory = 'budget' | 'audience' | 'creative' | 'targeting' | 'bidding' | 'timing' | 'platform';
export type RecommendationPriority = 'low' | 'medium' | 'high' | 'critical';

export interface IAdAIRecommendation extends Document {
  companyId: string;
  campaignId?: string;
  category: RecommendationCategory;
  priority: RecommendationPriority;
  title: string;
  description: string;
  rationale: string;
  expectedImpact: string;
  confidence: number;
  status: 'new' | 'viewed' | 'applied' | 'dismissed';
  source: 'ai' | 'manual';
  relatedEntityId?: string;
  relatedEntityType?: 'campaign' | 'ad' | 'audience' | 'budget' | 'creative';
  actionData?: Record<string, unknown>;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const AdAIRecommendationSchema = new Schema<IAdAIRecommendation>({
  companyId: { type: String, required: [true, 'Company ID is required'] },
  campaignId: { type: String },
  category: {
    type: String,
    enum: ['budget', 'audience', 'creative', 'targeting', 'bidding', 'timing', 'platform'],
    required: [true, 'Category is required'],
  },
  priority: {
    type: String,
    enum: ['low', 'medium', 'high', 'critical'],
    default: 'medium',
  },
  title: { type: String, required: [true, 'Title is required'], trim: true },
  description: { type: String, required: [true, 'Description is required'], trim: true },
  rationale: { type: String, trim: true },
  expectedImpact: { type: String, trim: true },
  confidence: { type: Number, default: 0, min: 0, max: 100 },
  status: {
    type: String,
    enum: ['new', 'viewed', 'applied', 'dismissed'],
    default: 'new',
  },
  source: {
    type: String,
    enum: ['ai', 'manual'],
    default: 'manual',
  },
  relatedEntityId: { type: String },
  relatedEntityType: {
    type: String,
    enum: ['campaign', 'ad', 'audience', 'budget', 'creative'],
  },
  actionData: { type: Schema.Types.Mixed },
}, { timestamps: true });

// Indexes
AdAIRecommendationSchema.index({ companyId: 1, status: 1 });
AdAIRecommendationSchema.index({ companyId: 1, category: 1 });
AdAIRecommendationSchema.index({ companyId: 1, campaignId: 1 });

export const AdAIRecommendation = mongoose.model<IAdAIRecommendation>('AdAIRecommendation', AdAIRecommendationSchema);