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

module.exports = (sequelize) => {
    const Project = sequelize.define('project', {
        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 },
        
        name: { type: DataTypes.STRING(100), allowNull: false, unique: true },
        display_name: { type: DataTypes.STRING(150), allowNull: true, unique: true },
        slug_name: { type: DataTypes.STRING(100), allowNull: true, unique: true },
        project_logo: { type: DataTypes.STRING, allowNull: true },
        project_logo_secondary: { type: DataTypes.STRING, allowNull: true },
        project_logo_navbar: { type: DataTypes.STRING, allowNull: true },
        project_url: { type: DataTypes.STRING(100), allowNull: true },
        opener_description: { type: DataTypes.TEXT, allowNull: true },
        about_description: { type: DataTypes.TEXT, allowNull: true },
        feature_description: { type: DataTypes.TEXT, allowNull: true },
    }, {
        tableName: 'projects',
    }, {
        freezeTableName: true
    });

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

        /**
         * Associates the Project model with the EmailAccount model.
         * Defines a many-to-many relationship between Project and EmailAccount.
         * @param {object} models - The models to associate with.
         */
        Project.belongsToMany(models.EmailAccount, { through: models.EmailAccountProject, foreignKey: 'project_id', otherKey: 'emailaccount_id', as: 'emailaccounts' });

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

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

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

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

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

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

    return Project;
};
