/**
 * Admin home content service
 */

const Banner = require('../models/Banner');
const HotService = require('../models/HotService');

// ============ Banner Services ============

exports.getAllBanners = async () => {
  const banners = await Banner.findAll({
    order: [
      ['sort_order', 'ASC'],
      ['id', 'ASC'],
    ],
  });
  return banners;
};

exports.createBanner = async (data) => {
  const { image_url, link_url, sort_order, is_active } = data;

  if (!image_url) {
    throw new Error('Image URL is required');
  }

  const banner = await Banner.create({
    image_url,
    link_url: link_url || null,
    sort_order: sort_order || 0,
    is_active: is_active !== undefined ? is_active : true,
  });

  return banner;
};

exports.updateBanner = async (id, data) => {
  const banner = await Banner.findByPk(id);

  if (!banner) {
    throw new Error('Banner not found');
  }

  const { image_url, link_url, sort_order, is_active } = data;

  await banner.update({
    image_url: image_url || banner.image_url,
    link_url: link_url !== undefined ? link_url : banner.link_url,
    sort_order: sort_order !== undefined ? sort_order : banner.sort_order,
    is_active: is_active !== undefined ? is_active : banner.is_active,
  });

  return banner;
};

exports.deleteBanner = async (id) => {
  const banner = await Banner.findByPk(id);

  if (!banner) {
    throw new Error('Banner not found');
  }

  await banner.destroy();
};

// ============ Hot Service Services ============

exports.getAllHotServices = async () => {
  const services = await HotService.findAll({
    order: [
      ['sort_order', 'ASC'],
      ['id', 'ASC'],
    ],
  });
  return services;
};

exports.createHotService = async (data) => {
  const { name_zh, name_en, name_pt, service_type, image_url, detail_image, link_url, sort_order, is_active } = data;

  if (!name_zh || !name_en || !name_pt) {
    throw new Error('All language names are required');
  }

  const service = await HotService.create({
    name_zh,
    name_en,
    name_pt,
    service_type: service_type || 'travel',
    image_url: image_url || null,
    detail_image: detail_image || null,
    link_url: link_url || null,
    sort_order: sort_order || 0,
    is_active: is_active !== undefined ? is_active : true,
  });

  return service;
};

exports.updateHotService = async (id, data) => {
  const service = await HotService.findByPk(id);

  if (!service) {
    throw new Error('Hot service not found');
  }

  const { name_zh, name_en, name_pt, service_type, image_url, detail_image, link_url, sort_order, is_active } = data;

  await service.update({
    name_zh: name_zh || service.name_zh,
    name_en: name_en || service.name_en,
    name_pt: name_pt || service.name_pt,
    service_type: service_type !== undefined ? service_type : service.service_type,
    image_url: image_url !== undefined ? image_url : service.image_url,
    detail_image: detail_image !== undefined ? detail_image : service.detail_image,
    link_url: link_url !== undefined ? link_url : service.link_url,
    sort_order: sort_order !== undefined ? sort_order : service.sort_order,
    is_active: is_active !== undefined ? is_active : service.is_active,
  });

  return service;
};

exports.deleteHotService = async (id) => {
  const service = await HotService.findByPk(id);

  if (!service) {
    throw new Error('Hot service not found');
  }

  await service.destroy();
};
