const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    const GimmickAllocation = sequelize.define('gimmickallocation', {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        creation: { type: DataTypes.DATE, allowNull: false },
        modified: { type: DataTypes.DATE, allowNull: false },
        modified_by: { type: DataTypes.INTEGER, allowNull: false },
        owner: { type: DataTypes.INTEGER, allowNull: false },
        enabled: { type: DataTypes.BOOLEAN, allowNull: false },
        
        item_name: { type: DataTypes.STRING, allowNull: true },
        gimmick_value: { type: DataTypes.DECIMAL, allowNull: true },
        kerja_sama: { type: DataTypes.DECIMAL(29, 2), allowNull: true },

        // fk
        item_id: { type: DataTypes.INTEGER, allowNull: true },
        qty: { type: DataTypes.INTEGER, allowNull: true },
        gimmickreq_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: "gimmickrequests", key: "id" } },
    }, {
        tableName: 'gimmickallocations',
        freezeTableName: true
    });

    /**
     * Associates the GimmickAllocation model with other models.
     * @param {object} models - The models to associate with.
     * @description
     */
    GimmickAllocation.associate = (models) => {
        GimmickAllocation.belongsTo(models.GimmickRequest, { foreignKey: 'gimmickreq_id', as: 'gimmickrequest', onDelete: 'CASCADE' });
        GimmickAllocation.belongsTo(models.Item, { foreignKey: 'item_id', as: 'item' });
        GimmickAllocation.hasMany(models.GimmickAllocationOutlet, { foreignKey: 'gimmickallocation_id', as: 'gimmickallocationoutlets', onDelete: 'CASCADE', onUpdate: 'CASCADE' });
    };
    return GimmickAllocation;
};
