<?php

namespace Korkz\Demirteknoloji\Models\Admin;

use Exception;
use Korkz\Demirteknoloji\Connect\DbOperations;
use Korkz\Demirteknoloji\traits\Lang;
use Korkz\Demirteknoloji\traits\Logs;
use Korkz\Demirteknoloji\traits\Seo;
use Korkz\Demirteknoloji\traits\TokenConfig;

class AuthModel extends DbOperations
{
    use TokenConfig;
    use Lang;
    use Seo;
    use Logs;

    /**
     * @param $username
     * @param $password
     * @param $remember_me
     * @return string[]
     * @throws Exception
     */
    protected function auth($username, $password, $remember_me): array
    {
        $reply = $this->langReply("admin", "login", $_SESSION["lang"] ?? "tr");
        $auth = $this->userAuthControl($username, md5($password));
        if ($auth["boolean"]) {

            $_SESSION['userCode'] = $this->locKey("key", $auth["userCode"]); # session oluşturlur
            $URL = $_SERVER['HTTP_HOST'];
            if (!empty($remember_me)) { #cookie oluşturlur
                setcookie("Kadi", $this->locKey("key", $username), strtotime("+7 day"), "/", $URL, "https", "http");
                setcookie("Pass", $this->locKey("key", $password), strtotime("+7 day"), "/", $URL, "https", "http");

            } else { #cookie silinir
                setcookie("Kadi", $this->locKey("key", $username), strtotime("-7 day"), "/", $URL, "https", "http");
                setcookie("Pass", $this->locKey("key", $password), strtotime("-7 day"), "/", $URL, "https", "http");

            }
            $result = ["conclusion" => "success", "message" => $reply["success"]["messages"]];

        } else {
            $result = ["conclusion" => "error", "message" => $reply["errors"]["messages"]];
        }
        $this->log(["request" => ["username" => $username, "password" => md5($password)], "info" => $result], "login");
        return $result;
    }

    /**
     * @throws Exception
     */
    private function userAuthControl($username, $password): ?array
    {

        if ($this->getData("users")) {
            $user = $this->getData("users", ["userCode"], ["username_slug" => $this->seo($username), "password" => $password]);
            if ($user) {
                return ["boolean" => true, "userCode" => $user[0]["userCode"]];
            }
            return ["boolean" => false];
        } else { // hiç kullanıcı yoksa yeni kayıt oluşturulur burada
            $code = substr($this->generateCode(), 0, 10);
            $avatar = "https://ui-avatars.com/api/?name=$username&background=random&font-size=0.5&format=svg";
            $userCode = $this->uniqCode("users", "userCode", "userCode", $code);
            $request = ["userCode" => $userCode, "user_avatar" => $avatar, "username" => $username, "username_slug" => $this->seo($username), "password" => $password];
            $user = $this->insertData("users", $request);
            if ($user) {
                $this->log(["request" => $request, "info" => "Kullanıcı kaydı oluşturuldu"], "login");
                return $this->userAuthControl($username, $password);
            }
            return null;
        }

    }
}