/**
 * AdABTest Model
 * A/B testing for ad variations
 */

import mongoose, { Document, Schema } from 'mongoose'; 

// ============================================
// TYPES
// ============================================

export type ABTestStatus = 'draft' | 'running' | 'paused' | 'completed';
export type ABTestMetric = 'ctr' | 'conversion-rate' | 'cost-per-click' | 'cost-per-conversion' | 'roas';

export interface IABTestVariant {
  id: string;
  name: string;
  type: 'headline' | 'description' | 'cta' | 'image' | 'video' | 'audience' | 'landing-page';
  content: string;
  mediaUrl?: string;
  ctr?: number;
  conversionRate?: number;
  impressions?: number;
  clicks?: number;
  conversions?: number;
  spend?: number;
  isWinner?: boolean;
}

export interface IAdABTest extends Document {
  companyId: string;
  campaignId: string;
  name: string;
  description?: string;
  status: ABTestStatus;
  variants: IABTestVariant[];
  startDate: string;
  endDate?: string;
  confidenceLevel?: number;
  winnerVariantId?: string;
  metric: ABTestMetric;
  minimumSampleSize?: number;
  notes?: string;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// SUB-SCHEMAS
// ============================================

const ABTestVariantSchema = new Schema({
  id: { type: String, required: true },
  name: { type: String, required: true },
  type: {
    type: String,
    enum: ['headline', 'description', 'cta', 'image', 'video', 'audience', 'landing-page'],
    required: true,
  },
  content: { type: String, required: true },
  mediaUrl: { type: String },
  ctr: { type: Number, default: 0 },
  conversionRate: { type: Number, default: 0 },
  impressions: { type: Number, default: 0 },
  clicks: { type: Number, default: 0 },
  conversions: { type: Number, default: 0 },
  spend: { type: Number, default: 0 },
  isWinner: { type: Boolean, default: false },
}, { _id: false });

// ============================================
// MAIN SCHEMA
// ============================================

const AdABTestSchema = new Schema<IAdABTest>({
  companyId: { type: String, required: [true, 'Company ID is required'] },
  campaignId: { type: String, required: [true, 'Campaign ID is required'], index: true },
  name: { type: String, required: [true, 'Test name is required'], trim: true },
  description: { type: String, trim: true },
  status: {
    type: String,
    enum: ['draft', 'running', 'paused', 'completed'],
    default: 'draft',
  },
  variants: [ABTestVariantSchema],
  startDate: { type: String, required: [true, 'Start date is required'] },
  endDate: { type: String },
  confidenceLevel: { type: Number },
  winnerVariantId: { type: String },
  metric: {
    type: String,
    enum: ['ctr', 'conversion-rate', 'cost-per-click', 'cost-per-conversion', 'roas'],
    default: 'ctr',
  },
  minimumSampleSize: { type: Number },
  notes: { type: String, trim: true },
}, { timestamps: true });

// Indexes
AdABTestSchema.index({ companyId: 1, campaignId: 1 });
AdABTestSchema.index({ companyId: 1, status: 1 });

export const AdABTest = mongoose.model<IAdABTest>('AdABTest', AdABTestSchema);