/**
 * Audio Content Model
 * AI-powered audio content with song lyrics, Suno integration,
 * and music management.
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type AudioGenre =
  | 'pop'
  | 'rock'
  | 'hip-hop'
  | 'r&b'
  | 'jazz'
  | 'classical'
  | 'electronic'
  | 'country'
  | 'folk'
  | 'latin'
  | 'reggae'
  | 'blues'
  | 'metal'
  | 'punk'
  | 'soul'
  | 'funk'
  | 'disco'
  | 'ambient'
  | 'indie'
  | 'alternative'
  | 'other';

export type AudioMood =
  | 'happy'
  | 'sad'
  | 'energetic'
  | 'calm'
  | 'romantic'
  | 'inspiring'
  | 'melancholic'
  | 'aggressive'
  | 'relaxed'
  | 'uplifting'
  | 'dark'
  | 'nostalgic'
  | 'dreamy'
  | 'epic'
  | 'playful'
  | 'mysterious'
  | 'hopeful'
  | 'other';

export type AudioSingerType = 'male' | 'female' | 'duet' | 'choir' | 'instrumental' | 'other';

export type AudioLanguage = 'en' | 'es' | 'fr' | 'de' | 'it' | 'pt' | 'ja' | 'ko' | 'zh' | 'hi' | 'ar' | 'other';

export type AudioContentStatus = 'draft' | 'processing' | 'completed' | 'failed';

export interface IAudioContent extends Document {
  companyId: string;
  songTitle: string;
  prompt: string;
  genre?: AudioGenre;
  mood?: AudioMood;
  language?: AudioLanguage;
  singerType?: AudioSingerType;
  duration?: number;
  instrumentStyle?: string;
  negativePrompt?: string;

  // User-input fields (Step 1 form)
  topic?: string;
  description?: string;
  targetAudience?: string;

  // AI-generated fields (populated after Ollama runs)
  generatedLyrics?: string;
  songConcept?: string;
  generatedChorus?: string;
  generatedVerses?: string[];
  generatedBridge?: string;
  generatedGenreSuggestions?: string[];
  generatedMood?: string;
  generatedMusicTags?: string[];
  optimizedSunoPrompt?: string;

  // Provider integration fields
  providerJobId?: string;
  providerStatus?: AudioContentStatus;
  audioUrl?: string;
  coverImageUrl?: string;
  providerMetadata?: {
    provider?: string;
    model?: string;
    generatedAt?: Date;
    durationSeconds?: number;
    format?: string;
    fileSize?: number;
    [key: string]: any;
  };

  // AI generation context
  aiGenerated?: boolean;
  aiGenerationContext?: {
    pipelineVersion?: string;
    provider?: string;
    model?: string;
    confidence?: number;
    generatedAt?: Date;
  };

  // Metadata
  tags?: string[];
  isFavorite: boolean;
  createdBy?: string;
  status: AudioContentStatus;

  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const AudioContentSchema = new Schema<IAudioContent>(
  {
    companyId: {
      type: String,
      required: [true, 'Company ID is required'],
      index: true,
    },
    songTitle: {
      type: String,
      required: [true, 'Song title is required'],
      trim: true,
      maxlength: 200,
    },
    prompt: {
      type: String,
      required: [true, 'Prompt is required'],
      trim: true,
    },
    genre: {
      type: String,
      enum: [
        'pop', 'rock', 'hip-hop', 'r&b', 'jazz', 'classical',
        'electronic', 'country', 'folk', 'latin', 'reggae', 'blues',
        'metal', 'punk', 'soul', 'funk', 'disco', 'ambient',
        'indie', 'alternative', 'other',
      ],
    },
    mood: {
      type: String,
      enum: [
        'happy', 'sad', 'energetic', 'calm', 'romantic', 'inspiring',
        'melancholic', 'aggressive', 'relaxed', 'uplifting', 'dark', 'nostalgic',
        'dreamy', 'epic', 'playful', 'mysterious', 'hopeful', 'other',
      ],
    },
    language: {
      type: String,
      enum: ['en', 'es', 'fr', 'de', 'it', 'pt', 'ja', 'ko', 'zh', 'hi', 'ar', 'other'],
      default: 'en',
    },
    singerType: {
      type: String,
      enum: ['male', 'female', 'duet', 'choir', 'instrumental', 'other'],
    },
    duration: { type: Number },
    instrumentStyle: { type: String, trim: true },
    negativePrompt: { type: String, trim: true },

    // User-input fields (Step 1 form)
    topic: { type: String, trim: true },
    description: { type: String, trim: true },
    targetAudience: { type: String, trim: true },

    // AI-generated fields
    generatedLyrics: { type: String, trim: true },
    songConcept: { type: String, trim: true },
    generatedChorus: { type: String, trim: true },
    generatedVerses: { type: [String], default: [] },
    generatedBridge: { type: String, trim: true },
    generatedGenreSuggestions: { type: [String], default: [] },
    generatedMood: { type: String, trim: true },
    generatedMusicTags: { type: [String], default: [] },
    optimizedSunoPrompt: { type: String, trim: true },

    // Provider integration fields
    providerJobId: { type: String, trim: true },
    providerStatus: {
      type: String,
      enum: ['processing', 'completed', 'failed'],
    },
    audioUrl: { type: String, trim: true },
    coverImageUrl: { type: String, trim: true },
    providerMetadata: {
      provider: { type: String },
      model: { type: String },
      generatedAt: { type: Date },
      durationSeconds: { type: Number },
      format: { type: String },
      fileSize: { type: Number },
    },
    tags: { type: [String], default: [] },
    isFavorite: { type: Boolean, default: false },
    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },

    // AI generation context
    aiGenerated: { type: Boolean, default: false },
    aiGenerationContext: {
      pipelineVersion: { type: String },
      provider: { type: String },
      model: { type: String },
      confidence: { type: Number },
      generatedAt: { type: Date },
    },

    status: {
      type: String,
      enum: ['draft', 'processing', 'completed', 'failed'],
      default: 'draft',
    },
  },
  { timestamps: true }
);

// Indexes
AudioContentSchema.index({ companyId: 1, status: 1 });
AudioContentSchema.index({ companyId: 1, genre: 1 });
AudioContentSchema.index({ companyId: 1, aiGenerated: 1 });
AudioContentSchema.index({ providerJobId: 1 }, { sparse: true, unique: true });

export const AudioContent =
  mongoose.models.AudioContent ||
  mongoose.model<IAudioContent>('AudioContent', AudioContentSchema);