/**
 * Migration: Create home content tables (banners and hot_services)
 */

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // Create banners table
    await queryInterface.createTable('banners', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      image_url: {
        type: Sequelize.STRING(500),
        allowNull: false,
        comment: 'Banner图片URL',
      },
      link_url: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '点击跳转链接',
      },
      sort_order: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        comment: '排序顺序,数字越小越靠前',
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
        comment: '是否启用',
      },
      created_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      updated_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
      },
    });

    // Add index for banners
    await queryInterface.addIndex('banners', ['sort_order', 'is_active'], {
      name: 'idx_sort_active',
    });

    // Create hot_services table
    await queryInterface.createTable('hot_services', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      name_zh: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '服务名称(中文)',
      },
      name_en: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '服务名称(英文)',
      },
      name_pt: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '服务名称(葡萄牙语)',
      },
      image_url: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '服务图片URL',
      },
      link_url: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '点击跳转链接',
      },
      sort_order: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        comment: '排序顺序,数字越小越靠前',
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
        comment: '是否启用',
      },
      created_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      updated_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
      },
    });

    // Add index for hot_services
    await queryInterface.addIndex('hot_services', ['sort_order', 'is_active'], {
      name: 'idx_hot_services_sort_active',
    });

    // Insert default hot services
    await queryInterface.bulkInsert('hot_services', [
      {
        name_zh: '国内/外机票',
        name_en: 'Domestic/International Tickets',
        name_pt: 'Passagens Domésticas/Internacionais',
        sort_order: 1,
        is_active: true,
      },
      {
        name_zh: '全球酒店',
        name_en: 'Global Hotels',
        name_pt: 'Hotéis Globais',
        sort_order: 2,
        is_active: true,
      },
      {
        name_zh: '机场贵宾室',
        name_en: 'VIP Lounge',
        name_pt: 'Sala VIP',
        sort_order: 3,
        is_active: true,
      },
      {
        name_zh: '特殊旅客定制',
        name_en: 'Custom Service',
        name_pt: 'Serviço Personalizado',
        sort_order: 4,
        is_active: true,
      },
    ]);

    console.log('✓ Home content tables created successfully');
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('hot_services');
    await queryInterface.dropTable('banners');
    console.log('✓ Home content tables dropped successfully');
  },
};
