'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const table = await queryInterface.describeTable('delivery_challan');

    if (!table.dispatch_date) {
      await queryInterface.addColumn('delivery_challan', 'dispatch_date', {
        type: Sequelize.DATE,
        allowNull: true
      });
    }

    if (!table.driver_phone) {
      await queryInterface.addColumn('delivery_challan', 'driver_phone', {
        type: Sequelize.STRING(30),
        allowNull: true
      });
    }

    if (!table.delivery_status) {
      await queryInterface.addColumn('delivery_challan', 'delivery_status', {
        type: Sequelize.ENUM('IN_TRANSIT', 'DELIVERED'),
        allowNull: false,
        defaultValue: 'IN_TRANSIT'
      });
      await queryInterface.addIndex('delivery_challan', ['delivery_status']);
      await queryInterface.sequelize.query(`
        UPDATE delivery_challan
        SET delivery_status = CASE
          WHEN status IN ('DELIVERED', 'CLOSED') THEN 'DELIVERED'
          ELSE 'IN_TRANSIT'
        END
      `);
    }

    if (!table.delivered_by_name) {
      await queryInterface.addColumn('delivery_challan', 'delivered_by_name', {
        type: Sequelize.STRING(120),
        allowNull: true
      });
    }

    if (!table.received_by) {
      await queryInterface.addColumn('delivery_challan', 'received_by', {
        type: Sequelize.STRING(120),
        allowNull: true
      });
    }

    if (!table.delivery_date) {
      await queryInterface.addColumn('delivery_challan', 'delivery_date', {
        type: Sequelize.DATEONLY,
        allowNull: true
      });
    }

    if (!table.delivery_time) {
      await queryInterface.addColumn('delivery_challan', 'delivery_time', {
        type: Sequelize.TIME,
        allowNull: true
      });
    }

    if (!table.receiver_signature) {
      await queryInterface.addColumn('delivery_challan', 'receiver_signature', {
        type: Sequelize.TEXT,
        allowNull: true
      });
    }

    if (!table.delivery_proof_path) {
      await queryInterface.addColumn('delivery_challan', 'delivery_proof_path', {
        type: Sequelize.STRING(255),
        allowNull: true
      });
    }
  },

  async down(queryInterface) {
    const table = await queryInterface.describeTable('delivery_challan');

    if (table.delivery_proof_path) await queryInterface.removeColumn('delivery_challan', 'delivery_proof_path');
    if (table.receiver_signature) await queryInterface.removeColumn('delivery_challan', 'receiver_signature');
    if (table.delivery_time) await queryInterface.removeColumn('delivery_challan', 'delivery_time');
    if (table.delivery_date) await queryInterface.removeColumn('delivery_challan', 'delivery_date');
    if (table.received_by) await queryInterface.removeColumn('delivery_challan', 'received_by');
    if (table.delivered_by_name) await queryInterface.removeColumn('delivery_challan', 'delivered_by_name');
    if (table.delivery_status) {
      await queryInterface.removeIndex('delivery_challan', ['delivery_status']);
      await queryInterface.removeColumn('delivery_challan', 'delivery_status');
    }
    if (table.driver_phone) await queryInterface.removeColumn('delivery_challan', 'driver_phone');
    if (table.dispatch_date) await queryInterface.removeColumn('delivery_challan', 'dispatch_date');
  }
};
