<?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;
            }
        } else {
            return 0;
        }
    }

    private static function create_salaries()
    {
        $row = salaries::get_salaries();
        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_users()
    {
        $row = user::getUsers();
        if (file_put_contents(DIR_DATAS_JSON . "users.json", json_encode($row))) {
            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";
    }
}
