// models/visa.js

const mongoose = require("mongoose");

// ==================== SCHEMAS ====================

// VisaItem Schema
const VisaItemSchema = new mongoose.Schema(
  {
    title: { type: String, default: "" },
    description: { type: String, default: "" },
  },
  { _id: false },
);

// VisaTypeCategory Schema
const VisaTypeCategorySchema = new mongoose.Schema(
  {
    category: { type: String, default: "" },
    items: [VisaItemSchema],
  },
  { _id: false },
);

// VisaProcessStep Schema
const VisaProcessStepSchema = new mongoose.Schema(
  {
    number: { type: String, default: "" },
    title: { type: String, default: "" },
    description: { type: String, default: "" },
  },
  { _id: false },
);

// VisaProcess Schema
const VisaProcessSchema = new mongoose.Schema(
  {
    title: { type: String, default: "" },
    steps: [VisaProcessStepSchema],
  },
  { _id: false },
);

// VisaCategory Schema
const VisaCategorySchema = new mongoose.Schema(
  {
    title: { type: String, default: "" },
    steps: {
      type: [[String]],
      default: [],
    },
  },
  { _id: false },
);

// VisaService Schema
const VisaServiceSchema = new mongoose.Schema(
  {
    title: { type: String, default: "" },
    steps: [VisaProcessStepSchema],
  },
  { _id: false },
);

// RelatedCountry Schema
const RelatedCountrySchema = new mongoose.Schema(
  {
    id: { type: Number, default: 0 },
    name: { type: String, default: "" },
    icon: { type: String, default: "" },
  },
  { _id: false },
);

// Phone Schema
const PhoneSchema = new mongoose.Schema(
  {
    label: { type: String, default: "" },
    value: { type: String, default: "" },
    link: { type: String, default: "" },
  },
  { _id: false },
);

// Email Schema
const EmailSchema = new mongoose.Schema(
  {
    label: { type: String, default: "" },
    value: { type: String, default: "" },
    link: { type: String, default: "" },
  },
  { _id: false },
);

// Location Schema
const LocationSchema = new mongoose.Schema(
  {
    label: { type: String, default: "" },
    address: { type: String, default: "" },
  },
  { _id: false },
);

// ContactInfo Schema
const ContactInfoSchema = new mongoose.Schema(
  {
    img: { type: String, default: "" },
    sectionTitle: { type: String, default: "" },
    helpText: { type: String, default: "" },

    phone: {
      type: PhoneSchema,
      default: () => ({}),
    },
    email: {
      type: EmailSchema,
      default: () => ({}),
    },
    location: {
      type: LocationSchema,
      default: () => ({}),
    },
  },
  { _id: false },
);

// ActiveCountry Schema
const ActiveCountrySchema = new mongoose.Schema(
  {
    id: { type: Number, default: 0 },
    name: { type: String, default: "" },
    title: { type: String, default: "" },
    mainImage: { type: String, default: "" },
    description: { type: String, default: "" },
    additionalInfo: { type: String, default: "" },
    tagline: { type: String, default: "" },
    visaTypes: [VisaTypeCategorySchema],
    visaProcess: {
      type: VisaProcessSchema,
      default: null,
    },
    gallery: {
      type: [String],
      default: [],
    },
    visaCategories: {
      type: VisaCategorySchema,
      default: null,
    },
    visaService: {
      type: VisaServiceSchema,
      default: null,
    },
  },
  { _id: false },
);

// DetailedView Schema
const DetailedViewSchema = new mongoose.Schema(
  {
    activeCountry: {
      type: ActiveCountrySchema,
      default: null,
    },
    relatedCountries: {
      type: [RelatedCountrySchema],
      default: [],
    },
    contactInfo: {
      type: ContactInfoSchema,
      default: null,
    },
  },
  { _id: false },
);

// ==================== MAIN VISA COUNTRY SCHEMA ====================

// Main VisaCountry Schema (Individual country object)
const VisaCountrySchema = new mongoose.Schema(
  {
    // Không dùng `index: true` ở đây vì đã tạo index riêng cho hero.summaryList.* bên dưới
    id: { type: Number, required: true },
    name: { type: String, required: true },
    slug: { type: String, required: true, unique: true },
    icon: { type: String, default: "" },
    services: {
      type: [String],
      default: [],
    },
    detailedView: {
      type: DetailedViewSchema,
      default: null,
    },
  },
  { _id: false },
);

// ==================== HERO SCHEMA ====================

const HeroSchema = new mongoose.Schema(
  {
    title: { type: String, default: "Visa" },
    summaryList: {
      type: [VisaCountrySchema],
      default: [],
    },
  },
  { _id: false },
);

// ==================== MAIN VISA SCHEMA ====================

const visaDataSchema = new mongoose.Schema(
  {
    hero: {
      type: HeroSchema,
      default: () => ({ title: "Visa", summaryList: [] }),
    },
  },
  {
    timestamps: true,
  },
);

// ==================== INDEXES ====================

visaDataSchema.index({ "hero.summaryList.slug": 1 });
visaDataSchema.index({ "hero.summaryList.id": 1 });
visaDataSchema.index({ "hero.summaryList.name": 1 });

// ==================== MODEL ====================

module.exports = mongoose.models.Visa || mongoose.model("Visa", visaDataSchema);
