/**
 * PR Headline Option Model
 * Manages headline options generated by the Headline Laboratory
 * Can store a single headline (legacy) or a group of headlines
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type HeadlineCategory = 'corporate' | 'news' | 'viral' | 'authority' | 'media-friendly';

export interface IHeadlineItem {
  headline: string;
  category?: HeadlineCategory;
  score?: number;
  reasoning?: string;
  isSelected?: boolean;
}

export interface IPRHeadlineOption extends Omit<Document, 'isSelected'> {
  companyId: string;
  contentId?: string;
  contentType?: string;
  headline?: string;
  category?: HeadlineCategory;
  score?: number;
  reasoning?: string;
  isSelected?: boolean;
  title?: string;
  status?: string;
  // Array of headlines for a headline group (when multiple headlines are generated together)
  headlines?: IHeadlineItem[];
  language?: string;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const HeadlineItemSchema = new Schema<IHeadlineItem>({
  headline: {
    type: String,
    required: true,
    trim: true,
    maxlength: [300, 'Headline cannot exceed 300 characters']
  },
  category: {
    type: String,
    enum: ['corporate', 'news', 'viral', 'authority', 'media-friendly'],
    default: 'news'
  },
  score: {
    type: Number,
    default: 0,
    min: 0,
    max: 100
  },
  reasoning: { type: String, default: '' },
  isSelected: { type: Boolean, default: false },
}, { _id: false });

const PRHeadlineOptionSchema = new Schema<IPRHeadlineOption>({
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true
  },
  contentId: {
    type: String,
    index: true
  },
  contentType: {
    type: String,
    enum: ['press-release', 'expert-column', 'news-story', 'thought-leadership'],
  },
  headline: {
    type: String,
    trim: true,
    maxlength: [300, 'Headline cannot exceed 300 characters']
  },
  category: {
    type: String,
    enum: ['corporate', 'news', 'viral', 'authority', 'media-friendly'],
    default: 'news'
  },
  score: {
    type: Number,
    default: 0,
    min: 0,
    max: 100
  },
  reasoning: { type: String, default: '' },
  isSelected: { type: Boolean, default: false },
  title: { type: String, default: 'PR Headlines' },
  status: { type: String, default: 'generated' },
  // Array of headlines for headline groups
  headlines: [HeadlineItemSchema],
  language: { type: String, default: 'en' },
}, {
  timestamps: true
});

// ============================================
// INDEXES
// ============================================

PRHeadlineOptionSchema.index({ companyId: 1, contentId: 1 });
PRHeadlineOptionSchema.index({ companyId: 1, contentType: 1 });

// ============================================
// EXPORT
// ============================================

export const PRHeadlineOption = mongoose.model<IPRHeadlineOption>('PRHeadlineOption', PRHeadlineOptionSchema);