/**
 * Video Content Model
 * Centralized video content library for storing, organizing, categorizing,
 * and managing all video-related materials.
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type VideoType =
  | 'educational'
  | 'product-demo'
  | 'service-walkthrough'
  | 'sop-video'
  | 'company-policy'
  | 'hr-training'
  | 'technical-tutorial'
  | 'sales-training'
  | 'founder-message'
  | 'customer-onboarding'
  | 'webinar-recording'
  | 'team-training'
  | 'interview'
  | 'feature-update'
  | 'marketing-strategy'
  | 'internal-communication'
  | 'compliance-training'
  | 'support-tutorial';

export type VideoCategory =
  | 'product-training'
  | 'service-training'
  | 'educational'
  | 'company-policies'
  | 'hr-training'
  | 'sop-videos'
  | 'sales-training'
  | 'technical-tutorials'
  | 'customer-support'
  | 'founder-sessions'
  | 'team-onboarding'
  | 'compliance'
  | 'marketing-training'
  | 'crm-training'
  | 'software-tutorials'
  | 'internal-meetings'
  | 'webinar-recordings'
  | 'client-training'
  | 'knowledge-sharing';

export type VideoSource =
  | 'youtube'
  | 'vimeo'
  | 'loom'
  | 'google-drive'
  | 'dropbox'
  | 'wistia'
  | 'internal-cdn'
  | 'other';

export type VideoStatus = 'draft' | 'approved' | 'archived';

export type VideoAccessLevel =
  | 'public'
  | 'team'
  | 'department'
  | 'manager-only'
  | 'hr-only';

export type WatchStatus = 'not-started' | 'in-progress' | 'completed' | 'skipped';

export interface IVideoSection extends Document {
  id: string;
  title: string;
  content: string;
  order: number;
}

export interface ITimestampNote extends Document {
  time: string;
  note: string;
}

export interface IVideoContent extends Document {
  companyId: string;
  name: string;
  description?: string;
  type: VideoType;
  category?: VideoCategory;
  source?: VideoSource;
  videoUrl?: string;
  thumbnailUrl?: string;
  duration?: string;
  transcript?: string;
  summary?: string;
  keyNotes?: string[];
  script?: string;
  shotList?: string[];
  sections?: IVideoSection[];
  timestampNotes?: ITimestampNote[];
  status: VideoStatus;
  accessLevel: VideoAccessLevel;
  watchStatus?: WatchStatus;
  tags?: string[];
  language?: string;
  department?: string;
  productIds?: string[];
  sopIds?: string[];
  linkedData?: {
    personaIds?: string[];
    salesScriptIds?: string[];
    faqIds?: string[];
    testimonialIds?: string[];
    collateralIds?: string[];
  };
  targetAudience?: string;
  usageNotes?: string;
  bestPractices?: string[];
  effectivenessTips?: string[];
  pdfReferences?: string[];
  downloadableResources?: string[];
  isFavorite: boolean;
  isPinned: boolean;
  viewCount?: number;
  downloadCount?: number;

  // AI generation
  aiGenerated?: boolean;
  aiGenerationContext?: {
    pipelineVersion?: string;
    provider?: string;
    model?: string;
    confidence?: number;
    generatedAt?: Date;
    videoPrompt?: string;
    videoGenerationId?: string;
  };

  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// SUB-SCHEMAS
// ============================================

const VideoSectionSchema = new Schema<IVideoSection>(
  {
    id: { type: String, required: true },
    title: { type: String, required: true },
    content: { type: String, default: '' },
    order: { type: Number, default: 0 },
  },
  { _id: false }
);

const TimestampNoteSchema = new Schema<ITimestampNote>(
  {
    time: { type: String, required: true },
    note: { type: String, required: true },
  },
  { _id: false }
);

// ============================================
// MAIN SCHEMA
// ============================================

const VideoContentSchema = new Schema<IVideoContent>(
  {
    companyId: {
      type: String,
      required: [true, 'Company ID is required'],
      index: true,
    },
    name: {
      type: String,
      required: [true, 'Name is required'],
      trim: true,
    },
    description: { type: String, trim: true },
    type: {
      type: String,
      required: [true, 'Type is required'],
      enum: [
        'educational', 'product-demo', 'service-walkthrough', 'sop-video',
        'company-policy', 'hr-training', 'technical-tutorial', 'sales-training',
        'founder-message', 'customer-onboarding', 'webinar-recording', 'team-training',
        'interview', 'feature-update', 'marketing-strategy', 'internal-communication',
        'compliance-training', 'support-tutorial',
      ],
      default: 'educational',
    },
    category: {
      type: String,
      enum: [
        'product-training', 'service-training', 'educational', 'company-policies',
        'hr-training', 'sop-videos', 'sales-training', 'technical-tutorials',
        'customer-support', 'founder-sessions', 'team-onboarding', 'compliance',
        'marketing-training', 'crm-training', 'software-tutorials', 'internal-meetings',
        'webinar-recordings', 'client-training', 'knowledge-sharing',
      ],
    },
    source: {
      type: String,
      enum: ['youtube', 'vimeo', 'loom', 'google-drive', 'dropbox', 'wistia', 'internal-cdn', 'other'],
    },
    videoUrl: { type: String, trim: true },
    thumbnailUrl: { type: String, trim: true },
    duration: { type: String, trim: true },
    transcript: { type: String, trim: true },
    summary: { type: String, trim: true },
    keyNotes: { type: [String], default: [] },
    script: { type: String, trim: true },
    shotList: { type: [String], default: [] },
    sections: { type: [VideoSectionSchema], default: [] },
    timestampNotes: { type: [TimestampNoteSchema], default: [] },
    status: {
      type: String,
      enum: ['draft', 'approved', 'archived'],
      default: 'draft',
    },
    accessLevel: {
      type: String,
      enum: ['public', 'team', 'department', 'manager-only', 'hr-only'],
      default: 'team',
    },
    watchStatus: {
      type: String,
      enum: ['not-started', 'in-progress', 'completed', 'skipped'],
      default: 'not-started',
    },
    tags: { type: [String], default: [] },
    language: { type: String, default: 'en' },
    department: { type: String, trim: true },
    productIds: { type: [String], default: [] },
    sopIds: { type: [String], default: [] },
    linkedData: {
      personaIds: { type: [String], default: [] },
      salesScriptIds: { type: [String], default: [] },
      faqIds: { type: [String], default: [] },
      testimonialIds: { type: [String], default: [] },
      collateralIds: { type: [String], default: [] },
    },
    targetAudience: { type: String, trim: true },
    usageNotes: { type: String, trim: true },
    bestPractices: { type: [String], default: [] },
    effectivenessTips: { type: [String], default: [] },
    pdfReferences: { type: [String], default: [] },
    downloadableResources: { type: [String], default: [] },
    isFavorite: { type: Boolean, default: false },
    isPinned: { type: Boolean, default: false },
    viewCount: { type: Number, default: 0 },
    downloadCount: { type: Number, default: 0 },

    // AI generation
    aiGenerated: { type: Boolean, default: false },
    aiGenerationContext: {
      pipelineVersion: { type: String },
      provider: { type: String },
      model: { type: String },
      confidence: { type: Number },
      generatedAt: { type: Date },
      videoPrompt: { type: String },
      videoGenerationId: { type: String },
    },
  },
  { timestamps: true }
);

// Indexes
VideoContentSchema.index({ companyId: 1, status: 1 });
VideoContentSchema.index({ companyId: 1, type: 1 });
VideoContentSchema.index({ companyId: 1, category: 1 });
VideoContentSchema.index({ companyId: 1, aiGenerated: 1 });

export const VideoContent = mongoose.models.VideoContent || mongoose.model<IVideoContent>('VideoContent', VideoContentSchema);