<?php

class historique
{
    public static function recRef(string $_name) {
        db::query("INSERT IGNORE INTO " . DB_T_HISTORIQUE_REF . " (name) VALUES (:name)");
        db::bind(':name', $_name);
        try {
            db::execute();
        } catch (Exception $ex) {
            alert::recError("Erreur lors de l'enregistrement de la référence historique");
        }
    }

    public static function getIdRef(string $_name) {
        db::query("SELECT id FROM " . DB_T_HISTORIQUE_REF . " WHERE name = :name");
        db::bind(':name', $_name);
        return db::single()["id"];
    }

    public static function getAll() {
        db::query("SELECT "
                . "" . DB_T_HISTORIQUE . ".id, "
                . "" . DB_T_HISTORIQUE . ".idType ,"
                . "HISTO_TYPE.name AS type ,"
                . "" . DB_T_HISTORIQUE . ".idPage, "
                . "HISTO_PAGE.name AS page, "
                . "" . DB_T_HISTORIQUE . ".idUser ,"
                . "COALESCE(CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom), 'CMS System') AS 'user' ,"
                . "" . DB_T_HISTORIQUE . ".log, "
                . "" . DB_T_HISTORIQUE . ".addDate "
                . "FROM " . DB_T_HISTORIQUE . " "
                . "LEFT JOIN " . DB_T_USER . " ON " . DB_T_HISTORIQUE . ".idUser = " . DB_T_USER . ".id "
                . "INNER JOIN " . DB_T_HISTORIQUE_REF . " HISTO_TYPE ON " . DB_T_HISTORIQUE . ".idType = HISTO_TYPE.id "
                . "INNER JOIN " . DB_T_HISTORIQUE_REF . " HISTO_PAGE ON " . DB_T_HISTORIQUE . ".idPage = HISTO_PAGE.id ");
        return db::resultset();
    }

    public static function getByPage(string $_idPage) {
        db::query("SELECT "
                . "" . DB_T_HISTORIQUE . ".id, "
                . "" . DB_T_HISTORIQUE . ".idType ,"
                . "HISTO_TYPE.name AS type ,"
                . "" . DB_T_HISTORIQUE . ".idPage, "
                . "HISTO_PAGE.name AS page, "
                . "" . DB_T_HISTORIQUE . ".idUser ,"
                . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user' ,"
                . "" . DB_T_HISTORIQUE . ".log, "
                . "" . DB_T_HISTORIQUE . ".addDate "
                . "FROM " . DB_T_HISTORIQUE . " "
                . "INNER JOIN " . DB_T_USER . " ON " . DB_T_HISTORIQUE . ".idUser = " . DB_T_USER . ".id "
                . "INNER JOIN " . DB_T_HISTORIQUE_REF . " HISTO_TYPE ON " . DB_T_HISTORIQUE . ".idType = HISTO_TYPE.id "
                . "INNER JOIN " . DB_T_HISTORIQUE_REF . " HISTO_PAGE ON " . DB_T_HISTORIQUE . ".idPage = HISTO_PAGE.id "
                . "WHERE " . DB_T_HISTORIQUE . ".idPage = :idPage");
        db::bind(':idPage', $_idPage);
        return db::resultset();
    }

    public static function getByUser(int $_idUser) {
        db::query("SELECT "
                . "" . DB_T_HISTORIQUE . ".id, "
                . "" . DB_T_HISTORIQUE . ".type ,"
                . "" . DB_T_HISTORIQUE . ".idPage, "
                . "" . DB_T_HISTORIQUE . ".idUser ,"
                . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user' ,"
                . "" . DB_T_HISTORIQUE . ".log, "
                . "" . DB_T_HISTORIQUE . ".addDate "
                . "FROM " . DB_T_HISTORIQUE . " "
                . "WHERE " . DB_T_HISTORIQUE . ".idUser = :idUser");
        db::bind(':idUser', $_idUser);
        return db::resultset();
    }

    public static function getByType(string $_type) {
        db::query("SELECT "
                . "" . DB_T_HISTORIQUE . ".id, "
                . "" . DB_T_HISTORIQUE . ".idType ,"
                . "HISTO_TYPE.name AS type ,"
                . "" . DB_T_HISTORIQUE . ".idPage, "
                . "HISTO_PAGE.name AS page, "
                . "" . DB_T_HISTORIQUE . ".idUser ,"
                . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user' ,"
                . "" . DB_T_HISTORIQUE . ".log, "
                . "" . DB_T_HISTORIQUE . ".addDate "
                . "FROM " . DB_T_HISTORIQUE . " "
                . "INNER JOIN " . DB_T_USER . " ON " . DB_T_HISTORIQUE . ".idUser = " . DB_T_USER . ".id "
                . "INNER JOIN " . DB_T_HISTORIQUE_REF . " HISTO_TYPE ON " . DB_T_HISTORIQUE . ".idType = HISTO_TYPE.id "
                . "INNER JOIN " . DB_T_HISTORIQUE_REF . " HISTO_PAGE ON " . DB_T_HISTORIQUE . ".idPage = HISTO_PAGE.id "
                . "WHERE " . DB_T_HISTORIQUE . ".type = :type");
        db::bind(':type', $_type);
        return db::resultset();
    } 

    public static function recordLogs(){
        if(session::isConnect() AND (alert::getSuccess() OR alert::getWarning() OR alert::getError())){
            if(alert::getSuccess() AND core::getConfig("LOG_SUCCESS") == 1){
                self::recordAlert("SUCCESS");
            }
            if(alert::getWarning() AND core::getConfig("LOG_WARNING") == 1){
                self::recordAlert("WARNING");
            }
            if(alert::getError() AND core::getConfig("LOG_ERROR") == 1){
                self::recordAlert("ERROR");
            }
        }
    }

    public static function recordAlert(string $_type){
        $log = NULL;
        switch ($_type) {
            case 'SUCCESS':
                $alert = alert::getSuccess();
                break;
            case 'WARNING':
                $alert = alert::getWarning();
                break;
            case 'ERROR':
                $alert = alert::getError();
                break;
            default:
                $alert = NULL;
                alert::recError("Erreur lors de l'enregistrement de l'historique");
                break;
        }

        if($alert != NULL){
            foreach ($alert as $value) { 
                $log .= $value . "<br />";
            }
            self::recRef($_SERVER["REQUEST_URI"]);
            self::add(array(
                "idType" => self::getIdRef($_type),
                "idUser" => session::getId(),
                "idPage" => self::getIdRef($_SERVER["REQUEST_URI"]),
                "log" => $log
            ));
        }
    }

    public static function add(array $_input){
        db::query("INSERT INTO " . DB_T_HISTORIQUE . " (idType, idUser, idPage, log) VALUES (:idType, :idUser, :idPage, :log)");
        db::bind(':idType', $_input["idType"]);
        db::bind(':idUser', $_input["idUser"]);
        db::bind(':idPage', $_input["idPage"]);
        db::bind(':log', $_input["log"]);
        try {
            db::execute();
        } catch (Exception $ex) {
            alert::recError("Erreur lors de l'enregistrement de l'historique");
        }
    }
}