<?php

namespace Korkz\Demirteknoloji\Models\Admin;

use Exception;
use Korkz\Demirteknoloji\Connect\DbOperations;
use Korkz\Demirteknoloji\traits\Seo;

class UserModel extends DbOperations
{
    use Seo;

    protected function editModel($userCode, $username, $pass): bool|Exception
    {
        try {
            $avatar = "https://ui-avatars.com/api/?name=$username&background=random&font-size=0.5&format=svg";
            if (!is_null($pass)) {
                $pass = htmlspecialchars($pass);
                $password = md5($pass);
                $data = ["user_avatar" => $avatar, "username" => $username, "username_slug" => $this->seo($username), "password" => $password, "updated_at" => date("Y-m-d H:i:s")];
            } else {
                $data = ["user_avatar" => $avatar, "username" => $username, "username_slug" => $this->seo($username), "updated_at" => date("Y-m-d H:i:s")];
            }
            $profileUpdate = $this->updateData("users", $data, ["userCode" => $userCode]);
            if ($profileUpdate) {
                return true;
            } else {
                return false;
            }

        } catch (Exception $exception) {
            return $exception;
        }
    }

    protected function userCreateModel($username, $pass): bool|Exception
    {
        try {
            $avatar = "https://ui-avatars.com/api/?name=$username&background=random&font-size=0.5&format=svg";
            $code = substr($this->generateCode(), 0, 10);
            $userCode = $this->uniqCode("users", "userCode", "userCode", $code);
            $pass = htmlspecialchars($pass);
            $password = md5($pass);
            $data = ["userCode" => $userCode, "username" => $username, "user_avatar" => $avatar, "username_slug" => $this->seo($username), "password" => $password];
            $profileUpdate = $this->insertData("users", $data);
            if ($profileUpdate) {
                return true;
            } else {
                return false;
            }
        } catch (Exception $exception) {
            return $exception;
        }
    }

}