module.exports = (sequelize, DataTypes) => {
  const Bank = sequelize.define('Bank', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    bank_code: {
      type: DataTypes.STRING(20),
      allowNull: false,
      unique: true
    },
    bank_name: {
      type: DataTypes.STRING(200),
      allowNull: false
    },
    bank_short_name: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    ifsc_code: {
      type: DataTypes.STRING(11),
      allowNull: true,
      comment: 'Bank IFSC code (first 4 characters)'
    },
    micr_code: {
      type: DataTypes.STRING(9),
      allowNull: true,
      comment: 'MICR code'
    },
    bank_type: {
      type: DataTypes.ENUM('PUBLIC_SECTOR', 'PRIVATE_SECTOR', 'FOREIGN', 'SMALL_FINANCE', 'PAYMENT', 'COOPERATIVE', 'OTHER'),
      allowNull: true
    },
    address_line1: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    address_line2: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    city_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'City',
        key: 'id'
      }
    },
    state_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'State',
        key: 'id'
      }
    },
    country_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Country',
        key: 'id'
      }
    },
    pincode: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    phone: {
      type: DataTypes.STRING(20),
      allowNull: true
    },
    email: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    website: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'bank',
    indexes: [
      {
        unique: true,
        fields: ['bank_code']
      },
      {
        fields: ['ifsc_code']
      },
      {
        fields: ['status']
      }
    ]
  });

  Bank.associate = function(models) {
    Bank.belongsTo(models.City, { foreignKey: 'city_id', as: 'city' });
    Bank.belongsTo(models.State, { foreignKey: 'state_id', as: 'state' });
    Bank.belongsTo(models.Country, { foreignKey: 'country_id', as: 'country' });
    Bank.hasMany(models.BPBank, { foreignKey: 'bank_id', as: 'bpBanks' });
  };

  return Bank;
};

