<?php

namespace Korkz\Demirteknoloji\Controllers\Admin;

use Exception;
use JetBrains\PhpStorm\NoReturn;
use Korkz\Demirteknoloji\Models\Admin\AuthModel;
use Korkz\Demirteknoloji\Router\View;
use Korkz\Demirteknoloji\traits\AuthControl;
use Korkz\Demirteknoloji\traits\Lang;
use Korkz\Demirteknoloji\traits\Settings;
use Korkz\Demirteknoloji\traits\TokenAndValidControl;
use Korkz\Demirteknoloji\traits\TokenConfig;

class AuthController extends AuthModel
{
    use AuthControl;
    use Lang;
    use TokenConfig;
    use TokenAndValidControl;
    use Settings;

    /**
     * @throws Exception
     */
    public function show(string|array $paths, $prefix): void
    {
        if (isset($_COOKIE["Kadi"]) && isset($_COOKIE["Pass"])) {
            $Kadi = $this->openKey($_COOKIE["Kadi"]);
            $Pass = $this->openKey($_COOKIE["Pass"]);
        } else {
            $Kadi = "";
            $Pass = "";
        }
        $baseUrl = $this->baseUrl();
        $lang = $this->getLang();
        self::checkAccess($paths, $prefix);
        echo $this->views(["baseUrl" => $baseUrl, "tittle" => " - Demirteknoloji yönetim paneli", "token" => $this->tokenCreate(), "lang" => $this->getLang(), "Kadi" => $Kadi, "Pass" => $Pass, "data" => ["settings" => $this->settingData($lang)]]);

    }

    /**
     * @param array $dataBinds
     * @return array|string
     */
    private function views(array $dataBinds = []): array|string
    {

        ob_start();
        $lang = "";
        if (!empty($dataBinds)) {
            foreach ($dataBinds as $key => $dataBind) {
                View::bind($key, $dataBind);
                if (str_starts_with($key, "lang")) {
                    $lang = $dataBind;
                }
            }
        }
        View::render("admin/login");
        $content = ob_get_clean();
        return $this->langViews("admin", "login", $content, $lang);
    }

    /**
     * @throws Exception
     */
    #[NoReturn] public function login($request): void
    {
        if (gettype($request) === "object") {
            $username = htmlspecialchars($request->username);
            $password = htmlspecialchars($request->password);
            $token = htmlspecialchars($request->token);
            $remember_me = is_null($request->remember_me) ? $request->remember_me : htmlspecialchars($request->remember_me);
        } else {
            $username = htmlspecialchars($request["username"]);
            $password = htmlspecialchars($request["password"]);
            $token = htmlspecialchars($request["token"]);
            $remember_me = htmlspecialchars($request["remember_me"]);
        }
        $request = ["username" => $username, "password" => $password, "token" => $token, "operation" => "login", "location" => "admin", "page" => "login", "lang" => $this->getLang()];
        list($await, $result) = $this->tokenAndValidControl($request);
        if ($await) {
            $url = $this->baseUrl();
            $remember_me = !empty($remember_me);
            $backUrl = !empty($_GET["redirect"]) ? htmlspecialchars($_GET["redirect"]) : $url . "admin";
            $result = [...$this->auth($username, $password, $remember_me), "url" => $backUrl, "timeout" => 2000];
            echo json_encode($result);
            exit();

        }
        echo json_encode($result);
        exit();
    }

    /**
     * @return void
     */
    #[NoReturn] public function logout(): void
    {
        $user["users"] = ["userCode" => $this->openKey($_SESSION["userCode"]), "process" => "Çıkış İşlemi Gerçekleştirildi"];
        $this->log(["request" => $user], "logout");
        unset($_SESSION["userCode"]);
        header("Location:./");
        exit();
    }

}
