/**
 * Founder Model
 */

import mongoose, { Schema, Document } from 'mongoose';

export type AssetType =
  | 'headshot' | 'profilePhoto' | 'signature' | 'businessCard'
  | 'emailSignature' | 'bioPdf' | 'resume' | 'speakerReel' | 'custom';

export type ResponsibilityArea =
  | 'vision' | 'tech' | 'sales' | 'marketing' | 'operations' | 'finance' | 'product' | 'hr';

export interface IFounderAsset {
  id: string;
  type: AssetType;
  name: string;
  url?: string;
  base64Data?: string;
  createdAt: string;
}

export interface ISocialProfiles {
  linkedIn?: string;
  twitter?: string;
  instagram?: string;
  facebook?: string;
  tikTok?: string;
  youTube?: string;
  pinterest?: string;
  threads?: string;
  quora?: string;
  medium?: string;
  reddit?: string;
  telegram?: string;
  whatsApp?: string;
  googleBusiness?: string;
  meetup?: string;
  spotifyPodcast?: string;
  applePodcast?: string;
  website?: string;
  github?: string;
}

export interface IFounder extends Document {
  name: string;
  companyId: string;
  designation?: string;
  email?: string;
  phone?: string;
  phoneCountryCode?: string;
  city?: string;
  state?: string;
  country?: string;
  dateOfBirth?: string;
  workAnniversary?: string;
  expertise?: string[];
  responsibilityArea?: ResponsibilityArea;
  bio?: string;
  socialProfiles: ISocialProfiles;
  /** AI-generated per-platform social media bios, keyed by platform id. */
  socialBios?: { [platform: string]: string };
  /** Optional per-platform usernames the user can feed into bio generation, keyed by platform id. */
  socialBioUsernames?: { [platform: string]: string };
  /** Uploaded or AI-generated profile image URL per platform, keyed by platform id. */
  socialBioImages?: { [platform: string]: string };
  assets: IFounderAsset[];
  businessAssets?: string[];
  // Photos & Media
  photos?: string[];           // Multiple photos (uploaded files or URLs)
  driveLink?: string;          // Google Drive link for bulk images
  createdBy?: string;          // User ID who created this record
  createdAt: Date;
  updatedAt: Date;
}

const FounderSchema = new Schema<IFounder>({
  name: {
    type: String,
    required: [true, 'Founder name is required'],
    trim: true,
    maxlength: [100, 'Name cannot exceed 100 characters']
  },
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true
  },
  designation: String,
  email: String,
  phone: String,
  phoneCountryCode: String,
  city: String,
  state: String,
  country: String,
  dateOfBirth: String,
  workAnniversary: String,
  expertise: [String],
  responsibilityArea: {
    type: String,
    enum: ['vision', 'tech', 'sales', 'marketing', 'operations', 'finance', 'product', 'hr']
  },
  bio: String,
  socialProfiles: {
    type: Object,
    default: {}
  },
  socialBios: {
    type: Object,
    default: {}
  },
  socialBioUsernames: {
    type: Object,
    default: {}
  },
  socialBioImages: {
    type: Object,
    default: {}
  },
  assets: {
    type: [{
      id: String,
      type: {
        type: String,
        enum: ['headshot', 'profilePhoto', 'signature', 'businessCard', 'emailSignature', 'bioPdf', 'resume', 'speakerReel', 'custom']
      },
      name: String,
      url: String,
      base64Data: String,
      createdAt: String
    }],
    default: []
  },
  businessAssets: {
    type: [String],
    default: []
  },
  // Photos & Media
  photos: [String],            // Array of image URLs
  driveLink: String,           // Google Drive link
  createdBy: String            // User ID who created this record
}, {
  timestamps: true
});

FounderSchema.index({ companyId: 1 });

// Enforce unique founder email per company at the DB level (race-condition backstop
// for the application-level check in the routes). Scoped to companyId so different
// companies can independently have a founder with the same email (multi-tenant).
// - partialFilterExpression: only index docs that actually have a string email, so
//   the many founders without an email don't collide on a null/missing value.
// - collation strength:2 makes the uniqueness check case-insensitive
//   (e.g. Rohan@... and rohan@... are treated as the same email).
FounderSchema.index(
  { companyId: 1, email: 1 },
  {
    unique: true,
    partialFilterExpression: { email: { $type: 'string' } },
    collation: { locale: 'en', strength: 2 },
    name: 'uniq_companyId_email_ci',
  }
);

export const Founder = mongoose.model<IFounder>('Founder', FounderSchema);
