/**
 * Speaking Engagement Model
 * AI-powered speech generator with 16 speech types, smart context, stage instructions, and delivery guidance
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type SpeechType =
  | 'tedx-speech'
  | 'josh-talks-speech'
  | 'one-line-speech'
  | 'storytelling-speech'
  | 'elevator-pitch'
  | 'keynote-speech'
  | 'event-speech'
  | 'motivational-speech'
  | 'company-introduction'
  | 'founder-introduction'
  | 'employee-introduction'
  | 'product-launch-speech'
  | 'award-acceptance'
  | 'investor-pitch-speech'
  | 'networking-introduction'
  | 'custom-speech';

export type SpeakerType = 'founder' | 'employee' | 'ceo' | 'manager' | 'other';

export type SpeechLanguage =
  | 'en' | 'hi' | 'mr' | 'es' | 'fr' | 'de' | 'pt' | 'it' | 'nl' | 'ru'
  | 'ja' | 'ko' | 'zh' | 'ar' | 'tr' | 'pl' | 'sv' | 'no' | 'da' | 'fi'
  | 'cs' | 'el' | 'he' | 'th' | 'vi' | 'id' | 'ms' | 'fil' | 'bn' | 'ur'
  | 'ta' | 'te' | 'kn' | 'ml' | 'pa' | 'gu' | 'sw' | 'am';

export type SpeechDuration = '30s' | '1min' | '2min' | '3min' | '5min' | '10min' | '15min' | '20min' | '30min' | '45min' | '1hr' | '2hr-plus';

export type SpeechStatus = 'draft' | 'review' | 'approved' | 'published' | 'archived';

export type SpeechTone =
  | 'professional'
  | 'inspirational'
  | 'motivational'
  | 'emotional'
  | 'humorous'
  | 'formal'
  | 'friendly'
  | 'corporate'
  | 'casual'
  | 'storytelling';

export type SpeechSource = 'ai-generation' | 'manual';

export interface ISpeakingEngagement extends Document {
  // Core fields
  companyId: string;
  name: string;
  speechType: SpeechType;
  audienceType: string;
  language: SpeechLanguage;
  duration: SpeechDuration;
  speakerType: SpeakerType;
  tone: SpeechTone;

  // Speaker profile fields (auto-populated or manual)
  speakerName?: string;
  speakerPosition?: string;
  speakerCompanyName?: string;
  speakerDepartment?: string;
  speakerIndustry?: string;

  // Additional context
  topic?: string;
  occasionContext?: string;
  additionalNotes?: string;

  // AI-generated outputs
  content: string;
  structure?: {
    opening: string;
    hook: string;
    story: string;
    mainMessage: string;
    conclusion: string;
    callToAction: string;
  };
  speakerNotes?: {
    deliveryGuidance: string[];
    emphasisPoints: string[];
    pausePoints: string[];
    audienceEngagement: string[];
  };
  stageInstructions?: {
    pausePoints: { timestamp: string; instruction: string }[];
    emphasisPoints: { phrase: string; intensity: 'light' | 'moderate' | 'strong' }[];
    audienceEngagementMoments: { moment: string; technique: string }[];
  };
  confidenceTips?: {
    deliverySuggestions: string[];
    bodyLanguageTips: string[];
    vocalVariationTips: string[];
  };
  specialOutputs?: Record<string, string | string[]>;

  // Multi-language translations
  translations?: {
    language: string;
    content?: string;
    structure?: {
      opening: string;
      hook: string;
      story: string;
      mainMessage: string;
      conclusion: string;
      callToAction: string;
    };
    speakerNotes?: {
      deliveryGuidance: string[];
      emphasisPoints: string[];
      pausePoints: string[];
      audienceEngagement: string[];
    };
    stageInstructions?: {
      pausePoints: { timestamp: string; instruction: string }[];
      emphasisPoints: { phrase: string; intensity: 'light' | 'moderate' | 'strong' }[];
      audienceEngagementMoments: { moment: string; technique: string }[];
    };
    confidenceTips?: {
      deliverySuggestions: string[];
      bodyLanguageTips: string[];
      vocalVariationTips: string[];
    };
    specialOutputs?: Record<string, string | string[]>;
  }[];

  // Metadata
  source: SpeechSource;
  status: SpeechStatus;
  version: number;

  // Linked data references
  linkedData?: {
    founderId?: string;
    employeeId?: string;
    productId?: string;
  };

  // Timestamps
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const SpeakingEngagementSchema = new Schema<ISpeakingEngagement>({
  // Core fields
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
  },
  name: {
    type: String,
    required: [true, 'Speech name is required'],
    trim: true,
    maxlength: [500, 'Name cannot exceed 500 characters'],
  },
  speechType: {
    type: String,
    enum: [
      'tedx-speech', 'josh-talks-speech', 'one-line-speech', 'storytelling-speech',
      'elevator-pitch', 'keynote-speech', 'event-speech', 'motivational-speech',
      'company-introduction', 'founder-introduction', 'employee-introduction',
      'product-launch-speech', 'award-acceptance', 'investor-pitch-speech',
      'networking-introduction', 'custom-speech',
    ],
    required: [true, 'Speech type is required'],
  },
  audienceType: {
    type: String,
    trim: true,
    default: '',
  },
  language: {
    type: String,
    enum: ['en', 'hi', 'mr', 'es', 'fr', 'de', 'pt', 'it', 'nl', 'ru', 'ja', 'ko', 'zh', 'ar', 'tr', 'pl', 'sv', 'no', 'da', 'fi', 'cs', 'el', 'he', 'th', 'vi', 'id', 'ms', 'fil', 'bn', 'ur', 'ta', 'te', 'kn', 'ml', 'pa', 'gu', 'sw', 'am'],
    default: 'en',
  },
  duration: {
    type: String,
    enum: ['30s', '1min', '2min', '3min', '5min', '10min', '15min', '20min', '30min', '45min', '1hr', '2hr-plus'],
    default: '2min',
  },
  speakerType: {
    type: String,
    enum: ['founder', 'employee', 'ceo', 'manager', 'other'],
    default: 'founder',
  },
  tone: {
    type: String,
    enum: [
      'professional', 'inspirational', 'motivational', 'emotional', 'humorous',
      'formal', 'friendly', 'corporate', 'casual', 'storytelling',
    ],
    default: 'professional',
  },

  // Speaker profile fields
  speakerName: { type: String, trim: true },
  speakerPosition: { type: String, trim: true },
  speakerCompanyName: { type: String, trim: true },
  speakerDepartment: { type: String, trim: true },
  speakerIndustry: { type: String, trim: true },

  // Additional context
  topic: { type: String, trim: true },
  occasionContext: { type: String, trim: true },
  additionalNotes: { type: String, trim: true },

  // AI-generated outputs
  content: {
    type: String,
    default: '',
    trim: true,
  },
  structure: {
    type: Schema.Types.Mixed,
    default: undefined,
  },
  speakerNotes: {
    type: Schema.Types.Mixed,
    default: undefined,
  },
  stageInstructions: {
    type: Schema.Types.Mixed,
    default: undefined,
  },
  confidenceTips: {
    type: Schema.Types.Mixed,
    default: undefined,
  },
  specialOutputs: {
    type: Schema.Types.Mixed,
    default: undefined,
  },

  // Multi-language translations
  translations: [{
    language: { type: String },
    content: { type: String, default: '' },
    structure: { type: Schema.Types.Mixed, default: undefined },
    speakerNotes: { type: Schema.Types.Mixed, default: undefined },
    stageInstructions: { type: Schema.Types.Mixed, default: undefined },
    confidenceTips: { type: Schema.Types.Mixed, default: undefined },
    specialOutputs: { type: Schema.Types.Mixed, default: undefined },
  }],

  // Metadata
  source: {
    type: String,
    enum: ['ai-generation', 'manual'],
    default: 'manual',
  },
  status: {
    type: String,
    enum: ['draft', 'review', 'approved', 'published', 'archived'],
    default: 'draft',
  },
  version: {
    type: Number,
    default: 1,
  },

  // Linked data references
  linkedData: {
    founderId: { type: String },
    employeeId: { type: String },
    productId: { type: String },
  },
}, {
  timestamps: true,
});

// ============================================
// INDEXES
// ============================================

SpeakingEngagementSchema.index({ companyId: 1, speechType: 1 });
SpeakingEngagementSchema.index({ companyId: 1, status: 1 });
SpeakingEngagementSchema.index({ companyId: 1, speakerType: 1 });

// ============================================
// EXPORT
// ============================================

export const SpeakingEngagement = mongoose.models.SpeakingEngagement as mongoose.Model<ISpeakingEngagement>
  || mongoose.model<ISpeakingEngagement>('SpeakingEngagement', SpeakingEngagementSchema);