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

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

        title: { type: DataTypes.STRING, allowNull: false },
        description: { type: DataTypes.TEXT, allowNull: true },
        aspect_description: { type: DataTypes.TEXT, allowNull: true },
        level_description: { type: DataTypes.TEXT, allowNull: true },
        task_description: { type: DataTypes.TEXT, allowNull: true },
        factor_description: { type: DataTypes.TEXT, allowNull: true },
        progression_table_description: { type: DataTypes.TEXT, allowNull: true },
        impact_description: { type: DataTypes.TEXT, allowNull: true },
        url: { type: DataTypes.TEXT, allowNull: true },
        icon: { type: DataTypes.TEXT, allowNull: true },

        // fk
        project_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'projects',
                key: 'id'
            },
            onDelete: 'CASCADE'
        },
    }, {
        indexes: [{ fields: ['project_id'] }],
    }, {
        tableName: 'developments',
    }, {
        freezeTableName: true
    });

    /**
     * Associates the Development model with other models.
     * @param {object} models - The models to associate with.
     */
    Development.associate = (models) => {
        /**
         * Associates the Development model with the Project model.
         * Defines a many-to-one relationship between Development and Project.
         * @param {object} models - The models to associate with.
         */
        Development.belongsTo(models.Project, { foreignKey: 'project_id', as: 'project' });

        /**
         * Associates the Development model with the Aspect model.
         * Defines a one-to-many relationship between Development and Aspect.
         * @param {object} models - The models to associate with.
         */
        Development.hasMany(models.Aspect, { foreignKey: 'development_id', as: 'aspects' });

        /**
         * Associates the Development model with the Factor model.
         * Defines a one-to-many relationship between Development and Factor.
         * @param {object} models - The models to associate with.
         */
        Development.hasMany(models.Factor, { foreignKey: 'development_id', as: 'factors' });

        /**
         * Associates the Development model with the ProgressionTable model.
         * Defines a one-to-many relationship between Development and ProgressionTable.
         * @param {object} models - The models to associate with.
         */
        Development.hasMany(models.ProgressionTable, { foreignKey: 'development_id', as: 'progressiontables' });

        /**
         * Associates the Development model with the Level model.
         * Defines a one-to-many relationship between Development and Level.
         * @param {object} models - The models to associate with.
         */
        Development.hasMany(models.Level, { foreignKey: 'development_id', as: 'levels' });

        /**
         * Associates the Development model with the Course model.
         * Defines a many-to-many relationship between Development and Course.
         * @param {object} models - The models to associate with.
         */
        Development.belongsToMany(models.Course, { through: models.CourseDevelopment, foreignKey: 'development_id', otherKey: 'course_id', as: 'courses' });
    };

    return Development;
};
