/**
 * Wikipedia Profile Model
 *
 * Stores AI-generated Wikipedia-style structured profiles (Company, Founder, Employee).
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPES
// ============================================

export type WikipediaProfileType = 'company' | 'founder' | 'employee';

export interface IWikipediaProfile extends Document {
  companyId: string;
  profileType: WikipediaProfileType;
  profileName: string;

  // References to source data
  businessProfileId?: string;
  founderId?: string;
  employeeId?: string;

  // Language
  language: string;

  // AI generation context
  prompt?: string;
  aiProvider?: string;
  aiModel?: string;
  aiGeneratedAt?: Date;

  // The structured Wikipedia-style profile data
  profileData: IWikipediaCompanyProfile | IWikipediaFounderProfile | IWikipediaEmployeeProfile;

  // Metadata
  createdBy: string;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// COMPANY PROFILE
// ============================================

export interface IWikipediaCompanyProfile {
  companyName?: string;
  nativeName?: string;
  logo?: string;
  companyType?: string;
  industry?: string;
  founded?: string;
  founder?: string;
  headquarters?: string;
  areaServed?: string;
  keyPeople?: string;
  numberOfEmployees?: string;
  products?: string;
  services?: string;
  brands?: string;
  parentOrganization?: string;
  subsidiaries?: string;
  website?: string;
  socialLinks?: {
    linkedIn?: string;
    twitter?: string;
    instagram?: string;
    facebook?: string;
    youtube?: string;
    other?: string;
  };
  mission?: string;
  vision?: string;
  description?: string;
  revenue?: string;
  funding?: string;
  businessModel?: string;
  targetGeography?: string;
  coreValues?: string;
  usp?: string;
  primaryOffering?: string;
  email?: string;
  phone?: string;
  address?: string;
}

// ============================================
// FOUNDER PROFILE
// ============================================

export interface IWikipediaFounderBorn {
  dateOfBirth?: string;
  birthPlace?: string;
}

export interface IWikipediaFounderProfile {
  // Personal Details
  fullName?: string;
  nativeName?: string;
  born?: IWikipediaFounderBorn | string;
  nationality?: string;
  citizenship?: string;
  residence?: string;
  occupation?: string;
  position?: string;
  organization?: string;
  knownFor?: string;
  // Family
  spouse?: string;
  children?: string;
  parents?: string;
  siblings?: string;
  // Education
  education?: string;
  almaMater?: string;
  qualifications?: string;
  // Career
  experience?: string;
  previousOrganizations?: string;
  currentOrganization?: string;
  awards?: string;
  honors?: string;
  recognitions?: string;
  // Other
  signature?: string;
  email?: string;
  phone?: string;
  city?: string;
  country?: string;
  expertise?: string;
  bio?: string;
  website?: string;
  socialProfiles?: {
    linkedIn?: string;
    twitter?: string;
    instagram?: string;
    facebook?: string;
    youtube?: string;
    other?: string;
  };
}

// ============================================
// EMPLOYEE PROFILE
// ============================================

export interface IWikipediaEmployeeBorn {
  dateOfBirth?: string;
  birthPlace?: string;
}

export interface IWikipediaEmployeeProfile {
  // Personal Details
  fullName?: string;
  born?: IWikipediaEmployeeBorn | string;
  nationality?: string;
  citizenship?: string;
  residence?: string;
  // Employment
  employeeId?: string;
  organization?: string;
  department?: string;
  designation?: string;
  employmentType?: string;
  reportingManager?: string;
  joiningDate?: string;
  experience?: string;
  // Education
  education?: string;
  almaMater?: string;
  certifications?: string;
  // Family
  spouse?: string;
  children?: string;
  parents?: string;
  // Professional
  skills?: string;
  awards?: string;
  honors?: string;
  officeLocation?: string;
  workEmail?: string;
  // Other
  signature?: string;
  linkedIn?: string;
  website?: string;
  phone?: string;
  city?: string;
  country?: string;
  expertise?: string;
  bio?: string;
}

// ============================================
// SCHEMAS
// ============================================

const SocialLinksSchema = new Schema({
  linkedIn: { type: String, default: '' },
  twitter: { type: String, default: '' },
  instagram: { type: String, default: '' },
  facebook: { type: String, default: '' },
  youtube: { type: String, default: '' },
  other: { type: String, default: '' },
}, { _id: false });

const BornSchema = new Schema({
  dateOfBirth: { type: String, default: '' },
  birthPlace: { type: String, default: '' },
}, { _id: false });

const CompanyProfileSchema = new Schema<IWikipediaCompanyProfile>({
  companyName: String,
  nativeName: String,
  logo: String,
  companyType: String,
  industry: String,
  founded: String,
  founder: String,
  headquarters: String,
  areaServed: String,
  keyPeople: String,
  numberOfEmployees: String,
  products: String,
  services: String,
  brands: String,
  parentOrganization: String,
  subsidiaries: String,
  website: String,
  socialLinks: { type: SocialLinksSchema, default: {} },
  mission: String,
  vision: String,
  description: String,
  revenue: String,
  funding: String,
  businessModel: String,
  targetGeography: String,
  coreValues: String,
  usp: String,
  primaryOffering: String,
  email: String,
  phone: String,
  address: String,
}, { _id: false });

const FounderProfileSchema = new Schema<IWikipediaFounderProfile>({
  fullName: String,
  nativeName: String,
  born: { type: Schema.Types.Mixed, default: {} },
  nationality: String,
  citizenship: String,
  residence: String,
  occupation: String,
  position: String,
  organization: String,
  knownFor: String,
  spouse: String,
  children: String,
  parents: String,
  siblings: String,
  education: String,
  almaMater: String,
  qualifications: String,
  experience: String,
  previousOrganizations: String,
  currentOrganization: String,
  awards: String,
  honors: String,
  recognitions: String,
  signature: String,
  email: String,
  phone: String,
  city: String,
  country: String,
  expertise: String,
  bio: String,
  website: String,
  socialProfiles: { type: SocialLinksSchema, default: {} },
}, { _id: false });

const EmployeeProfileSchema = new Schema<IWikipediaEmployeeProfile>({
  fullName: String,
  born: { type: Schema.Types.Mixed, default: {} },
  nationality: String,
  citizenship: String,
  residence: String,
  employeeId: String,
  organization: String,
  department: String,
  designation: String,
  employmentType: String,
  reportingManager: String,
  joiningDate: String,
  experience: String,
  education: String,
  almaMater: String,
  certifications: String,
  spouse: String,
  children: String,
  parents: String,
  skills: String,
  awards: String,
  honors: String,
  officeLocation: String,
  workEmail: String,
  signature: String,
  linkedIn: String,
  website: String,
  phone: String,
  city: String,
  country: String,
  expertise: String,
  bio: String,
}, { _id: false });

// ============================================
// MAIN SCHEMA
// ============================================

const WikipediaProfileSchema = new Schema<IWikipediaProfile>({
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true,
  },
  profileType: {
    type: String,
    enum: ['company', 'founder', 'employee'],
    required: [true, 'Profile type is required'],
  },
  profileName: {
    type: String,
    required: [true, 'Profile name is required'],
    trim: true,
    maxlength: [300, 'Profile name cannot exceed 300 characters'],
  },
  businessProfileId: {
    type: String,
    default: '',
  },
  founderId: String,
  employeeId: String,
  language: {
    type: String,
    default: 'English',
  },
  prompt: String,
  aiProvider: String,
  aiModel: String,
  aiGeneratedAt: Date,
  profileData: {
    type: Schema.Types.Mixed,
    required: [true, 'Profile data is required'],
  },
  createdBy: {
    type: String,
    required: [true, 'Created by is required'],
  },
}, {
  timestamps: true,
  toJSON: {
    virtuals: true,
    transform: (_doc, ret) => {
      ret.id = ret._id?.toString?.() || ret._id;
      return ret;
    },
  },
  toObject: {
    virtuals: true,
  },
});

// Indexes
WikipediaProfileSchema.index({ companyId: 1, profileType: 1 });
WikipediaProfileSchema.index({ companyId: 1, businessProfileId: 1 });
WikipediaProfileSchema.index({ companyId: 1, createdBy: 1 });

export const WikipediaProfile = mongoose.models.WikipediaProfile || mongoose.model<IWikipediaProfile>('WikipediaProfile', WikipediaProfileSchema);