/**
 * Thought Leadership Model
 * Manages thought leadership content for the PR Content Studio
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type ThoughtLeadershipType =
  | 'industry-opinion'
  | 'market-trends'
  | 'predictions'
  | 'business-insights'
  | 'founder-perspective'
  | 'innovation-story';

export interface IThoughtLeadership extends Document {
  companyId: string;
  title: string;
  contentType: ThoughtLeadershipType;
  headline: string;
  introduction: string;
  mainContent: string;
  conclusion: string;
  heroImage?: string;
  keyTakeaways?: 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: 'draft' | 'generated' | 'reviewed' | 'published';
  publishedDate?: string;
  tags?: string[];
  version?: number;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const ThoughtLeadershipSchema = new Schema<IThoughtLeadership>({
  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']
  },
  contentType: {
    type: String,
    enum: ['industry-opinion', 'market-trends', 'predictions', 'business-insights', 'founder-perspective', 'innovation-story'],
    default: 'industry-opinion'
  },
  headline: { type: String, trim: true, default: '' },
  introduction: { type: String, default: '' },
  mainContent: { type: String, default: '' },
  conclusion: { type: String, default: '' },
  heroImage: { type: String, trim: true },
  keyTakeaways: [{ type: String, trim: true }],
  wordCount: { type: Number, default: 1000 },
  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
// ============================================

ThoughtLeadershipSchema.index({ companyId: 1, status: 1 });
ThoughtLeadershipSchema.index({ companyId: 1, contentType: 1 });

// ============================================
// EXPORT
// ============================================

export const ThoughtLeadership = mongoose.model<IThoughtLeadership>('ThoughtLeadership', ThoughtLeadershipSchema);