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

module.exports = (sequelize) => {
    const TerritoryVersionChild = sequelize.define('territoryversionchild', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: 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 },

        name: { type: DataTypes.STRING(50), allowNull: false },
        display_name: { type: DataTypes.STRING(100), allowNull: true },
        slug_name: { type: DataTypes.STRING(100), allowNull: true },
        description: { type: DataTypes.TEXT, allowNull: true },
        code: { type: DataTypes.STRING(20), allowNull: true },
        type: { type: DataTypes.STRING(20), allowNull: true },
        is_parent: { type: DataTypes.BOOLEAN, allowNull: true },

        // fk
        parent_id: { type: DataTypes.INTEGER, allowNull: true },
        territory_id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'territories',
                key: 'id'
            }
        },
        company_id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'companies',
                key: 'id'
            }
        },
        territoryversion_id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'territoryversions',
                key: 'id'
            }
        },

        // sappsv1 or erpnext
        ids: { type: DataTypes.INTEGER, allowNull: true },
    }, {
        tableName: 'territoryversionchilds',
        freezeTableName: true
    });

    /**
     * Associates the TerritoryVersionChild model with other models.
     * @param {object} models - The models to associate with.
     * @description
     *   - A TerritoryVersionChild belongs to a TerritoryVersion.
     *   - A TerritoryVersionChild belongs to a Company.
     */
    TerritoryVersionChild.associate = (models) => {
        /**
         * Associates the TerritoryVersionChild model with the TerritoryVersion model.
         * A TerritoryVersionChild belongs to a TerritoryVersion.
         * @param {object} models - The models to associate with.
         */
        TerritoryVersionChild.belongsTo(models.TerritoryVersion, { foreignKey: 'territoryversion_id', as: 'territoryversion', onDelete: 'CASCADE' });

        /**
         * Associates the TerritoryVersionChild model with the Company model.
         * A TerritoryVersionChild belongs to a Company.
         * @param {object} models - The models to associate with.
         */
        TerritoryVersionChild.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });

        /**
         * Associates the TerritoryVersionChild model with the Territory model.
         * A TerritoryVersionChild belongs to a Territory.
         * @param {object} models - The models to associate with.
         * @description
         *   This association is used to define the parent-children relationship between territories.
         *   The parent territory is the territory that contains this territory version child.
         *   The parent territory is used to generate the territory hierarchy.
         */
        TerritoryVersionChild.belongsTo(models.Territory, { as: 'parent', foreignKey: 'parent_id' });

        /**
         * Associates the TerritoryVersionChild model with the Territory model.
         * A TerritoryVersionChild belongs to a Territory.
         * @param {object} models - The models to associate with.
         * @description
         *   This association is used to define the parent-children relationship between territories.
         *   The parent territory is the territory that contains this territory version child.
         *   The parent territory is used to generate the territory hierarchy.
         */
        TerritoryVersionChild.belongsTo(models.Territory, { as: 'territory', foreignKey: 'territory_id' });
    };

    return TerritoryVersionChild;
};
