/**
 * Brand Asset Model
 *
 * Logos, favicons, social media images, and other brand assets.
 */

import mongoose, { Schema, Document } from 'mongoose';

export type AssetType =
  | 'logo'
  | 'secondary-logo'
  | 'wordmark'
  | 'logo-icon'
  | 'logoMarkLight'
  | 'logoMarkDark'
  | 'logoHorizontal'
  | 'logoVertical'
  | 'logoIconOnly'
  | 'favicon'
  | 'social-og'
  | 'social-twitter'
  | 'social-linkedin'
  | 'social-instagram'
  | 'social-facebook'
  | 'social-tiktok'
  | 'social-youtube'
  | 'email-header'
  | 'email-footer'
  | 'email-signature'
  | 'presentation'
  | 'document'
  | 'web-banner'
  | 'app-icon'
  | 'brandPattern'
  | 'backgroundImage'
  | 'backdrop'
  | 'watermark'
  | 'virtual-background'
  | 'clear-space-guidelines'
  | 'minimum-size-guidelines'
  | 'brand-usage-rules'
  | 'dos-and-donts'
  | 'other'
  | 'custom';

export type AssetFormat = 'svg' | 'png' | 'jpg' | 'pdf' | 'webp' | 'ico' | 'gif' | 'content';

export interface IBrandAsset extends Document {
  companyId: string;
  name: string;
  type: AssetType;
  description?: string;
  format: AssetFormat;
  url?: string;
  base64Data?: string;
  /** Structured guidelines content stored as JSON string (for clear-space-guidelines, minimum-size-guidelines, brand-usage-rules, dos-and-donts) */
  contentData?: string;
  fileName?: string;
  fileType?: string;
  sourceUrl?: string;
  source?: string;
  dimensions?: {
    width: number;
    height: number;
  };
  fileSize?: number;
  isPrimary: boolean;
  tags: string[];
  // Linkage to founders and employees
  founderId?: string;
  employeeId?: string;
  createdAt: Date;
  updatedAt: Date;
}

const BrandAssetSchema = new Schema<IBrandAsset>(
  {
    companyId: {
      type: String,
      required: [true, 'Company ID is required'],
      index: true,
    },
    name: {
      type: String,
      required: [true, 'Asset name is required'],
      trim: true,
      maxlength: [100, 'Name cannot exceed 100 characters'],
    },
    type: {
      type: String,
      required: [true, 'Asset type is required'],
      enum: [
        'logo',
        'secondary-logo',
        'wordmark',
        'logo-icon',
        'logoMarkLight',
        'logoMarkDark',
        'logoHorizontal',
        'logoVertical',
        'logoIconOnly',
        'favicon',
        'social-og',
        'social-twitter',
        'social-linkedin',
        'social-instagram',
        'social-facebook',
        'social-tiktok',
        'social-youtube',
        'email-header',
        'email-footer',
        'email-signature',
        'presentation',
        'document',
        'web-banner',
        'app-icon',
        'brandPattern',
        'backgroundImage',
        'backdrop',
        'watermark',
        'virtual-background',
        'pattern',
        'background',
        'print-ready',
        'vector',
        'transparent',
        'dark-version',
        'light-version',
        'clear-space-guidelines',
        'minimum-size-guidelines',
        'brand-usage-rules',
        'dos-and-donts',
        'other',
        'custom',
      ],
    },
    description: String,
    format: {
      type: String,
      required: [true, 'Format is required'],
      enum: ['svg', 'png', 'jpg', 'pdf', 'webp', 'ico', 'gif', 'content'],
    },
    url: String,
    /** @deprecated Legacy — only populated for pre-migration records. New records store files on disk with url pointing to /uploads/brand-assets/. */
    base64Data: String,
    /** Structured guidelines content stored as JSON string (for clear-space-guidelines, minimum-size-guidelines, brand-usage-rules, dos-and-donts) */
    contentData: String,
    fileName: String,
    fileType: String,
    sourceUrl: String,
    source: String,
    dimensions: {
      width: Number,
      height: Number,
    },
    fileSize: Number,
    isPrimary: {
      type: Boolean,
      default: false,
    },
    tags: [String],
    // Linkage to founders and employees
    founderId: String,
    employeeId: String,
  },
  {
    timestamps: true,
  }
);

BrandAssetSchema.index({ companyId: 1, type: 1 });
BrandAssetSchema.index({ companyId: 1, isPrimary: 1 });
BrandAssetSchema.index({ companyId: 1, founderId: 1 });
BrandAssetSchema.index({ companyId: 1, employeeId: 1 });

export const BrandAsset = mongoose.model<IBrandAsset>('BrandAsset', BrandAssetSchema);
