/**
 * Business Profile Model
 */

import mongoose, { Schema, Document } from 'mongoose';
import { validateStartDateForStage } from '../utils/dateValidation';

export type BusinessStage = 'idea' | 'mvp' | 'early' | 'growth' | 'scale' | 'established';
export type Industry =
  | 'technology' | 'healthcare' | 'finance' | 'education' | 'ecommerce'
  | 'saas' | 'consulting' | 'manufacturing' | 'retail' | 'real-estate'
  | 'hospitality' | 'media' | 'non-profit' | 'legal' | 'marketing'
  | 'design' | 'food-beverage' | 'sports' | 'other';

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 IMapLinks {
  googleMaps?: string;
  appleMaps?: string;
  bingMaps?: string;
  hereMaps?: string;
  openStreetMap?: string;
  what3Words?: string;
}

export interface IBusinessProfile extends Document {
  // A. Basic Info
  name: string;
  companyId: string;
  startDate?: string;
  stage: BusinessStage;
  teamSize?: number;

  // B. Overview
  description?: string;
  descriptionLong?: string;
  mission?: string;
  vision?: string;
  coreValues?: string;
  usp?: string;

  // C. Market
  primaryIndustry?: string;
  secondaryIndustries?: string;
  targetGeography?: string;
  businessModel?: string;

  // D. Offer Layer
  primaryOffering?: string;
  secondaryOfferings?: string;
  pricingModel?: string;
  averageTicketSize?: string;

  // E. Financial
  funding?: string;
  revenue?: string;
  isRevenuePublic: boolean;

  // F. Contact
  email?: string;
  phone?: string;
  phoneCountryCode?: string;
  whatsappNumber?: string;
  whatsappCountryCode?: string;
  additionalContacts?: { countryCode: string; number: string }[];
  website?: string;

  // G. Location
  address?: string;
  city?: string;
  state?: string;
  country?: string;
  googleMapsLink?: string;

  // Legacy fields
  industries: Industry[];
  isFounderPublic: boolean;
  socialProfiles: ISocialProfiles;
  mapLinks: IMapLinks;
  createdAt: Date;
  updatedAt: Date;
}

const BusinessProfileSchema = new Schema<IBusinessProfile>({
  name: {
    type: String,
    required: [true, 'Business name is required'],
    trim: true,
    maxlength: [200, 'Business name cannot exceed 200 characters']
  },
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true,
    unique: true
  },
  startDate: {
    type: String,
    validate: {
      validator: function(v: string) {
        if (!v) return true; // optional field
        if (!/^\d{4}-\d{2}-\d{2}$/.test(v)) return false;
        const [y, m, d] = v.split('-').map(Number);
        const year = Number(y);
        const month = Number(m);
        const day = Number(d);
        if (year < 1900 || year > 2026) return false;
        if (month < 1 || month > 12) return false;
        if (day < 1 || day > 31) return false;
        const date = new Date(year, month - 1, day);
        if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) return false;
        // Stage-aware validation: startDate must be valid for the selected business stage
        const stage = this.stage;
        if (stage) {
          const result = validateStartDateForStage(v, stage);
          if (result) return false;
        }
        return true;
      },
      message: 'Please select a valid start date for the selected business stage.'
    }
  },
  stage: {
    type: String,
    enum: ['idea', 'mvp', 'early', 'growth', 'scale', 'established'],
    required: true
  },
  industries: [{
    type: String,
    enum: ['technology', 'healthcare', 'finance', 'education', 'ecommerce',
           'saas', 'consulting', 'manufacturing', 'retail', 'real-estate',
           'hospitality', 'media', 'non-profit', 'legal', 'marketing',
           'design', 'food-beverage', 'sports', 'other']
  }],
  // B. Overview
  description: String,
  descriptionLong: String,
  mission: String,
  vision: String,
  coreValues: String,
  usp: String,

  // C. Market
  primaryIndustry: String,
  secondaryIndustries: String,
  targetGeography: String,
  businessModel: String,

  // D. Offer Layer
  primaryOffering: String,
  secondaryOfferings: String,
  pricingModel: String,
  averageTicketSize: String,

  // E. Financial
  funding: String,
  revenue: String,
  isRevenuePublic: {
    type: Boolean,
    default: false
  },

  // Legacy fields
  teamSize: {
    type: Number,
    min: 1,
    validate: {
      validator: Number.isInteger,
      message: 'Team Size must be a positive number.',
    },
  },
  isFounderPublic: {
    type: Boolean,
    default: true
  },

  // G. Location
  address: String,
  city: String,
  state: String,
  country: String,
  googleMapsLink: String,
  phone: String,
  phoneCountryCode: String,
  whatsappNumber: String,
  whatsappCountryCode: String,
  additionalContacts: {
    type: [{
      _id: false,
      countryCode: { type: String, default: '' },
      number: { type: String, default: '' },
    }],
    default: [],
  },
  email: String,
  website: String,
  socialProfiles: {
    type: Object,
    default: {}
  },
  mapLinks: {
    type: Object,
    default: {}
  }
}, {
  timestamps: true
});

// Indexes
BusinessProfileSchema.index({ companyId: 1 }, { unique: true });

export const BusinessProfile = mongoose.models.BusinessProfile || mongoose.model<IBusinessProfile>('BusinessProfile', BusinessProfileSchema);
