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

module.exports = (sequelize) => {
    const SalesTargetDetail = sequelize.define(
        "salestargetdetail",
        {
            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 },


            // fk
            salestarget_id: {
                type: DataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: "salestargets",
                    key: "id",
                },
            },
            item_id: {
                type: DataTypes.INTEGER,
                allowNull: true,
                references: {
                    model: "items",
                    key: "id",
                },
            },
            quantity: { type: DataTypes.INTEGER, allowNull: true },
        },
        {
            tableName: "salestargetdetails",
            freezeTableName: true,
        }
    );

    SalesTargetDetail.associate = (models) => {
        SalesTargetDetail.belongsTo(models.SalesTarget, { foreignKey: "salestarget_id", onDelete: "CASCADE" });
        SalesTargetDetail.belongsTo(models.Item, { foreignKey: "item_id", as: "item" });
    };

    return SalesTargetDetail;
};
