/**
 * Pitch Deck Model
 *
 * AI-powered pitch deck creation with templates, slide editor, version control, and export.
 */

import mongoose, { Schema, Document, models, Model } from 'mongoose';

// ============================================
// TYPES
// ============================================

export type DeckStatus = 'draft' | 'in-review' | 'approved' | 'published' | 'archived';
export type DeckTemplate = 'investor' | 'sales' | 'product' | 'partner' | 'event' | 'internal';
export type PitchDeckSlideType =
  | 'title'
  | 'problem'
  | 'solution'
  | 'market'
  | 'product'
  | 'business-model'
  | 'traction'
  | 'team'
  | 'financials'
  | 'competition'
  | 'ask'
  | 'timeline'
  | 'custom';
export type PitchDeckSlideLayout = 'single-column' | 'two-column' | 'image-left' | 'image-right' | 'full-image';

export interface IPitchDeckSlide {
  id: string;
  order: number;
  type: PitchDeckSlideType;
  title: string;
  subtitle?: string;
  content?: string;
  notes?: string;
  mediaUrls?: string[];
  chartData?: Record<string, unknown>;
  layout?: PitchDeckSlideLayout;
  aiGenerated?: boolean;
  generatedAt?: Date;
}

export interface IPitchDeckVersion {
  version: number;
  slides: IPitchDeckSlide[];
  savedAt: Date;
  savedBy: string;
}

export interface IPitchDeckLinkedData {
  businessProfileId?: string;
  productIds?: string[];
  icpIds?: string[];
  competitorIds?: string[];
  founderIds?: string[];
  caseStudyIds?: string[];
  financialModelId?: string;
}

export interface IPitchDeck extends Document {
  companyId: string;
  name: string;
  description?: string;
  template: DeckTemplate;
  status: DeckStatus;
  slides: IPitchDeckSlide[];
  linkedData?: IPitchDeckLinkedData;
  version: number;
  previousVersions?: IPitchDeckVersion[];
  exportSettings?: {
    format: 'pdf' | 'ppt' | 'google-slides';
    branding: boolean;
    includeNotes: boolean;
  };
  approvalStatus?: 'pending' | 'approved' | 'rejected';
  approvedBy?: string;
  approvedAt?: Date;
  createdBy: string;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// SCHEMA
// ============================================

const SlideSchema = new Schema<IPitchDeckSlide>(
  {
    id: { type: String, required: true },
    order: { type: Number, required: true },
    type: {
      type: String,
      enum: ['title', 'problem', 'solution', 'market', 'product', 'business-model', 'traction', 'team', 'financials', 'competition', 'ask', 'timeline', 'custom'],
      required: true,
    },
    title: { type: String, required: true },
    subtitle: { type: String },
    content: { type: String },
    notes: { type: String },
    mediaUrls: [{ type: String }],
    chartData: { type: Schema.Types.Mixed },
    layout: {
      type: String,
      enum: ['single-column', 'two-column', 'image-left', 'image-right', 'full-image'],
    },
    aiGenerated: { type: Boolean, default: false },
    generatedAt: { type: Date },
  },
  { _id: false }
);

const VersionSchema = new Schema<IPitchDeckVersion>(
  {
    version: { type: Number, required: true },
    slides: [SlideSchema],
    savedAt: { type: Date, default: Date.now },
    savedBy: { type: String, required: true },
  },
  { _id: false }
);

const LinkedDataSchema = new Schema<IPitchDeckLinkedData>(
  {
    businessProfileId: { type: String },
    productIds: [{ type: String }],
    icpIds: [{ type: String }],
    competitorIds: [{ type: String }],
    founderIds: [{ type: String }],
    caseStudyIds: [{ type: String }],
    financialModelId: { type: String },
  },
  { _id: false }
);

const PitchDeckSchema = new Schema<IPitchDeck>(
  {
    companyId: { type: String, required: true, index: true },
    name: { type: String, required: true, maxlength: 200 },
    description: { type: String, maxlength: 2000 },
    template: {
      type: String,
      enum: ['investor', 'sales', 'product', 'partner', 'event', 'internal'],
      required: true,
      default: 'investor',
    },
    status: {
      type: String,
      enum: ['draft', 'in-review', 'approved', 'published', 'archived'],
      default: 'draft',
    },
    slides: [SlideSchema],
    linkedData: LinkedDataSchema,
    version: { type: Number, default: 1 },
    previousVersions: [VersionSchema],
    exportSettings: {
      format: {
        type: String,
        enum: ['pdf', 'ppt', 'google-slides'],
        default: 'pdf',
      },
      branding: { type: Boolean, default: true },
      includeNotes: { type: Boolean, default: false },
    },
    approvalStatus: {
      type: String,
      enum: ['pending', 'approved', 'rejected'],
    },
    approvedBy: { type: String },
    approvedAt: { type: Date },
    createdBy: { type: String, required: true },
  },
  { timestamps: true }
);

// Indexes
PitchDeckSchema.index({ companyId: 1, status: 1 });
PitchDeckSchema.index({ companyId: 1, template: 1 });
PitchDeckSchema.index({ companyId: 1, createdAt: -1 });

// ============================================
// MODEL
// ============================================

export const PitchDeck: Model<IPitchDeck> =
  models.PitchDeck || mongoose.model<IPitchDeck>('PitchDeck', PitchDeckSchema);

export default PitchDeck;