const { Model, DataTypes } = require('sequelize');
const sequelize = require('../config/db.js');
const GroupModules = require('./group_modules.js');
const { sanitizeSort, sanitizePagination } = require('./querySafety.js');

class Module extends Model {

    static associate(models) {

        Module.belongsToMany(models.Group, {
            through: GroupModules,
            foreignKey: "moduleId",
            otherKey: "groupId"
        });
    }

    async createModule(data) {
        try {
            const module = await Module.create(data)
            console.log(module.toJSON());
            return module.toJSON();
        }
        catch (err) {
            console.log(err);
            return err;
        }
    }
    
    async readModule(ordCol, ordDir, offsetVal, limitVal) {
        try {
            const [safeOrderCol, safeOrderDir] = sanitizeSort(ordCol, ordDir, [
                'id', 'name', 'description', 'status', 'createdAt', 'updatedAt'
            ]);
            const { safeOffset, safeLimit } = sanitizePagination(offsetVal, limitVal);
            const module = await Module.findAll(
                {
                    order: [[safeOrderCol, safeOrderDir]],
                    offset: safeOffset,
                    limit: safeLimit
                }
            );
            console.log(module.map(m => m.toJSON()));
            return module.map(m => m.toJSON());
        }
        catch (err) {
            console.log(err);
            return err;
        }
    }

    async readModuleRow(data) {
        try {
            const module = await Module.findAll(
                {
                    where: {
                        id: data.id
                    }
                }
            )
            console.log(module.map(m => m.toJSON()));
            return module.map(m => m.toJSON());
        }
        catch (err) {
            console.log(err);
            return err;
        }
    } 

    async updateModule(data) {
        try {
            const result = await Module.update(data.data, {where: {id: data.id}});
            console.log(`Module with ${data.id} updated`, result[0]);
            return result[0];
        }
        catch (err) {
            console.log(err);
            return err;
        }
    }

    async deleteModule(data) {
        try {
            const result = await Module.destroy(
                {
                    where: {id: data.id}
                }
            );

            if (result) {
                console.log(`Module ${data.id} deleted`);
                return `Module ${data.id} deleted`;
            }
            else {
                console.log(`Module ${data.id} not found`);
                return `Module ${data.id} not found`;
            }
        }
        catch (err) {
            console.log(err);
            return err;
        }
    }

    async countModule() {
        try {
            const count = await Module.count();
            console.log(count);
            return count;
        }
        catch (err) {
            console.log(err);
            return err;
        }
    }
}

Module.init(
    {
        name: {
            type: DataTypes.STRING(50),
            allowNull: false
        },
        description: {
            type: DataTypes.STRING(250),
            allowNull: false
        },
        status: {
            type: DataTypes.STRING(25),
            allowNull: false
        }
    },
    {
        sequelize,
        modelName: "Module",
        tableName: "modules"
    }
)

module.exports = Module;