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

module.exports = (sequelize) => {
    const CompanySetting = sequelize.define(
        'companysetting',
        {
            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 },

            key: { type: DataTypes.STRING(100), allowNull: false },
            value: { type: DataTypes.TEXT("long"), allowNull: false },

            company_id: {
                type: DataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'companies',
                    key: 'id'
                }
            },
        },
        {
            tableName: 'companysettings',
        },
        {
            freezeTableName: true
        }
    );

    /**
     * Associates the CompanySetting model with other models.
     * @param {object} models - The models to associate with.
     * @description
     *   - A CompanySetting belongs to one Company.
     */
    CompanySetting.associate = (models) => {
        /**
         * Associates the CompanySetting model with the Company model.
         * @param {object} models - The models to associate with.
         * @description
         *   - A CompanySetting belongs to one Company.
         */
        CompanySetting.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    };

    return CompanySetting;
};
