<?php

class json extends db
{

    public function __construct()
    {
    }

    public static function create(string $_target)
    {
        if (isset($_target)) {
            switch ($_target) {
                case "salaries":
                    return self::create_salaries();
                    break;
                case "excel":
                    return self::create_excel();
                    break;
                case "excel-proweb":
                    return self::create_excelProweb();
                    break;
                case "events":
                    return self::create_events();
                    break;
                case "users":
                    return self::create_users();
                    break;
                case "salaries-proweb":
                    return self::create_salariesProweb();
                    break;
                case "lotterys":
                    return self::create_lotterys();
                    break;
                case "banque-lignes-1":
                    return self::create_banque_lignes(1);
                    break;
                case "banque-lignes-2":
                    return self::create_banque_lignes(2);
                    break;
                case "banque-lignes-3":
                    return self::create_banque_lignes(3);
                    break;
                case "banque-lignes-4":
                    return self::create_banque_lignes(4);
                    break;
                case "banque-csv":
                    return self::create_banque_csv();
                    break;
                case "documents":
                    return self::create_document();
                    break;
                case "documents-limited":
                    return self::create_document("limited");
                    break;
            }
        } else {
            return 0;
        }
    }

    private static function create_salaries()
    {
        $row = salaries::getSalaries();
        if (file_put_contents(DIR_DATAS_JSON . "salaries.json", json_encode($row))) {
            return 1;
        } else {
            return 0;
        }
    }

    private static function create_salariesProweb()
    {
        $row = proweb::getBase();
        if (file_put_contents(DIR_DATAS_JSON . "salaries-proweb.json", json_encode($row))) {
            return 1;
        } else {
            return 0;
        }
    }

    private static function create_excel()
    {
        db::query("SELECT 
        " . DB_T_EXCEL . ".id, 
        " . DB_T_FILES . ".name, 
        " . DB_T_EXCEL . ".nbSalaries, 
        CONCAT(ROUND((" . DB_T_FILES . ".size / 1024 / 1024), 2), ' Mo') AS size, 
        " . DB_T_EXCEL . ".md5, 
        " . DB_T_EXCEL . ".cree, 
        CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user', 
        dateData, 
        goMysql, 
        md5forSFTP, 
        log 
        FROM " . DB_T_EXCEL . " 
        INNER JOIN " . DB_T_USER . " ON " . DB_T_EXCEL . ".id_user = " . DB_T_USER . ".id 
        LEFT JOIN " . DB_T_FILES . " ON " . DB_T_EXCEL . ".md5 = " . DB_T_FILES . ".id");
        $row = db::resultset();

        if (file_put_contents(DIR_DATAS_JSON . "excel.json", json_encode($row))) {
            return 1;
        } else {
            return 0;
        }
    }

    private static function create_excelProweb()
    {
        db::query("SELECT 
        " . DB_T_EXCEL_PROWEB . ".id, 
        " . DB_T_FILES . ".name, 
        " . DB_T_EXCEL_PROWEB . ".nbSalaries, 
        CONCAT(ROUND((" . DB_T_FILES . ".size / 1024 / 1024), 2), ' Mo') AS size, 
        " . DB_T_EXCEL_PROWEB . ".md5, 
        " . DB_T_EXCEL_PROWEB . ".cree, 
        CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user', 
        dateData 
        FROM " . DB_T_EXCEL_PROWEB . " 
        INNER JOIN " . DB_T_USER . " ON " . DB_T_EXCEL_PROWEB . ".id_user = " . DB_T_USER . ".id 
        LEFT JOIN " . DB_T_FILES . " ON " . DB_T_EXCEL_PROWEB . ".md5 = " . DB_T_FILES . ".id");
        $row = db::resultset();

        if (file_put_contents(DIR_DATAS_JSON . "excel-proweb.json", json_encode($row))) {
            return 1;
        } else {
            return 0;
        }
    }

    private static function create_events()
    {
        $row = event::getEvents();
        if (file_put_contents(DIR_DATAS_JSON . "events.json", json_encode($row))) {
            return 1;
        } else {
            return 0;
        }
    }

    private static function create_lotterys()
    {
        $row = lottery::getLotterys();
        if (file_put_contents(DIR_DATAS_JSON . "lotterys.json", json_encode($row))) {
            return 1;
        } else {
            return 0;
        }
    }

    private static function create_users()
    {
        $row = user::getUsers();
        if (file_put_contents(DIR_DATAS_JSON . "users.json", json_encode($row))) {
            return 1;
        } else {
            return 0;
        }
    }

    private static function create_banque_csv()
    {
        $row = banque::getHistoriqueCSV();
        if (file_put_contents(DIR_DATAS_JSON . "banque-csv.json", json_encode($row))) {
            return 1;
        } else {
            return 0;
        }
    }

    private static function create_banque_lignes(int $_id)
    {
        $row = array(banque::getInitial($_id));
        $row2 = banque::getLignes($_id, $row[0]["solde"]);
        $return = array_merge($row, $row2);

        if (file_put_contents(DIR_DATAS_JSON . "banque-lignes-" . $_id . ".json", json_encode($return))) {
            return 1;
        } else {
            return 0;
        }
    }

    public static function delete(string $_target)
    {
        if (is_file(DIR_DATAS_JSON . "/" . $_target . ".json")) {
            unlink(DIR_DATAS_JSON . "/" . $_target . ".json");
        }
    }

    public static function testSFTP()
    {
        return (sftp::testAccessHost()) ? "OK" : "KO";
    }

    private static function create_document(string $_limited = NULL)
    {
        $where = ($_limited == TRUE) ? " WHERE " . DB_T_DOCUMENTS . ".id_type != 3" : NULL;

        if(is_null($_limited)){
            $where = NULL;
            $file = "documents";
        } else {
            $where = " WHERE " . DB_T_DOCUMENTS . ".id_type != 3";
            $file = "documents-limited";
        }
        
        db::query("SELECT 
        " . DB_T_DOCUMENTS . ".id, 
        " . DB_T_DOCUMENTS . ".titre, 
        " . DB_T_DOCUMENTS . ".date,        
        " . DB_T_DOCUMENTS . ".deadline, 
        " . DB_T_DOCUMENTS . ".description, 
        " . DB_T_DOCUMENTS . ".montant, 
            ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ') 
            FROM  " . DB_T_DOCUMENT_TAGS . " 
            INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags
            WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 2 
            ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS tags, 
            ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ') 
            FROM  " . DB_T_DOCUMENT_TAGS . " 
            INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags
            WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 1 
            ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS assign, 
        IF(" . DB_T_DOCUMENTS . ".id_user_done IS NOT NULL, 'Traité', 'Non traité') AS done, 
        " . DB_T_TYPE_DOCUMENT . ".label
        FROM " . DB_T_DOCUMENTS . " 
        INNER JOIN " . DB_T_TYPE_DOCUMENT . " ON " . DB_T_TYPE_DOCUMENT . ".id = " . DB_T_DOCUMENTS . ".id_type" . $where);

        $row =  db::resultset();

        if (file_put_contents(DIR_DATAS_JSON . $file . ".json", json_encode($row))) {
            return 1;
        } else {
            return 0;
        }
    }
}
