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

module.exports = (sequelize) => {
    const SalesTarget = sequelize.define(
        "salestarget",
        {
            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
            period_id: {
                type: DataTypes.INTEGER,
                allowNull: true,
                references: {
                    model: "period  s",
                    key: "id",
                },
            },
            territory_id: {
                type: DataTypes.INTEGER,
                allowNull: true,
                references: {
                    model: "territories",
                    key: "id",
                },
            },
            company_id: {
                type: DataTypes.INTEGER,
                allowNull: true,
                references: {
                    model: "companies",
                    key: "id",
                },
            },
        },
        {
            tableName: "salestargets",
            freezeTableName: true,
        }
    );

    SalesTarget.associate = (models) => {
        SalesTarget.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
        SalesTarget.belongsTo(models.Period, { foreignKey: "period_id", as: "period" });
        SalesTarget.belongsTo(models.Territory, { foreignKey: "territory_id", as: "territory" });
        SalesTarget.hasMany(models.SalesTargetDetail, { foreignKey: "salestarget_id", as: "salestargetdetails" });
    };

    return SalesTarget;
};
