module.exports = (sequelize, DataTypes) => {
  const BPBank = sequelize.define('BPBank', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    bp_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    bank_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Bank',
        key: 'id'
      }
    },
    account_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    account_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    ifsc_code: {
      type: DataTypes.STRING(11),
      allowNull: true
    },
    branch_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    branch_address: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    is_default: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    effective_from: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    effective_to: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    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: 'bp_bank',
    indexes: [
      {
        fields: ['bp_id']
      },
      {
        fields: ['bank_id']
      },
      {
        fields: ['status']
      },
      {
        unique: true,
        fields: ['bp_id', 'bank_id', 'account_number']
      }
    ]
  });

  BPBank.associate = function(models) {
    BPBank.belongsTo(models.BusinessPartner, { foreignKey: 'bp_id', as: 'businessPartner' });
    BPBank.belongsTo(models.Bank, { foreignKey: 'bank_id', as: 'bank' });
  };

  return BPBank;
};

