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

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

        const driver_compliances = await db.driver_compliances.create(
            {
                id: data.id || undefined,
        
        license_country: data.license_country
        ||
        null
            ,
            
        license_number: data.license_number
        ||
        null
            ,
            
        license_issued_at: data.license_issued_at
        ||
        null
            ,
            
        license_expires_at: data.license_expires_at
        ||
        null
            ,
            
        license_restrictions: data.license_restrictions
        ||
        null
            ,
            
        license_suspended: data.license_suspended
        ||
        false
        
            ,
            
        cpc_number: data.cpc_number
        ||
        null
            ,
            
        cpc_issued_at: data.cpc_issued_at
        ||
        null
            ,
            
        cpc_expires_at: data.cpc_expires_at
        ||
        null
            ,
            
        cpc_center: data.cpc_center
        ||
        null
            ,
            
        cpc_last_training_at: data.cpc_last_training_at
        ||
        null
            ,
            
        cpc_next_training_at: data.cpc_next_training_at
        ||
        null
            ,
            
        cpc_type: data.cpc_type
        ||
        null
            ,
            
        adr_number: data.adr_number
        ||
        null
            ,
            
        adr_expires_at: data.adr_expires_at
        ||
        null
            ,
            
        adr_tank: data.adr_tank
        ||
        false
        
            ,
            
        adr_packages: data.adr_packages
        ||
        false
        
            ,
            
        adr_last_course_at: data.adr_last_course_at
        ||
        null
            ,
            
        adr_next_course_at: data.adr_next_course_at
        ||
        null
            ,
            
        adr_restrictions: data.adr_restrictions
        ||
        null
            ,
            
        medical_cert_number: data.medical_cert_number
        ||
        null
            ,
            
        medical_issued_by: data.medical_issued_by
        ||
        null
            ,
            
        medical_expires_at: data.medical_expires_at
        ||
        null
            ,
            
        psych_test_number: data.psych_test_number
        ||
        null
            ,
            
        psych_expires_at: data.psych_expires_at
        ||
        null
            ,
            
        eye_exam_at: data.eye_exam_at
        ||
        null
            ,
            
        cardio_exam_at: data.cardio_exam_at
        ||
        null
            ,
            
        medical_restrictions: data.medical_restrictions
        ||
        null
            ,
            
        tacho_card_number: data.tacho_card_number
        ||
        null
            ,
            
        tacho_country: data.tacho_country
        ||
        null
            ,
            
        tacho_expires_at: data.tacho_expires_at
        ||
        null
            ,
            
        tacho_last_download_at: data.tacho_last_download_at
        ||
        null
            ,
            
        tacho_next_download_at: data.tacho_next_download_at
        ||
        null
            ,
            
        violations_count: data.violations_count
        ||
        null
            ,
            
        warnings_count: data.warnings_count
        ||
        null
            ,
            
        driving_ban: data.driving_ban
        ||
        false
        
            ,
            
        driving_ban_until: data.driving_ban_until
        ||
        null
            ,
            
            importHash: data.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
    },
        { transaction },
    );

        
        await driver_compliances.setDriver_profile( data.driver_profile || null, {
            transaction,
        });
        

        

        

        return driver_compliances;
    }
    

    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 driver_compliancesData = data.map((item, index) => ({
                id: item.id || undefined,
                
                license_country: item.license_country
            ||
            null
            ,
            
                license_number: item.license_number
            ||
            null
            ,
            
                license_issued_at: item.license_issued_at
            ||
            null
            ,
            
                license_expires_at: item.license_expires_at
            ||
            null
            ,
            
                license_restrictions: item.license_restrictions
            ||
            null
            ,
            
                license_suspended: item.license_suspended
            ||
            false
        
            ,
            
                cpc_number: item.cpc_number
            ||
            null
            ,
            
                cpc_issued_at: item.cpc_issued_at
            ||
            null
            ,
            
                cpc_expires_at: item.cpc_expires_at
            ||
            null
            ,
            
                cpc_center: item.cpc_center
            ||
            null
            ,
            
                cpc_last_training_at: item.cpc_last_training_at
            ||
            null
            ,
            
                cpc_next_training_at: item.cpc_next_training_at
            ||
            null
            ,
            
                cpc_type: item.cpc_type
            ||
            null
            ,
            
                adr_number: item.adr_number
            ||
            null
            ,
            
                adr_expires_at: item.adr_expires_at
            ||
            null
            ,
            
                adr_tank: item.adr_tank
            ||
            false
        
            ,
            
                adr_packages: item.adr_packages
            ||
            false
        
            ,
            
                adr_last_course_at: item.adr_last_course_at
            ||
            null
            ,
            
                adr_next_course_at: item.adr_next_course_at
            ||
            null
            ,
            
                adr_restrictions: item.adr_restrictions
            ||
            null
            ,
            
                medical_cert_number: item.medical_cert_number
            ||
            null
            ,
            
                medical_issued_by: item.medical_issued_by
            ||
            null
            ,
            
                medical_expires_at: item.medical_expires_at
            ||
            null
            ,
            
                psych_test_number: item.psych_test_number
            ||
            null
            ,
            
                psych_expires_at: item.psych_expires_at
            ||
            null
            ,
            
                eye_exam_at: item.eye_exam_at
            ||
            null
            ,
            
                cardio_exam_at: item.cardio_exam_at
            ||
            null
            ,
            
                medical_restrictions: item.medical_restrictions
            ||
            null
            ,
            
                tacho_card_number: item.tacho_card_number
            ||
            null
            ,
            
                tacho_country: item.tacho_country
            ||
            null
            ,
            
                tacho_expires_at: item.tacho_expires_at
            ||
            null
            ,
            
                tacho_last_download_at: item.tacho_last_download_at
            ||
            null
            ,
            
                tacho_next_download_at: item.tacho_next_download_at
            ||
            null
            ,
            
                violations_count: item.violations_count
            ||
            null
            ,
            
                warnings_count: item.warnings_count
            ||
            null
            ,
            
                driving_ban: item.driving_ban
            ||
            false
        
            ,
            
                driving_ban_until: item.driving_ban_until
            ||
            null
            ,
            
            importHash: item.importHash || null,
            createdById: currentUser.id,
            updatedById: currentUser.id,
            createdAt: new Date(Date.now() + index * 1000),
    }));

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

        // For each item created, replace relation files
        

        return driver_compliances;
    }

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

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


        

        const updatePayload = {};
        
        if (data.license_country !== undefined) updatePayload.license_country = data.license_country;
        
        
        if (data.license_number !== undefined) updatePayload.license_number = data.license_number;
        
        
        if (data.license_issued_at !== undefined) updatePayload.license_issued_at = data.license_issued_at;
        
        
        if (data.license_expires_at !== undefined) updatePayload.license_expires_at = data.license_expires_at;
        
        
        if (data.license_restrictions !== undefined) updatePayload.license_restrictions = data.license_restrictions;
        
        
        if (data.license_suspended !== undefined) updatePayload.license_suspended = data.license_suspended;
        
        
        if (data.cpc_number !== undefined) updatePayload.cpc_number = data.cpc_number;
        
        
        if (data.cpc_issued_at !== undefined) updatePayload.cpc_issued_at = data.cpc_issued_at;
        
        
        if (data.cpc_expires_at !== undefined) updatePayload.cpc_expires_at = data.cpc_expires_at;
        
        
        if (data.cpc_center !== undefined) updatePayload.cpc_center = data.cpc_center;
        
        
        if (data.cpc_last_training_at !== undefined) updatePayload.cpc_last_training_at = data.cpc_last_training_at;
        
        
        if (data.cpc_next_training_at !== undefined) updatePayload.cpc_next_training_at = data.cpc_next_training_at;
        
        
        if (data.cpc_type !== undefined) updatePayload.cpc_type = data.cpc_type;
        
        
        if (data.adr_number !== undefined) updatePayload.adr_number = data.adr_number;
        
        
        if (data.adr_expires_at !== undefined) updatePayload.adr_expires_at = data.adr_expires_at;
        
        
        if (data.adr_tank !== undefined) updatePayload.adr_tank = data.adr_tank;
        
        
        if (data.adr_packages !== undefined) updatePayload.adr_packages = data.adr_packages;
        
        
        if (data.adr_last_course_at !== undefined) updatePayload.adr_last_course_at = data.adr_last_course_at;
        
        
        if (data.adr_next_course_at !== undefined) updatePayload.adr_next_course_at = data.adr_next_course_at;
        
        
        if (data.adr_restrictions !== undefined) updatePayload.adr_restrictions = data.adr_restrictions;
        
        
        if (data.medical_cert_number !== undefined) updatePayload.medical_cert_number = data.medical_cert_number;
        
        
        if (data.medical_issued_by !== undefined) updatePayload.medical_issued_by = data.medical_issued_by;
        
        
        if (data.medical_expires_at !== undefined) updatePayload.medical_expires_at = data.medical_expires_at;
        
        
        if (data.psych_test_number !== undefined) updatePayload.psych_test_number = data.psych_test_number;
        
        
        if (data.psych_expires_at !== undefined) updatePayload.psych_expires_at = data.psych_expires_at;
        
        
        if (data.eye_exam_at !== undefined) updatePayload.eye_exam_at = data.eye_exam_at;
        
        
        if (data.cardio_exam_at !== undefined) updatePayload.cardio_exam_at = data.cardio_exam_at;
        
        
        if (data.medical_restrictions !== undefined) updatePayload.medical_restrictions = data.medical_restrictions;
        
        
        if (data.tacho_card_number !== undefined) updatePayload.tacho_card_number = data.tacho_card_number;
        
        
        if (data.tacho_country !== undefined) updatePayload.tacho_country = data.tacho_country;
        
        
        if (data.tacho_expires_at !== undefined) updatePayload.tacho_expires_at = data.tacho_expires_at;
        
        
        if (data.tacho_last_download_at !== undefined) updatePayload.tacho_last_download_at = data.tacho_last_download_at;
        
        
        if (data.tacho_next_download_at !== undefined) updatePayload.tacho_next_download_at = data.tacho_next_download_at;
        
        
        if (data.violations_count !== undefined) updatePayload.violations_count = data.violations_count;
        
        
        if (data.warnings_count !== undefined) updatePayload.warnings_count = data.warnings_count;
        
        
        if (data.driving_ban !== undefined) updatePayload.driving_ban = data.driving_ban;
        
        
        if (data.driving_ban_until !== undefined) updatePayload.driving_ban_until = data.driving_ban_until;
        
        
        updatePayload.updatedById = currentUser.id;

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

        
        
        if (data.driver_profile !== undefined) {
            await driver_compliances.setDriver_profile(
              
              data.driver_profile,
              
              { transaction }
            );
        }
        

        
        

        

        return driver_compliances;
    }

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

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

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


        return driver_compliances;
    }

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

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

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

        await driver_compliances.destroy({
            transaction
        });

        return driver_compliances;
    }

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

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

        if (!driver_compliances) {
            return driver_compliances;
        }

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

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        output.driver_profiles_compliance = await driver_compliances.getDriver_profiles_compliance({
            transaction
        });
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        output.driver_profile = await driver_compliances.getDriver_profile({
            transaction
        });
        
        

        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 = [

      {
        model: db.driver_profiles,
        as: 'driver_profile',
        
        where: filter.driver_profile ? {
          [Op.or]: [
            { id: { [Op.in]: filter.driver_profile.split('|').map(term => Utils.uuid(term)) } },
            {
              driver_notes: {
                [Op.or]: filter.driver_profile.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
              }
            },
          ]
        } : {},
        
      },



    ];

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

            
            if (filter.license_country) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'license_country',
                        filter.license_country,
                    ),
                };
            }
            
            if (filter.license_number) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'license_number',
                        filter.license_number,
                    ),
                };
            }
            
            if (filter.license_restrictions) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'license_restrictions',
                        filter.license_restrictions,
                    ),
                };
            }
            
            if (filter.cpc_number) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'cpc_number',
                        filter.cpc_number,
                    ),
                };
            }
            
            if (filter.cpc_center) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'cpc_center',
                        filter.cpc_center,
                    ),
                };
            }
            
            if (filter.cpc_type) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'cpc_type',
                        filter.cpc_type,
                    ),
                };
            }
            
            if (filter.adr_number) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'adr_number',
                        filter.adr_number,
                    ),
                };
            }
            
            if (filter.adr_restrictions) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'adr_restrictions',
                        filter.adr_restrictions,
                    ),
                };
            }
            
            if (filter.medical_cert_number) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'medical_cert_number',
                        filter.medical_cert_number,
                    ),
                };
            }
            
            if (filter.medical_issued_by) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'medical_issued_by',
                        filter.medical_issued_by,
                    ),
                };
            }
            
            if (filter.psych_test_number) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'psych_test_number',
                        filter.psych_test_number,
                    ),
                };
            }
            
            if (filter.medical_restrictions) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'medical_restrictions',
                        filter.medical_restrictions,
                    ),
                };
            }
            
            if (filter.tacho_card_number) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'tacho_card_number',
                        filter.tacho_card_number,
                    ),
                };
            }
            
            if (filter.tacho_country) {
                where = {
                    ...where,
                    [Op.and]: Utils.ilike(
                        'driver_compliances',
                        'tacho_country',
                        filter.tacho_country,
                    ),
                };
            }
            

            
            

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
            if (filter.license_suspended) {
                where = {
                    ...where,
                license_suspended: filter.license_suspended,
            };
            }
            
            if (filter.adr_tank) {
                where = {
                    ...where,
                adr_tank: filter.adr_tank,
            };
            }
            
            if (filter.adr_packages) {
                where = {
                    ...where,
                adr_packages: filter.adr_packages,
            };
            }
            
            if (filter.driving_ban) {
                where = {
                    ...where,
                driving_ban: filter.driving_ban,
            };
            }
            


      



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

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

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

    
};

