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

module.exports = (sequelize) => {
    const WorkflowApprover = sequelize.define(
        "workflowapprover",
        {
            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: true },
            owner: { type: DataTypes.INTEGER, allowNull: true },

            status: { type: DataTypes.STRING, allowNull: true },

            // fk
            workflow_id: { type: DataTypes.INTEGER, allowNull: false },
            territory_id: { type: DataTypes.INTEGER, allowNull: true },
            designation_id: { type: DataTypes.INTEGER, allowNull: true },
        },
        {
            tableName: "workflowapprovers",
            freezeTableName: true,
        }
    );

    WorkflowApprover.associate = (models) => {
        /**
         * Associates the WorkflowApprover model with the Workflow model.
         * Defines a many-to-one relationship between WorkflowApprover and Workflow.
         * @param {object} models - The models to associate with.
         */
        WorkflowApprover.belongsTo(models.Workflow, { foreignKey: "workflow_id", as: "workflow" });

        /**
         * Associates the WorkflowApprover model with the Territory model.
         * Defines a many-to-one relationship between WorkflowApprover and Territory.
         * @param {object} models - The models to associate with.
         */
        WorkflowApprover.belongsTo(models.Territory, { foreignKey: "territory_id", as: "territory" });

        /**
         * Associates the WorkflowApprover model with the Designation model.
         * Defines a many-to-one relationship between WorkflowApprover and Designation.
         * @param {object} models - The models to associate with.
         */
        WorkflowApprover.belongsTo(models.Designation, { foreignKey: "designation_id", as: "designation" });
    };

    return WorkflowApprover;
};
