const config = require('config.json');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const db = require('helpers/db');
const { Op, QueryTypes } = require('sequelize');

module.exports = {
    getAll,
    getFilter,
    getOne, 
    getUDKbyUDKid, 
    getName,
    getOption,
    getCount,
    create,
    update,
    updateByDocType,
    delete: _delete
};

// ============================= || FUNCTION || ============================= //

async function getAll() {
    const udks = await db.ASA_Udk.findAll({
        order: [
            ['modified', 'DESC']
        ],
    });

    const success = true;
    const message = "Loaded Successfully!";

    return  { udks: udks, success, message };
}

async function getFilter(params) {
    const filterConditions = {};

    if (params.start_date && params.end_date) {
        filterConditions.date = {
            [Op.between]: [params.start_date, params.end_date]
        };
    }

    const udks = await db.ASA_Udk.findAll({
        where: filterConditions,
        order: [
            ['modified', 'DESC']
        ],
        
    });

    const success = true;
    const message = "Loaded Successfully!";

    return  { udks: udks, success, message };
}

async function getOne(id) {
    const udk = await getUdk(id); 

    const success = true;
    const message = "Loaded Successfully!";

    return  { udk: udk, success, message };
}

async function getUDKbyUDKid(id) {
    const udk = await getUdkByDocType(id); 

    const success = true;
    const message = "Loaded Successfully!";

    return  { udk: udk, success, message };
}

async function getName(id) {
    const udk = await db.ASA_Udk.findAll({
        attributes: ['no_udk'],
        where: { 
            id: id 
        },
    });

    const success = true;
    const message = "Loaded Successfully!";

    return  { udk: udk, success, message };
}

async function getOption() {
    const udks = await db.ASA_Udk.findAll({
        attributes: ['id', 'no_udk','customer','udk_id'],
        where: { 
            enabled: true 
        },
    });

    const success = true;
    const message = "Loaded Successfully!";

    return  { udks: udks, success, message };
}

async function getCount() {
    var udkCount = await db.ASA_Udk.count({ 
        where: { 
            enabled: true 
        } 
    })

    const success = true;
    const message = "Loaded Successfully!";

    // return
    return { count: udkCount, success, message};
}

async function create(params) {
    // validate
    if (await db.ASA_Udk.findOne({ where: { no_udk: params.no_udk } })) {
        throw 'UDK Number"' + params.no_udk + '"is already taken';
    }

    // default params
    if (!params.enabled) {
        params.enabled = true;
    }

    // save
    await db.ASA_Udk.create(params);

    // default format return
    // const udks = await db.ASA_Udk.findAll({
    //     order: [
    //         ['modified', 'DESC']
    //     ],
    // });
    
    const success = true;
    const message = "Your data has been saved!";

    // return
    return  { success, message }
}

async function update(id, params) {
    const udk = await getUdk(id);

    // validate
    const udkChanged = params.no_udk && udk.no_udk !== params.no_udk;
    if (udkChanged && await db.ASA_Udk.findOne({ where: { no_udk: params.no_udk } })) {
        throw 'UDK' + params.no_udk + '" is already taken. Try again!';
    }
    // copy params to user and save
    Object.assign(udk, params);
    await udk.save();

    // default format return
    // const udks = await db.ASA_Udk.findAll({
    //     order: [
    //         ['modified', 'DESC']
    //     ],
    // });

    const success = true;
    const message = "Your data has been saved!";

    // return
    return  { success, message }
}

async function updateByDocType(id, params) {
    const udk = await getUdkByDocType(id, params);;

    // validate
    const udkChanged = params.no_udk && udk.no_udk !== params.no_udk;
    if (udkChanged && await db.ASA_Udk.findOne({ where: { no_udk: params.no_udk } })) {
        throw 'UDK' + params.no_udk + '" is already taken. Try again!';
    }
    // copy params to user and save
    Object.assign(udk, params);
    await udk.save();

    // default format return
    // const udks = await db.ASA_Udk.findAll({
    //     order: [
    //         ['modified', 'DESC']
    //     ],
    // });

    const success = true;
    const message = "Your data has been saved!";

    // return
    return  { success, message }
}

async function _delete(id) {
    const udk = await getUdk(id);
    await udk.destroy();

    // const udks = await db.ASA_Udk.findAll({
    //     order: [
    //         ['modified', 'DESC']
    //     ],
    // });

    const success = true;
    const message = "Your data has been deleted!";

    return  { success, message }
}

// ============================= || HELPER FUNCTION || ============================= //

async function getUdk(id) {
    const udk = await db.ASA_Udk.findByPk(id);
    if (!udk) throw 'Udk not found, please kindly check!';
    return udk;
}
async function getUdkByDocType(id) {
    const udk = await db.ASA_Udk.findOne({
        where: { udk_id: id }
    });
    if (!udk) throw 'Udk not found, please kindly check!';
    return udk;
}
