/**
 * News Story Model
 * Manages news stories for the PR Content Studio
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type NewsStoryEventType =
  | 'product-launch'
  | 'company-growth'
  | 'award'
  | 'milestone'
  | 'event-announcement'
  | 'partnership'
  | 'funding'
  | 'leadership-change';

export type NewsStoryStatus = 'draft' | 'generated' | 'reviewed' | 'published';

export interface INewsStory extends Document {
  companyId: string;
  title: string;
  eventType: NewsStoryEventType;
  headline: string;
  newsSummary: string;
  articleBody: string;
  mediaQuote: string;
  founderQuote: string;
  callToAction: string;
  heroImage?: string;
  keyFacts?: string[];
  wordCount: number;
  tone: string;
  /** Every predefined tone selected at generation time (superset of `tone`). */
  tones?: string[];
  /** Free-text tone supplied via the "Custom" tone option. */
  customTone?: string;
  language?: string;
  status: NewsStoryStatus;
  publishedDate?: string;
  tags?: string[];
  version?: number;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const NewsStorySchema = new Schema<INewsStory>({
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true
  },
  title: {
    type: String,
    required: [true, 'Title is required'],
    trim: true,
    maxlength: [500, 'Title cannot exceed 500 characters']
  },
  eventType: {
    type: String,
    enum: ['product-launch', 'company-growth', 'award', 'milestone', 'event-announcement', 'partnership', 'funding', 'leadership-change'],
    default: 'company-growth'
  },
  headline: { type: String, trim: true, default: '' },
  newsSummary: { type: String, default: '' },
  articleBody: { type: String, default: '' },
  mediaQuote: { type: String, default: '' },
  founderQuote: { type: String, default: '' },
  callToAction: { type: String, default: '' },
  heroImage: { type: String, trim: true },
  keyFacts: [{ type: String, trim: true }],
  wordCount: { type: Number, default: 800 },
  tone: {
    type: String,
    enum: ['professional', 'educational', 'authoritative', 'conversational'],
    default: 'professional'
  },
  // Multi-tone selection. `tone` above stays the single enum value for
  // backwards compatibility; these carry the full selection.
  tones: { type: [String], default: undefined },
  customTone: { type: String, trim: true },

  language: { type: String, default: 'en' },
  status: {
    type: String,
    enum: ['draft', 'generated', 'reviewed', 'published'],
    default: 'draft'
  },
  publishedDate: { type: String },
  tags: [{ type: String, trim: true }],
  version: { type: Number, default: 1 },
}, {
  timestamps: true
});

// ============================================
// INDEXES
// ============================================

NewsStorySchema.index({ companyId: 1, status: 1 });
NewsStorySchema.index({ companyId: 1, eventType: 1 });

// ============================================
// EXPORT
// ============================================

export const NewsStory = mongoose.model<INewsStory>('NewsStory', NewsStorySchema);