/**
 * Magazine & Sponsorship Models
 *
 * 6 Mongoose models for the Publishing module group:
 * - MagazineArticle
 * - MagazineAd
 * - MagazineIssue
 * - SponsorshipOption
 * - SponsorshipDeal
 * - SponsorshipAsset
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// MAGAZINE ARTICLE
// ============================================

export type MagazineArticleCategory = 'feature' | 'technology' | 'interview' | 'opinion' | 'news' | 'special-report';
export type MagazineArticleStatus = 'draft' | 'in-review' | 'approved' | 'published' | 'archived';

export interface IMagazineArticle extends Document {
  title: string;
  companyId: string;
  subtitle?: string;
  content?: string;
  excerpt?: string;
  author?: string;
  authorBio?: string;
  category: MagazineArticleCategory;
  tags?: string[];
  status: MagazineArticleStatus;
  issueId?: string;
  coverImage?: string;
  images?: string[];
  seoKeywords?: string[];
  seoMetaDescription?: string;
  wordCount?: number;
  readingTime?: string;
  publicationDate?: Date;
  aiGenerated?: boolean;
  aiPrompt?: string;
  createdBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

const MagazineArticleSchema = new Schema<IMagazineArticle>({
  title: {
    type: String,
    required: [true, 'Article title is required'],
    trim: true,
    maxlength: [300, 'Title cannot exceed 300 characters'],
  },
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true,
  },
  subtitle: { type: String, trim: true, maxlength: 500 },
  content: { type: String },
  excerpt: { type: String, maxlength: 1000 },
  author: { type: String, trim: true, maxlength: 200 },
  authorBio: { type: String, maxlength: 1000 },
  category: {
    type: String,
    enum: ['feature', 'technology', 'interview', 'opinion', 'news', 'special-report'],
    default: 'feature',
  },
  tags: [String],
  status: {
    type: String,
    enum: ['draft', 'in-review', 'approved', 'published', 'archived'],
    default: 'draft',
  },
  issueId: { type: String, index: true },
  coverImage: { type: String },
  images: [String],
  seoKeywords: [String],
  seoMetaDescription: { type: String, maxlength: 500 },
  wordCount: { type: Number, min: 0 },
  readingTime: { type: String },
  publicationDate: { type: Date },
  aiGenerated: { type: Boolean, default: false },
  aiPrompt: { type: String, trim: true },
  createdBy: { type: String },
}, { timestamps: true });

MagazineArticleSchema.index({ companyId: 1, status: 1 });
MagazineArticleSchema.index({ companyId: 1, category: 1 });

export const MagazineArticle = mongoose.model<IMagazineArticle>('MagazineArticle', MagazineArticleSchema);

// ============================================
// MAGAZINE AD
// ============================================

export type MagazineAdType = 'full-page' | 'half-page' | 'quarter-page' | 'back-cover' | 'inside-cover' | 'spread' | 'digital-banner';
export type MagazineAdPlacement = 'front' | 'back' | 'center' | 'margin';
export type MagazineAdStatus = 'draft' | 'confirmed' | 'active' | 'completed' | 'cancelled';

export interface IMagazineAd extends Document {
  title: string;
  companyId: string;
  advertiserName: string;
  advertiserContact?: string;
  advertiserEmail?: string;
  advertiserPhone?: string;
  adType: MagazineAdType;
  issueId?: string;
  placement?: MagazineAdPlacement;
  dimensions?: string;
  artworkUrl?: string;
  startDate?: Date;
  endDate?: Date;
  price?: number;
  currency?: string;
  status: MagazineAdStatus;
  clickThroughUrl?: string;
  notes?: string;
  aiPrompt?: string;
  createdBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

const MagazineAdSchema = new Schema<IMagazineAd>({
  title: {
    type: String,
    required: [true, 'Ad title is required'],
    trim: true,
    maxlength: [300, 'Title cannot exceed 300 characters'],
  },
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true,
  },
  advertiserName: {
    type: String,
    required: [true, 'Advertiser name is required'],
    trim: true,
    maxlength: 200,
  },
  advertiserContact: { type: String, trim: true },
  advertiserEmail: { type: String, trim: true },
  advertiserPhone: { type: String, trim: true },
  adType: {
    type: String,
    enum: ['full-page', 'half-page', 'quarter-page', 'back-cover', 'inside-cover', 'spread', 'digital-banner'],
    default: 'full-page',
  },
  issueId: { type: String, index: true },
  placement: {
    type: String,
    enum: ['front', 'back', 'center', 'margin'],
  },
  dimensions: { type: String, trim: true },
  artworkUrl: { type: String },
  startDate: { type: Date },
  endDate: { type: Date },
  price: { type: Number, min: 0 },
  currency: {
    type: String,
    enum: ['INR', 'USD', 'EUR', 'GBP', 'AED'],
    default: 'INR',
  },
  status: {
    type: String,
    enum: ['draft', 'confirmed', 'active', 'completed', 'cancelled'],
    default: 'draft',
  },
  clickThroughUrl: { type: String },
  notes: { type: String, maxlength: 2000 },
  aiPrompt: { type: String, trim: true },
  createdBy: { type: String },
}, { timestamps: true });

MagazineAdSchema.index({ companyId: 1, status: 1 });
MagazineAdSchema.index({ companyId: 1, issueId: 1 });

export const MagazineAd = mongoose.model<IMagazineAd>('MagazineAd', MagazineAdSchema);

// ============================================
// MAGAZINE ISSUE
// ============================================

export type MagazineIssueStatus = 'planning' | 'in-design' | 'in-review' | 'published' | 'archived';

export interface IMagazineIssue extends Document {
  title: string;
  companyId: string;
  issueNumber: string;
  volumeNumber?: string;
  publicationDate?: Date;
  coverImage?: string;
  description?: string;
  theme?: string;
  editorialNote?: string;
  status: MagazineIssueStatus;
  articleIds?: string[];
  adIds?: string[];
  pageCount?: number;
  pdfUrl?: string;
  printRun?: number;
  digitalUrl?: string;
  distributionChannels?: string[];
  aiPrompt?: string;
  createdBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

const MagazineIssueSchema = new Schema<IMagazineIssue>({
  title: {
    type: String,
    required: [true, 'Issue title is required'],
    trim: true,
    maxlength: [300, 'Title cannot exceed 300 characters'],
  },
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true,
  },
  issueNumber: {
    type: String,
    required: [true, 'Issue number is required'],
    trim: true,
  },
  volumeNumber: { type: String, trim: true },
  publicationDate: { type: Date },
  coverImage: { type: String },
  description: { type: String, maxlength: 2000 },
  theme: { type: String, trim: true, maxlength: 200 },
  editorialNote: { type: String, maxlength: 5000 },
  status: {
    type: String,
    enum: ['planning', 'in-design', 'in-review', 'published', 'archived'],
    default: 'planning',
  },
  articleIds: [String],
  adIds: [String],
  pageCount: { type: Number, min: 0 },
  pdfUrl: { type: String },
  printRun: { type: Number, min: 0 },
  digitalUrl: { type: String },
  distributionChannels: [String],
  aiPrompt: { type: String, trim: true },
  createdBy: { type: String },
}, { timestamps: true });

MagazineIssueSchema.index({ companyId: 1, status: 1 });
MagazineIssueSchema.index({ companyId: 1, issueNumber: 1 });

export const MagazineIssue = mongoose.model<IMagazineIssue>('MagazineIssue', MagazineIssueSchema);

// ============================================
// SPONSORSHIP OPTION
// ============================================

export type SponsorshipTier = 'platinum' | 'gold' | 'silver' | 'bronze' | 'custom';
export type SponsorshipOptionStatus = 'active' | 'inactive' | 'sold-out';

export interface ISponsorshipOption extends Document {
  name: string;
  companyId: string;
  description?: string;
  tier: SponsorshipTier;
  price?: number;
  currency?: string;
  benefits?: string[];
  includes?: string[];
  availability?: string;
  maxSponsors?: number;
  currentSponsors?: number;
  status: SponsorshipOptionStatus;
  eventEdition?: string;
  digitalBenefits?: string[];
  aiPrompt?: string;
  createdBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

const SponsorshipOptionSchema = new Schema<ISponsorshipOption>({
  name: {
    type: String,
    required: [true, 'Option name is required'],
    trim: true,
    maxlength: [200, 'Name cannot exceed 200 characters'],
  },
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true,
  },
  description: { type: String, maxlength: 2000 },
  tier: {
    type: String,
    enum: ['platinum', 'gold', 'silver', 'bronze', 'custom'],
    default: 'silver',
  },
  price: { type: Number, min: 0 },
  currency: {
    type: String,
    enum: ['INR', 'USD', 'EUR', 'GBP', 'AED'],
    default: 'INR',
  },
  benefits: [String],
  includes: [String],
  availability: { type: String, trim: true },
  maxSponsors: { type: Number, min: 0 },
  currentSponsors: { type: Number, min: 0, default: 0 },
  status: {
    type: String,
    enum: ['active', 'inactive', 'sold-out'],
    default: 'active',
  },
  eventEdition: { type: String, trim: true, maxlength: 200 },
  digitalBenefits: [String],
  aiPrompt: { type: String, trim: true },
  createdBy: { type: String },
}, { timestamps: true });

SponsorshipOptionSchema.index({ companyId: 1, status: 1 });
SponsorshipOptionSchema.index({ companyId: 1, tier: 1 });

export const SponsorshipOption = mongoose.model<ISponsorshipOption>('SponsorshipOption', SponsorshipOptionSchema);

// ============================================
// SPONSORSHIP DEAL
// ============================================

export type SponsorshipDealStatus = 'negotiation' | 'pending' | 'active' | 'completed' | 'cancelled' | 'expired';
export type PaymentStatus = 'unpaid' | 'partial' | 'paid' | 'overdue';

export interface ISponsorshipDeal extends Document {
  sponsorName: string;
  companyId: string;
  sponsorContact?: string;
  sponsorEmail?: string;
  sponsorPhone?: string;
  sponsorLogo?: string;
  optionId?: string;
  dealValue?: number;
  currency?: string;
  startDate?: Date;
  endDate?: Date;
  status: SponsorshipDealStatus;
  contractUrl?: string;
  invoiceNumber?: string;
  paymentStatus?: PaymentStatus;
  notes?: string;
  aiPrompt?: string;
  createdBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

const SponsorshipDealSchema = new Schema<ISponsorshipDeal>({
  sponsorName: {
    type: String,
    required: [true, 'Sponsor name is required'],
    trim: true,
    maxlength: 200,
  },
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true,
  },
  sponsorContact: { type: String, trim: true },
  sponsorEmail: { type: String, trim: true },
  sponsorPhone: { type: String, trim: true },
  sponsorLogo: { type: String },
  optionId: { type: String, index: true },
  dealValue: { type: Number, min: 0 },
  currency: {
    type: String,
    enum: ['INR', 'USD', 'EUR', 'GBP', 'AED'],
    default: 'INR',
  },
  startDate: { type: Date },
  endDate: { type: Date },
  status: {
    type: String,
    enum: ['negotiation', 'pending', 'active', 'completed', 'cancelled', 'expired'],
    default: 'negotiation',
  },
  contractUrl: { type: String },
  invoiceNumber: { type: String, trim: true },
  paymentStatus: {
    type: String,
    enum: ['unpaid', 'partial', 'paid', 'overdue'],
    default: 'unpaid',
  },
  notes: { type: String, maxlength: 2000 },
  aiPrompt: { type: String, trim: true },
  createdBy: { type: String },
}, { timestamps: true });

SponsorshipDealSchema.index({ companyId: 1, status: 1 });
SponsorshipDealSchema.index({ companyId: 1, optionId: 1 });

export const SponsorshipDeal = mongoose.model<ISponsorshipDeal>('SponsorshipDeal', SponsorshipDealSchema);

// ============================================
// SPONSORSHIP ASSET
// ============================================

export type SponsorshipAssetType = 'logo' | 'banner' | 'advertorial' | 'dedicated-email' | 'social-post' | 'print-ad' | 'booth-signage';
export type SponsorshipAssetFormat = 'print' | 'digital' | 'both';
export type SponsorshipAssetStatus = 'pending' | 'approved' | 'rejected' | 'active' | 'expired';

export interface ISponsorshipAsset extends Document {
  name: string;
  companyId: string;
  description?: string;
  assetType: SponsorshipAssetType;
  format: SponsorshipAssetFormat;
  dimensions?: string;
  fileUrl?: string;
  thumbnailUrl?: string;
  sponsorId?: string;
  dealId?: string;
  issueId?: string;
  status: SponsorshipAssetStatus;
  expiryDate?: Date;
  guidelines?: string;
  notes?: string;
  aiPrompt?: string;
  createdBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

const SponsorshipAssetSchema = new Schema<ISponsorshipAsset>({
  name: {
    type: String,
    required: [true, 'Asset name is required'],
    trim: true,
    maxlength: [200, 'Name cannot exceed 200 characters'],
  },
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true,
  },
  description: { type: String, maxlength: 2000 },
  assetType: {
    type: String,
    enum: ['logo', 'banner', 'advertorial', 'dedicated-email', 'social-post', 'print-ad', 'booth-signage'],
    default: 'banner',
  },
  format: {
    type: String,
    enum: ['print', 'digital', 'both'],
    default: 'digital',
  },
  dimensions: { type: String, trim: true },
  fileUrl: { type: String },
  thumbnailUrl: { type: String },
  sponsorId: { type: String },
  dealId: { type: String, index: true },
  issueId: { type: String, index: true },
  status: {
    type: String,
    enum: ['pending', 'approved', 'rejected', 'active', 'expired'],
    default: 'pending',
  },
  expiryDate: { type: Date },
  guidelines: { type: String, maxlength: 3000 },
  notes: { type: String, maxlength: 2000 },
  aiPrompt: { type: String, trim: true },
  createdBy: { type: String },
}, { timestamps: true });

SponsorshipAssetSchema.index({ companyId: 1, status: 1 });
SponsorshipAssetSchema.index({ companyId: 1, assetType: 1 });

export const SponsorshipAsset = mongoose.model<ISponsorshipAsset>('SponsorshipAsset', SponsorshipAssetSchema);