/**
 * Expert Column Model
 * Manages expert column articles for the PR Content Engine
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type ExpertColumnFormat = 'article' | 'qa' | 'thought-leadership' | 'industry-insights';
export type ContentTone = 'professional' | 'educational' | 'authoritative' | 'conversational';
export type ContentWordCount = 500 | 800 | 1000 | 1500;

export interface IExpertColumn extends Document {
  companyId: string;
  title: string;
  weekNumber: number;
  format: ExpertColumnFormat;
  tone: ContentTone;
  /** 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;
  wordCount: ContentWordCount;
  headline: string;
  topic?: string;
  description?: string;
  planName?: string;
  numberOfWeeks?: number;
  introduction: string;
  mainContent: string;
  conclusion: string;
  body?: string;
  authorBio: string;
  mediaSubmissionNotes: string;
  heroImage?: string;
  status: 'draft' | 'generated' | 'reviewed' | 'published';
  tags?: string[];
  publishedDate?: string;
  version?: number;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const ExpertColumnSchema = new Schema<IExpertColumn>({
  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']
  },
  weekNumber: {
    type: Number,
    required: [true, 'Week number is required'],
    min: [1, 'Week number must be between 1 and 52'],
    max: [52, 'Week number must be between 1 and 52']
  },
  format: {
    type: String,
    enum: ['article', 'qa', 'thought-leadership', 'industry-insights'],
    default: 'article'
  },
  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' },
  wordCount: {
    type: Number,
    enum: [500, 800, 1000, 1500],
    default: 800
  },
  headline: { type: String, trim: true, default: '' },
  topic: { type: String, trim: true, default: '' },
  description: { type: String, default: '' },
  planName: { type: String, trim: true, default: '' },
  numberOfWeeks: { type: Number, min: 1, max: 52 },
  introduction: { type: String, default: '' },
  mainContent: { type: String, default: '' },
  conclusion: { type: String, default: '' },
  body: { type: String, default: '' },
  authorBio: { type: String, default: '' },
  mediaSubmissionNotes: { type: String, default: '' },
  heroImage: { type: String, trim: true },
  status: {
    type: String,
    enum: ['draft', 'generated', 'reviewed', 'published'],
    default: 'draft'
  },
  tags: [{ type: String, trim: true }],
  publishedDate: { type: String },
  version: { type: Number, default: 1 },
}, {
  timestamps: true
});

// ============================================
// INDEXES
// ============================================

ExpertColumnSchema.index({ companyId: 1, status: 1 });
ExpertColumnSchema.index({ companyId: 1, weekNumber: 1 });
ExpertColumnSchema.index({ companyId: 1, format: 1 });

// ============================================
// EXPORT
// ============================================

export const ExpertColumn = mongoose.model<IExpertColumn>('ExpertColumn', ExpertColumnSchema);