
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 Cleaning_staffDBApi {
    

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

        const cleaning_staff = await db.cleaning_staff.create(
            {
                id: data.id || undefined,
        
        full_name: data.full_name
        ||
        null
            ,
            
        national_number: data.national_number
        ||
        null
            ,
            
        phone: data.phone
        ||
        null
            ,
            
        address: data.address
        ||
        null
            ,
            
        specialty: data.specialty
        ||
        null
            ,
            
        salary: data.salary
        ||
        null
            ,
            
        bonuses: data.bonuses
        ||
        null
            ,
            
        hire_date: data.hire_date
        ||
        null
            ,
            
            importHash: data.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
    },
        { transaction },
    );

        

        

        

        return cleaning_staff;
    }
    

    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 cleaning_staffData = data.map((item, index) => ({
                id: item.id || undefined,
                
                full_name: item.full_name
            ||
            null
            ,
            
                national_number: item.national_number
            ||
            null
            ,
            
                phone: item.phone
            ||
            null
            ,
            
                address: item.address
            ||
            null
            ,
            
                specialty: item.specialty
            ||
            null
            ,
            
                salary: item.salary
            ||
            null
            ,
            
                bonuses: item.bonuses
            ||
            null
            ,
            
                hire_date: item.hire_date
            ||
            null
            ,
            
            importHash: item.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
            createdAt: new Date(Date.now() + index * 1000),
    }));

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

        // For each item created, replace relation files
        

        return cleaning_staff;
    }

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

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


        

        const updatePayload = {};
        
        if (data.full_name !== undefined) updatePayload.full_name = data.full_name;
        
        
        if (data.national_number !== undefined) updatePayload.national_number = data.national_number;
        
        
        if (data.phone !== undefined) updatePayload.phone = data.phone;
        
        
        if (data.address !== undefined) updatePayload.address = data.address;
        
        
        if (data.specialty !== undefined) updatePayload.specialty = data.specialty;
        
        
        if (data.salary !== undefined) updatePayload.salary = data.salary;
        
        
        if (data.bonuses !== undefined) updatePayload.bonuses = data.bonuses;
        
        
        if (data.hire_date !== undefined) updatePayload.hire_date = data.hire_date;
        
        
        updatePayload.updatedById = currentUser.id;

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

        
        

        
        

        

        return cleaning_staff;
    }

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

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

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


        return cleaning_staff;
    }

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

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

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

        await cleaning_staff.destroy({
            transaction
        });

        return cleaning_staff;
    }

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

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

        if (!cleaning_staff) {
            return cleaning_staff;
        }

        const output = cleaning_staff.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.full_name) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'cleaning_staff',
                        'full_name',
                        filter.full_name,
                    ),
                };
            }
            
            if (filter.national_number) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'cleaning_staff',
                        'national_number',
                        filter.national_number,
                    ),
                };
            }
            
            if (filter.phone) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'cleaning_staff',
                        'phone',
                        filter.phone,
                    ),
                };
            }
            
            if (filter.address) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'cleaning_staff',
                        'address',
                        filter.address,
                    ),
                };
            }
            

            
            

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

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

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

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

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

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

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

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

            
            if (filter.specialty) {
                where = {
                    ...where,
                specialty: filter.specialty,
            };
            }
            




            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.cleaning_staff.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(
                        'cleaning_staff',
                        'full_name',
                        query,
                    ),
                ],
            };
        }

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

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

    
};

