/**
 * Home content service
 */

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

/**
 * Get active banners (sorted by sort_order)
 */
exports.getActiveBanners = async () => {
  const banners = await Banner.findAll({
    where: { is_active: true },
    attributes: ['id', 'image_url', 'link_url', 'sort_order'],
    order: [
      ['sort_order', 'ASC'],
      ['id', 'ASC'],
    ],
  });
  return banners;
};

/**
 * Get active hot services (sorted by sort_order)
 */
exports.getActiveHotServices = async (lang = 'zh') => {
  const services = await HotService.findAll({
    where: { is_active: true },
    attributes: ['id', 'name_zh', 'name_en', 'name_es', 'service_type', 'image_url', 'detail_image', 'link_url', 'sort_order'],
    order: [
      ['sort_order', 'ASC'],
      ['id', 'ASC'],
    ],
  });

  // Map name based on language
  return services.map((service) => {
    const nameField = lang === 'en' ? 'name_en' : lang === 'es' ? 'name_es' : 'name_zh';
    return {
      id: service.id,
      name: service[nameField],
      service_type: service.service_type,
      image_url: service.image_url,
      detail_image: service.detail_image,
      link_url: service.link_url,
      sort_order: service.sort_order,
    };
  });
};
