import { Model, DataTypes, Sequelize } from 'sequelize';

export interface DealerBankDetailAttributes {
    id: string;
    dealerId: string;
    accountHolderName: string;
    accountNumber: string;
    ifscCode: string;
    bankName: string;
    branchName: string;
    isPrimary: boolean;
    status: string;
}

export interface DealerBankDetailInstance extends Model<DealerBankDetailAttributes>, DealerBankDetailAttributes { }

export default (sequelize: Sequelize) => {
    const DealerBankDetail = sequelize.define<DealerBankDetailInstance>('DealerBankDetail', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        dealerId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'dealers',
                key: 'id'
            }
        },
        accountHolderName: {
            type: DataTypes.STRING,
            allowNull: false
        },
        accountNumber: {
            type: DataTypes.STRING,
            allowNull: false
        },
        ifscCode: {
            type: DataTypes.STRING,
            allowNull: false
        },
        bankName: {
            type: DataTypes.STRING,
            allowNull: false
        },
        branchName: {
            type: DataTypes.STRING,
            allowNull: true
        },
        isPrimary: {
            type: DataTypes.BOOLEAN,
            defaultValue: false
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'active'
        }
    }, {
        tableName: 'dealer_bank_details',
        timestamps: true,
        indexes: [
            { fields: ['dealerId'] },
            { fields: ['status'] }
        ]
    });

    (DealerBankDetail as any).associate = (models: any) => {
        DealerBankDetail.belongsTo(models.Dealer, { foreignKey: 'dealerId', as: 'dealer' });
    };

    return DealerBankDetail;
};
