/**
 * Stationery Model
 *
 * Business cards, letterheads, email signatures, and other stationery items.
 */

import mongoose, { Schema, Document } from 'mongoose';

// Core Stationery (Must Have)
export type CoreStationery =
  | 'business-card'
  | 'letterhead'
  | 'envelope-a4'
  | 'envelope-dl'
  | 'email-signature'
  | 'presentation-template';

// Office Use Assets
export type OfficeAssets =
  | 'invoice-template'
  | 'quotation-template'
  | 'receipt-design'
  | 'purchase-order'
  | 'billing-format'
  | 'proposal-template';

// Packaging Stationery
export type PackagingStationery =
  | 'thank-you-card'
  | 'warranty-card'
  | 'instruction-manual'
  | 'product-insert-card'
  | 'branded-stickers'
  | 'packaging-tape';

// Print Stationery
export type PrintStationery =
  | 'stamps'
  | 'branding-print'
  | 'standees-print'
  | 'booth-designs'
  | 't-shirts'
  | 'notebook'
  | 'coffee-mug'
  | 'tote-bag';

// Marketing Assets
export type MarketingAssets =
  | 'newsletter-template'
  | 'brochure-pdf'
  | 'pitch-deck'
  | 'tagline'
  | 'hook-style'
  | 'standees-marketing'
  | 'marketing-collateral';

// Legacy/Other types
export type OtherStationery =
  | 'envelope'
  | 'memo-pad'
  | 'folder'
  | 'compliment-slip'
  | 'other';

// Combined Stationery Type
export type StationeryType =
  | CoreStationery
  | OfficeAssets
  | PackagingStationery
  | PrintStationery
  | MarketingAssets
  | OtherStationery;

// Canonical list of stationery type values (single source of truth — reused by
// StationeryTemplate so the two models never drift).
export const STATIONERY_TYPE_VALUES: string[] = [
  // Core Stationery (Must Have)
  'business-card',
  'letterhead',
  'envelope-a4',
  'envelope-dl',
  'email-signature',
  'presentation-template',
  // Office Use Assets
  'invoice-template',
  'quotation-template',
  'receipt-design',
  'purchase-order',
  'billing-format',
  'proposal-template',
  // Packaging Stationery
  'thank-you-card',
  'warranty-card',
  'instruction-manual',
  'product-insert-card',
  'branded-stickers',
  'packaging-tape',
  // Print Stationery
  'stamps',
  'branding-print',
  'standees-print',
  'booth-designs',
  't-shirts',
  'notebook',
  'coffee-mug',
  'tote-bag',
  // Marketing Assets
  'newsletter-template',
  'brochure-pdf',
  'pitch-deck',
  'tagline',
  'hook-style',
  'standees-marketing',
  'marketing-collateral',
  // Legacy/Other
  'envelope',
  'memo-pad',
  'folder',
  'compliment-slip',
  'other',
];

export type StationeryStatus = 'draft' | 'approved' | 'archived';

export interface IStationery extends Document {
  companyId: string;
  name: string;
  type: StationeryType;
  description?: string;
  templateUrl?: string; // URL to the template file or base64
  previewImageUrl?: string; // URL to preview image or base64
  sourceUrl?: string; // Canva, Figma, design file URL
  base64Data?: string; // Uploaded file as base64
  fileName?: string;
  fileSize?: number;
  fileType?: string;
  dimensions?: {
    width: number;
    height: number;
    unit: 'mm' | 'in' | 'px';
  };
  status: StationeryStatus;
  approvedBy?: string;
  approvedAt?: Date;
  tags: string[];
  // Linkage to founders and employees
  founderId?: string;
  employeeId?: string;
  // ── Guided workflow provenance ──
  templateId?: string;
  backTemplateId?: string;
  kind?: 'ai' | 'manual';
  renderedImageUrl?: string;
  populatedHtml?: string;
  bleedMm?: number;
  marginMm?: number;
  exportFormats?: string[];
  createdAt: Date;
  updatedAt: Date;
}

const StationerySchema = new Schema<IStationery>(
  {
    companyId: {
      type: String,
      required: [true, 'Company ID is required'],
      index: true,
    },
    name: {
      type: String,
      required: [true, 'Stationery name is required'],
      trim: true,
      maxlength: [100, 'Name cannot exceed 100 characters'],
    },
    type: {
      type: String,
      required: [true, 'Stationery type is required'],
      enum: STATIONERY_TYPE_VALUES,
    },
    description: {
      type: String,
      trim: true,
      maxlength: [500, 'Description cannot exceed 500 characters'],
    },
    templateUrl: String,
    previewImageUrl: String,
    sourceUrl: String, // Canva, Figma design file URL
    base64Data: String, // For uploaded files
    fileName: String,
    fileSize: Number,
    fileType: String,
    dimensions: {
      width: Number,
      height: Number,
      unit: { type: String, enum: ['mm', 'in', 'px'] },
    },
    status: {
      type: String,
      enum: ['draft', 'approved', 'archived'],
      default: 'draft',
    },
    approvedBy: String,
    approvedAt: Date,
    tags: [String],
    // Linkage to founders and employees
    founderId: String,
    employeeId: String,
    // ── Guided workflow provenance (non-breaking additions) ──
    // Link to the StationeryTemplate that produced this asset (if any).
    templateId: { type: String, index: true },
    backTemplateId: String,
    // 'ai' = produced from an AI HTML template; 'manual' = overlaid on a manual image template.
    kind: { type: String, enum: ['ai', 'manual'] },
    // The final composed/rendered stationery image (distinct from the template's own image).
    renderedImageUrl: String,
    // AI-only internal snapshot of the populated HTML (never surfaced in the UI).
    populatedHtml: String,
    // Export print settings (millimetres).
    bleedMm: Number,
    marginMm: Number,
    // Which export formats were produced.
    exportFormats: [String],
  },
  {
    timestamps: true,
  }
);

StationerySchema.index({ companyId: 1, type: 1 });
StationerySchema.index({ companyId: 1, status: 1 });
StationerySchema.index({ companyId: 1, founderId: 1 });
StationerySchema.index({ companyId: 1, employeeId: 1 });
StationerySchema.index({ companyId: 1, templateId: 1 });

export const Stationery = mongoose.model<IStationery>('Stationery', StationerySchema);
