/**
 * Press Release Model
 * Manages press releases for the PR Content Engine
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type PressReleaseEventType =
  | 'product-launch'
  | 'company-announcement'
  | 'business-milestone'
  | 'award'
  | 'event'
  | 'partnership'
  | 'expansion'
  | 'achievement';

export type PressReleaseWordCount = 500 | 800 | 1000 | 1200;

export interface IPressRelease extends Document {
  companyId: string;
  title: string;
  eventType: PressReleaseEventType;
  keyAnnouncement?: string;
  customNotes?: string;
  wordCount: PressReleaseWordCount;
  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;
  headline: string;
  subheadline: string;
  introduction?: string;
  mainContent?: string;
  conclusion?: string;
  /**
   * The story broken into titled sub-stories — what the Newspaper template lays
   * out as its secondary story band. Without this the generator's output is one
   * continuous essay and the front page collapses to a single column.
   */
  sections?: { sectionTitle?: string; content?: string }[];
  /** Short quotes set as newspaper pull quotes. */
  pullQuotes?: { text?: string; attribution?: string }[];
  body: string;
  founderQuotes: string;
  companyBackground: string;
  boilerplate: string;
  callToAction: string;
  heroImage?: string;
  status: 'draft' | 'generated' | 'reviewed' | 'published';
  publishedDate?: string;
  tags?: string[];
  version?: number;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const PressReleaseSchema = new Schema<IPressRelease>({
  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-announcement', 'business-milestone', 'award', 'event', 'partnership', 'expansion', 'achievement'],
    default: 'company-announcement'
  },
  keyAnnouncement: {
    type: String,
    default: '',
    trim: true
  },
  customNotes: { type: String, default: '' },
  wordCount: {
    type: Number,
    enum: [500, 800, 1000, 1200],
    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' },
  headline: { type: String, trim: true, default: '' },
  subheadline: { type: String, trim: true, default: '' },
  introduction: { type: String, default: '' },
  mainContent: { type: String, default: '' },
  conclusion: { type: String, default: '' },
  // `_id: false` — these are plain value objects, not separately addressable
  // documents, and an injected _id would leak into the rendered output.
  sections: {
    type: [new Schema({
      sectionTitle: { type: String, default: '' },
      content: { type: String, default: '' },
    }, { _id: false })],
    default: [],
  },
  pullQuotes: {
    type: [new Schema({
      text: { type: String, default: '' },
      attribution: { type: String, default: '' },
    }, { _id: false })],
    default: [],
  },
  body: { type: String, default: '' },
  founderQuotes: { type: String, default: '' },
  companyBackground: { type: String, default: '' },
  boilerplate: { type: String, default: '' },
  callToAction: { type: String, default: '' },
  heroImage: { type: String, trim: true },
  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
// ============================================

PressReleaseSchema.index({ companyId: 1, status: 1 });
PressReleaseSchema.index({ companyId: 1, eventType: 1 });

// ============================================
// EXPORT
// ============================================

export const PressRelease = mongoose.model<IPressRelease>('PressRelease', PressReleaseSchema);