/**
 * Migration: Add airfare booking fields to appointment table
 * 添加机票预约相关字段
 */

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const tableInfo = await queryInterface.describeTable('appointment');
    
    // 添加热门服务ID字段
    if (!tableInfo.hot_service_id) {
      await queryInterface.addColumn('appointment', 'hot_service_id', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }
    
    // 添加服务类型字段
    if (!tableInfo.service_type) {
      await queryInterface.addColumn('appointment', 'service_type', {
        type: Sequelize.STRING(50),
        allowNull: true,
      });
    }
    
    // 添加多联系方式字段
    if (!tableInfo.wechat_id) {
      await queryInterface.addColumn('appointment', 'wechat_id', {
        type: Sequelize.STRING(100),
        allowNull: true,
      });
    }
    
    if (!tableInfo.phone) {
      await queryInterface.addColumn('appointment', 'phone', {
        type: Sequelize.STRING(50),
        allowNull: true,
      });
    }
    
    if (!tableInfo.phone_country_code) {
      await queryInterface.addColumn('appointment', 'phone_country_code', {
        type: Sequelize.STRING(10),
        allowNull: true,
      });
    }
    
    if (!tableInfo.whatsapp) {
      await queryInterface.addColumn('appointment', 'whatsapp', {
        type: Sequelize.STRING(50),
        allowNull: true,
      });
    }
    
    // 添加机票预约专用字段
    if (!tableInfo.trip_type) {
      await queryInterface.addColumn('appointment', 'trip_type', {
        type: Sequelize.ENUM('single', 'round'),
        allowNull: true,
      });
    }
    
    if (!tableInfo.departure_date) {
      await queryInterface.addColumn('appointment', 'departure_date', {
        type: Sequelize.DATEONLY,
        allowNull: true,
      });
    }
    
    if (!tableInfo.return_date) {
      await queryInterface.addColumn('appointment', 'return_date', {
        type: Sequelize.DATEONLY,
        allowNull: true,
      });
    }
    
    if (!tableInfo.departure_city) {
      await queryInterface.addColumn('appointment', 'departure_city', {
        type: Sequelize.STRING(100),
        allowNull: true,
      });
    }
    
    if (!tableInfo.arrival_city) {
      await queryInterface.addColumn('appointment', 'arrival_city', {
        type: Sequelize.STRING(100),
        allowNull: true,
      });
    }
    
    if (!tableInfo.adult_count) {
      await queryInterface.addColumn('appointment', 'adult_count', {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 0,
      });
    }
    
    if (!tableInfo.child_count) {
      await queryInterface.addColumn('appointment', 'child_count', {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 0,
      });
    }
    
    if (!tableInfo.infant_count) {
      await queryInterface.addColumn('appointment', 'infant_count', {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 0,
      });
    }
    
    if (!tableInfo.cabin_type) {
      await queryInterface.addColumn('appointment', 'cabin_type', {
        type: Sequelize.ENUM('economy', 'premium_economy', 'business'),
        allowNull: true,
      });
    }
    
    if (!tableInfo.luggage_count) {
      await queryInterface.addColumn('appointment', 'luggage_count', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }
    
    // 添加索引
    try {
      await queryInterface.addIndex('appointment', ['departure_date'], {
        name: 'idx_appointment_departure_date',
      });
    } catch (e) {
      // 索引可能已存在
    }
    
    try {
      await queryInterface.addIndex('appointment', ['hot_service_id'], {
        name: 'idx_appointment_hot_service_id',
      });
    } catch (e) {
      // 索引可能已存在
    }
    
    // 修改 service_id 为可空（因为现在支持热门服务）
    await queryInterface.changeColumn('appointment', 'service_id', {
      type: Sequelize.UUID,
      allowNull: true,
    });
    
    // 修改 contact_method 和 contact_value 为可空（因为现在支持多联系方式）
    await queryInterface.changeColumn('appointment', 'contact_method', {
      type: Sequelize.ENUM('phone', 'whatsapp', 'wechat'),
      allowNull: true,
    });
    
    await queryInterface.changeColumn('appointment', 'contact_value', {
      type: Sequelize.STRING(100),
      allowNull: true,
    });
  },

  down: async (queryInterface, Sequelize) => {
    // 移除新增字段
    await queryInterface.removeColumn('appointment', 'hot_service_id');
    await queryInterface.removeColumn('appointment', 'service_type');
    await queryInterface.removeColumn('appointment', 'wechat_id');
    await queryInterface.removeColumn('appointment', 'phone');
    await queryInterface.removeColumn('appointment', 'phone_country_code');
    await queryInterface.removeColumn('appointment', 'whatsapp');
    await queryInterface.removeColumn('appointment', 'trip_type');
    await queryInterface.removeColumn('appointment', 'departure_date');
    await queryInterface.removeColumn('appointment', 'return_date');
    await queryInterface.removeColumn('appointment', 'departure_city');
    await queryInterface.removeColumn('appointment', 'arrival_city');
    await queryInterface.removeColumn('appointment', 'adult_count');
    await queryInterface.removeColumn('appointment', 'child_count');
    await queryInterface.removeColumn('appointment', 'infant_count');
    await queryInterface.removeColumn('appointment', 'cabin_type');
    await queryInterface.removeColumn('appointment', 'luggage_count');
    
    // 恢复 service_id 为必填
    await queryInterface.changeColumn('appointment', 'service_id', {
      type: Sequelize.UUID,
      allowNull: false,
    });
    
    // 恢复 contact_method 和 contact_value 为必填
    await queryInterface.changeColumn('appointment', 'contact_method', {
      type: Sequelize.ENUM('phone', 'whatsapp', 'wechat'),
      allowNull: false,
    });
    
    await queryInterface.changeColumn('appointment', 'contact_value', {
      type: Sequelize.STRING(100),
      allowNull: false,
    });
  },
};
