/**
 * Migration: Add service appointment form fields to appointment table
 * 添加各类服务预约表单字段
 * 
 * Requirements: 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1, 10.1, 11.1, 12.1, 13.1, 14.1, 15.1, 16.1
 */

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const tableInfo = await queryInterface.describeTable('appointment');
    
    // ========== Hotel Reservation Fields (全球酒店预定) ==========
    if (!tableInfo.check_in_date) {
      await queryInterface.addColumn('appointment', 'check_in_date', {
        type: Sequelize.DATEONLY,
        allowNull: true,
      });
    }
    
    if (!tableInfo.check_out_date) {
      await queryInterface.addColumn('appointment', 'check_out_date', {
        type: Sequelize.DATEONLY,
        allowNull: true,
      });
    }
    
    if (!tableInfo.country_city) {
      await queryInterface.addColumn('appointment', 'country_city', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.hotel_name) {
      await queryInterface.addColumn('appointment', 'hotel_name', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.room_count) {
      await queryInterface.addColumn('appointment', 'room_count', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }
    
    if (!tableInfo.room_type) {
      await queryInterface.addColumn('appointment', 'room_type', {
        type: Sequelize.STRING(100),
        allowNull: true,
      });
    }

    if (!tableInfo.need_meal) {
      await queryInterface.addColumn('appointment', 'need_meal', {
        type: Sequelize.BOOLEAN,
        allowNull: true,
      });
    }
    
    if (!tableInfo.meal_plan) {
      await queryInterface.addColumn('appointment', 'meal_plan', {
        type: Sequelize.ENUM('breakfast', 'three_meals', 'all_inclusive'),
        allowNull: true,
      });
    }
    
    // ========== VIP Lounge / Airport Transfer Fields ==========
    if (!tableInfo.arrival_flight_no) {
      await queryInterface.addColumn('appointment', 'arrival_flight_no', {
        type: Sequelize.STRING(50),
        allowNull: true,
      });
    }
    
    if (!tableInfo.departure_flight_no) {
      await queryInterface.addColumn('appointment', 'departure_flight_no', {
        type: Sequelize.STRING(50),
        allowNull: true,
      });
    }
    
    if (!tableInfo.airport_terminal) {
      await queryInterface.addColumn('appointment', 'airport_terminal', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.delivery_address) {
      await queryInterface.addColumn('appointment', 'delivery_address', {
        type: Sequelize.TEXT,
        allowNull: true,
      });
    }
    
    // ========== Unaccompanied Minor Fields (无成人陪伴儿童代办) ==========
    if (!tableInfo.itinerary) {
      await queryInterface.addColumn('appointment', 'itinerary', {
        type: Sequelize.TEXT,
        allowNull: true,
      });
    }
    
    if (!tableInfo.child_age) {
      await queryInterface.addColumn('appointment', 'child_age', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }
    
    if (!tableInfo.boy_count) {
      await queryInterface.addColumn('appointment', 'boy_count', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }
    
    if (!tableInfo.girl_count) {
      await queryInterface.addColumn('appointment', 'girl_count', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }
    
    // ========== Rail Ticket Fields (高铁票代订) ==========
    if (!tableInfo.origin_station) {
      await queryInterface.addColumn('appointment', 'origin_station', {
        type: Sequelize.STRING(100),
        allowNull: true,
      });
    }
    
    if (!tableInfo.destination_station) {
      await queryInterface.addColumn('appointment', 'destination_station', {
        type: Sequelize.STRING(100),
        allowNull: true,
      });
    }
    
    if (!tableInfo.seat_class) {
      await queryInterface.addColumn('appointment', 'seat_class', {
        type: Sequelize.ENUM('first', 'second', 'third'),
        allowNull: true,
      });
    }
    
    if (!tableInfo.passenger_count) {
      await queryInterface.addColumn('appointment', 'passenger_count', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }

    // ========== Medical/Special Needs Fields ==========
    if (!tableInfo.hospital_name) {
      await queryInterface.addColumn('appointment', 'hospital_name', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.condition_description) {
      await queryInterface.addColumn('appointment', 'condition_description', {
        type: Sequelize.TEXT,
        allowNull: true,
      });
    }
    
    if (!tableInfo.special_assistance_reason) {
      await queryInterface.addColumn('appointment', 'special_assistance_reason', {
        type: Sequelize.TEXT,
        allowNull: true,
      });
    }
    
    // ========== Pet Transportation Fields (宠物托运代理) ==========
    if (!tableInfo.origin) {
      await queryInterface.addColumn('appointment', 'origin', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.destination) {
      await queryInterface.addColumn('appointment', 'destination', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.flight_no) {
      await queryInterface.addColumn('appointment', 'flight_no', {
        type: Sequelize.STRING(50),
        allowNull: true,
      });
    }
    
    if (!tableInfo.pet_type) {
      await queryInterface.addColumn('appointment', 'pet_type', {
        type: Sequelize.STRING(100),
        allowNull: true,
      });
    }
    
    if (!tableInfo.pet_name) {
      await queryInterface.addColumn('appointment', 'pet_name', {
        type: Sequelize.STRING(100),
        allowNull: true,
      });
    }
    
    if (!tableInfo.has_quarantine_cert) {
      await queryInterface.addColumn('appointment', 'has_quarantine_cert', {
        type: Sequelize.BOOLEAN,
        allowNull: true,
      });
    }
    
    // ========== Guide/Translation Fields (西班牙语专业导游/翻译服务) ==========
    if (!tableInfo.service_days) {
      await queryInterface.addColumn('appointment', 'service_days', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }
    
    // ========== Consultation Fields ==========
    if (!tableInfo.specific_requirements) {
      await queryInterface.addColumn('appointment', 'specific_requirements', {
        type: Sequelize.TEXT,
        allowNull: true,
      });
    }

    // ========== Logistics Fields ==========
    if (!tableInfo.item_name) {
      await queryInterface.addColumn('appointment', 'item_name', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.item_quantity) {
      await queryInterface.addColumn('appointment', 'item_quantity', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }
    
    if (!tableInfo.origin_port) {
      await queryInterface.addColumn('appointment', 'origin_port', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.destination_port) {
      await queryInterface.addColumn('appointment', 'destination_port', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.cargo_name) {
      await queryInterface.addColumn('appointment', 'cargo_name', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.cargo_quantity) {
      await queryInterface.addColumn('appointment', 'cargo_quantity', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }
    
    // ========== Travel Planning Fields (旅游线路规划/咨询) ==========
    if (!tableInfo.travel_date) {
      await queryInterface.addColumn('appointment', 'travel_date', {
        type: Sequelize.DATEONLY,
        allowNull: true,
      });
    }
    
    if (!tableInfo.travel_destination) {
      await queryInterface.addColumn('appointment', 'travel_destination', {
        type: Sequelize.STRING(200),
        allowNull: true,
      });
    }
    
    if (!tableInfo.travel_days) {
      await queryInterface.addColumn('appointment', 'travel_days', {
        type: Sequelize.INTEGER,
        allowNull: true,
      });
    }
    
    // Add indexes for commonly queried fields
    try {
      await queryInterface.addIndex('appointment', ['check_in_date'], {
        name: 'idx_appointment_check_in_date',
      });
    } catch (e) {
      // Index may already exist
    }
    
    try {
      await queryInterface.addIndex('appointment', ['travel_date'], {
        name: 'idx_appointment_travel_date',
      });
    } catch (e) {
      // Index may already exist
    }
  },

  down: async (queryInterface, Sequelize) => {
    // Remove all added fields
    const fieldsToRemove = [
      // Hotel Reservation
      'check_in_date', 'check_out_date', 'country_city', 'hotel_name',
      'room_count', 'room_type', 'need_meal', 'meal_plan',
      // VIP Lounge / Airport Transfer
      'arrival_flight_no', 'departure_flight_no', 'airport_terminal', 'delivery_address',
      // Unaccompanied Minor
      'itinerary', 'child_age', 'boy_count', 'girl_count',
      // Rail Ticket
      'origin_station', 'destination_station', 'seat_class', 'passenger_count',
      // Medical/Special Needs
      'hospital_name', 'condition_description', 'special_assistance_reason',
      // Pet Transportation
      'origin', 'destination', 'flight_no', 'pet_type', 'pet_name', 'has_quarantine_cert',
      // Guide/Translation
      'service_days',
      // Consultation
      'specific_requirements',
      // Logistics
      'item_name', 'item_quantity', 'origin_port', 'destination_port', 'cargo_name', 'cargo_quantity',
      // Travel Planning
      'travel_date', 'travel_destination', 'travel_days',
    ];
    
    for (const field of fieldsToRemove) {
      try {
        await queryInterface.removeColumn('appointment', field);
      } catch (e) {
        // Column may not exist
      }
    }
    
    // Remove indexes
    try {
      await queryInterface.removeIndex('appointment', 'idx_appointment_check_in_date');
    } catch (e) {
      // Index may not exist
    }
    
    try {
      await queryInterface.removeIndex('appointment', 'idx_appointment_travel_date');
    } catch (e) {
      // Index may not exist
    }
  },
};
