const mongoose = require("mongoose");

// Schema cho social links
const socialLinkSchema = new mongoose.Schema(
  {
    platform: {
      type: String,
      required: true,
      trim: true,
    },
    url: {
      type: String,
      required: true,
      trim: true,
    },
    icon: {
      type: String,
      required: true,
      trim: true,
    },
  },
  { _id: false }
);

// Schema cho footer links
const footerLinkSchema = new mongoose.Schema(
  {
    title: {
      type: String,
      required: true,
      trim: true,
    },
    url: {
      type: String,
      required: true,
      trim: true,
    },
  },
  { _id: false }
);

// Schema cho footer columns
const footerColumnSchema = new mongoose.Schema(
  {
    title: {
      type: String,
      required: true,
      trim: true,
    },
    links: {
      type: [footerLinkSchema],
      default: [],
    },
  },
  { _id: false }
);

// Schema cho logo
const logoSchema = new mongoose.Schema(
  {
    src: {
      type: String,
      required: true,
      trim: true,
    },
    alt: {
      type: String,
      required: true,
      trim: true,
    },
  },
  { _id: false }
);

// Schema cho address
const addressSchema = new mongoose.Schema(
  {
    text: {
      type: String,
      required: true,
      trim: true,
    },
    // Thêm address2 (địa chỉ dòng 2) không bắt buộc
    address2: {
      type: String,
      trim: true,
      default: "",
    },
    // Optional secondary link (e.g., a second map or external resource)
    link2: {
      type: String,
      trim: true,
      default: "",
    },
    mapUrl: {
      type: String,
      required: true,
      trim: true,
    },
  },
  { _id: false }
);

// Schema cho contact info
const contactInfoSchema = new mongoose.Schema(
  {
    phone: {
      type: String,
      required: true,
      trim: true,
    },
    hours: {
      type: String,
      required: true,
      trim: true,
    },
    email: {
      type: String,
      required: true,
      trim: true,
    },
  },
  { _id: false }
);

// Schema cho copyright
const copyrightSchema = new mongoose.Schema(
  {
    text: {
      type: String,
      required: true,
      trim: true,
    },
  },
  { _id: false }
);

// Schema cho about section (column đầu tiên)
const aboutSchema = new mongoose.Schema(
  {
    title: {
      type: String,
      required: true,
      trim: true,
    },
    description: {
      type: String,
      required: true,
      trim: true,
    },
    mapLink: {
      text: {
        type: String,
        required: true,
        trim: true,
      },
      url: {
        type: String,
        required: true,
        trim: true,
      },
    },
  },
  { _id: false }
);

// Main Footer Schema
const footerSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      default: "default",
      unique: true,
    },
    about: {
      type: aboutSchema,
      required: true,
    },
    address: {
      type: addressSchema,
      required: true,
    },
    contact: {
      type: contactInfoSchema,
      required: true,
    },
    columns: {
      type: [footerColumnSchema],
      default: [],
    },
    social: {
      links: {
        type: [socialLinkSchema],
        default: [],
      },
    },
    copyright: {
      type: copyrightSchema,
      required: true,
    },
  },
  {
    timestamps: true,
  }
);

// Tạo migration script để import dữ liệu từ footer.json
footerSchema.statics.migrateFromJson = async function (jsonData) {
  try {
    // Kiểm tra xem đã có footer mặc định chưa
    const existingFooter = await this.findOne({ name: "default" });

    if (existingFooter) {
      // Cập nhật footer hiện có
      Object.assign(existingFooter, jsonData);
      await existingFooter.save();
      console.log("Footer data updated successfully");
      return existingFooter;
    } else {
      // Tạo footer mới với dữ liệu từ JSON
      const newFooter = await this.create({
        name: "default",
        ...jsonData,
      });
      console.log("Footer data imported successfully");
      return newFooter;
    }
  } catch (error) {
    console.error("Error migrating footer data:", error);
    throw error;
  }
};

module.exports = mongoose.model("Footer", footerSchema);
