'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('bank', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      bank_code: {
        type: Sequelize.STRING(20),
        allowNull: false,
        unique: true
      },
      bank_name: {
        type: Sequelize.STRING(200),
        allowNull: false
      },
      bank_short_name: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      ifsc_code: {
        type: Sequelize.STRING(11),
        allowNull: true,
        comment: 'Bank IFSC code (first 4 characters)'
      },
      micr_code: {
        type: Sequelize.STRING(9),
        allowNull: true,
        comment: 'MICR code'
      },
      bank_type: {
        type: Sequelize.ENUM('PUBLIC_SECTOR', 'PRIVATE_SECTOR', 'FOREIGN', 'SMALL_FINANCE', 'PAYMENT', 'COOPERATIVE', 'OTHER'),
        allowNull: true
      },
      address_line1: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      address_line2: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      city_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'city',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      state_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'state',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      country_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'country',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      pincode: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      phone: {
        type: Sequelize.STRING(20),
        allowNull: true
      },
      email: {
        type: Sequelize.STRING(100),
        allowNull: true
      },
      website: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('bank', ['bank_code'], { unique: true });
    await queryInterface.addIndex('bank', ['ifsc_code']);
    await queryInterface.addIndex('bank', ['status']);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('bank');
  }
};

