
const db = require('../models');
const FileDBApi = require('./file');
const crypto = require('crypto');
const Utils = require('../utils');



const Sequelize = db.Sequelize;
const Op = Sequelize.Op;

module.exports = class Sss_contribution_tablesDBApi {
    

    
    static async create(data, options) {
        const currentUser = (options && options.currentUser) || { id: null };
        const transaction = (options && options.transaction) || undefined;

        const sss_contribution_tables = await db.sss_contribution_tables.create(
            {
                id: data.id || undefined,
        
        salaryFrom: data.salaryFrom
        ||
        null
            ,
            
        salaryTo: data.salaryTo
        ||
        null
            ,
            
        msc: data.msc
        ||
        null
            ,
            
        employeeShare: data.employeeShare
        ||
        null
            ,
            
        employerShare: data.employerShare
        ||
        null
            ,
            
        wispEmployee: data.wispEmployee
        ||
        null
            ,
            
        wispEmployer: data.wispEmployer
        ||
        null
            ,
            
        effectiveYear: data.effectiveYear
        ||
        null
            ,
            
            importHash: data.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
    },
        { transaction },
    );

        

        

        

        return sss_contribution_tables;
    }
    

    static async bulkImport(data, options) {
        const currentUser = (options && options.currentUser) || { id: null };
        const transaction = (options && options.transaction) || undefined;

        // Prepare data - wrapping individual data transformations in a map() method
        const sss_contribution_tablesData = data.map((item, index) => ({
                id: item.id || undefined,
                
                salaryFrom: item.salaryFrom
            ||
            null
            ,
            
                salaryTo: item.salaryTo
            ||
            null
            ,
            
                msc: item.msc
            ||
            null
            ,
            
                employeeShare: item.employeeShare
            ||
            null
            ,
            
                employerShare: item.employerShare
            ||
            null
            ,
            
                wispEmployee: item.wispEmployee
            ||
            null
            ,
            
                wispEmployer: item.wispEmployer
            ||
            null
            ,
            
                effectiveYear: item.effectiveYear
            ||
            null
            ,
            
            importHash: item.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
            createdAt: new Date(Date.now() + index * 1000),
    }));

        // Bulk create items
        const sss_contribution_tables = await db.sss_contribution_tables.bulkCreate(sss_contribution_tablesData, { transaction });

        // For each item created, replace relation files
        

        return sss_contribution_tables;
    }

    static async update(id, data,  options) {
        const currentUser = (options && options.currentUser) || {id: null};
        const transaction = (options && options.transaction) || undefined;
        

        const sss_contribution_tables = await db.sss_contribution_tables.findByPk(id, {}, {transaction});


        

        const updatePayload = {};
        
        if (data.salaryFrom !== undefined) updatePayload.salaryFrom = data.salaryFrom;
        
        
        if (data.salaryTo !== undefined) updatePayload.salaryTo = data.salaryTo;
        
        
        if (data.msc !== undefined) updatePayload.msc = data.msc;
        
        
        if (data.employeeShare !== undefined) updatePayload.employeeShare = data.employeeShare;
        
        
        if (data.employerShare !== undefined) updatePayload.employerShare = data.employerShare;
        
        
        if (data.wispEmployee !== undefined) updatePayload.wispEmployee = data.wispEmployee;
        
        
        if (data.wispEmployer !== undefined) updatePayload.wispEmployer = data.wispEmployer;
        
        
        if (data.effectiveYear !== undefined) updatePayload.effectiveYear = data.effectiveYear;
        
        
        updatePayload.updatedById = currentUser.id;

        await sss_contribution_tables.update(updatePayload, {transaction});

        
        

        
        

        

        return sss_contribution_tables;
    }

    static async deleteByIds(ids, options) {
        const currentUser = (options && options.currentUser) || { id: null };
        const transaction = (options && options.transaction) || undefined;

        const sss_contribution_tables = await db.sss_contribution_tables.findAll({
            where: {
                id: {
                    [Op.in]: ids,
                },
            },
            transaction,
        });

        await db.sequelize.transaction(async (transaction) => {
            for (const record of sss_contribution_tables) {
                await record.update(
                    {deletedBy: currentUser.id},
                    {transaction}
                );
            }
            for (const record of sss_contribution_tables) {
                await record.destroy({transaction});
            }
        });


        return sss_contribution_tables;
    }

    static async remove(id, options) {
        const currentUser = (options && options.currentUser) || {id: null};
        const transaction = (options && options.transaction) || undefined;

        const sss_contribution_tables = await db.sss_contribution_tables.findByPk(id, options);

        await sss_contribution_tables.update({
            deletedBy: currentUser.id
        }, {
            transaction,
        });

        await sss_contribution_tables.destroy({
            transaction
        });

        return sss_contribution_tables;
    }

    static async findBy(where, options) {
        const transaction = (options && options.transaction) || undefined;

        const sss_contribution_tables = await db.sss_contribution_tables.findOne(
            { where },
            { transaction },
        );

        if (!sss_contribution_tables) {
            return sss_contribution_tables;
        }

        const output = sss_contribution_tables.get({plain: true});

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

        return output;
    }

    static async findAll(
          filter,
           options
        ) {
        const limit = filter.limit || 0;
        let offset = 0;
        let where = {};
        const currentPage = +filter.page;

        

        

        offset = currentPage * limit;

        const orderBy = null;

    const transaction = (options && options.transaction) || undefined;

    let include = [



    ];

        if (filter) {
            if (filter.id) {
                where = {
                    ...where,
                    ['id']: Utils.uuid(filter.id),
                };
            }

            

            
            

            
            if (filter.salaryFromRange) {
                const [start, end] = filter.salaryFromRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                    salaryFrom: {
                    ...where.salaryFrom,
                            [Op.gte]: start,
                    },
                };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                    salaryFrom: {
                    ...where.salaryFrom,
                            [Op.lte]: end,
                    },
                };
                }
            }
            
            if (filter.salaryToRange) {
                const [start, end] = filter.salaryToRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                    salaryTo: {
                    ...where.salaryTo,
                            [Op.gte]: start,
                    },
                };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                    salaryTo: {
                    ...where.salaryTo,
                            [Op.lte]: end,
                    },
                };
                }
            }
            
            if (filter.mscRange) {
                const [start, end] = filter.mscRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                    msc: {
                    ...where.msc,
                            [Op.gte]: start,
                    },
                };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                    msc: {
                    ...where.msc,
                            [Op.lte]: end,
                    },
                };
                }
            }
            
            if (filter.employeeShareRange) {
                const [start, end] = filter.employeeShareRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                    employeeShare: {
                    ...where.employeeShare,
                            [Op.gte]: start,
                    },
                };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                    employeeShare: {
                    ...where.employeeShare,
                            [Op.lte]: end,
                    },
                };
                }
            }
            
            if (filter.employerShareRange) {
                const [start, end] = filter.employerShareRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                    employerShare: {
                    ...where.employerShare,
                            [Op.gte]: start,
                    },
                };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                    employerShare: {
                    ...where.employerShare,
                            [Op.lte]: end,
                    },
                };
                }
            }
            
            if (filter.wispEmployeeRange) {
                const [start, end] = filter.wispEmployeeRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                    wispEmployee: {
                    ...where.wispEmployee,
                            [Op.gte]: start,
                    },
                };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                    wispEmployee: {
                    ...where.wispEmployee,
                            [Op.lte]: end,
                    },
                };
                }
            }
            
            if (filter.wispEmployerRange) {
                const [start, end] = filter.wispEmployerRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                    wispEmployer: {
                    ...where.wispEmployer,
                            [Op.gte]: start,
                    },
                };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                    wispEmployer: {
                    ...where.wispEmployer,
                            [Op.lte]: end,
                    },
                };
                }
            }
            
            if (filter.effectiveYearRange) {
                const [start, end] = filter.effectiveYearRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                    effectiveYear: {
                    ...where.effectiveYear,
                            [Op.gte]: start,
                    },
                };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                    effectiveYear: {
                    ...where.effectiveYear,
                            [Op.lte]: end,
                    },
                };
                }
            }
            

            if (filter.active !== undefined) {
                where = {
                    ...where,
                    active: filter.active === true || filter.active === 'true'
                };
            }

            




            if (filter.createdAtRange) {
                const [start, end] = filter.createdAtRange;

                if (start !== undefined && start !== null && start !== '') {
                    where = {
                        ...where,
                        ['createdAt']: {
                            ...where.createdAt,
                            [Op.gte]: start,
                        },
                    };
                }

                if (end !== undefined && end !== null && end !== '') {
                    where = {
                        ...where,
                        ['createdAt']: {
                            ...where.createdAt,
                            [Op.lte]: end,
                        },
                    };
                }
            }
        }
        

        

        const queryOptions = {
            where,
            include,
            distinct: true,
            order: filter.field && filter.sort
                ? [[filter.field, filter.sort]]
                : [['createdAt', 'desc']],
            transaction: options?.transaction,
            logging: console.log
        };

        if (!options?.countOnly) {
            queryOptions.limit = limit ? Number(limit) : undefined;
            queryOptions.offset = offset ? Number(offset) : undefined;
        }

        try {
            const { rows, count } = await db.sss_contribution_tables.findAndCountAll(queryOptions);

            return {
                rows: options?.countOnly ? [] : rows,
                count: count
            };
        } catch (error) {
            console.error('Error executing query:', error);
            throw error;
        }
    }

    static async findAllAutocomplete(query, limit, offset, ) {
        let where = {};
        
        

        if (query) {
            where = {
                [Op.or]: [
                    { ['id']: Utils.uuid(query) },
                    Utils.ilike(
                        'sss_contribution_tables',
                        'effectiveYear',
                        query,
                    ),
                ],
            };
        }

        const records = await db.sss_contribution_tables.findAll({
            attributes: [ 'id', 'effectiveYear' ],
            where,
            limit: limit ? Number(limit) : undefined,
            offset: offset ? Number(offset) : undefined,
            orderBy: [['effectiveYear', 'ASC']],
        });

        return records.map((record) => ({
            id: record.id,
            label: record.effectiveYear,
        }));
    }

    
};

