<?php
/*
 * Copyright (c) 2022. Micha Espey <tracer@24unix.net>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */

namespace App\Controller;

use App\Entity\User;
use App\Service\Router;
use App\Service\Template;
use App\Repository\UserRepository;

class AddressBookAdminController
{
    public function __construct(
        private readonly Template       $template,
        private readonly User           $user,
        private readonly UserRepository $userRepository,
        private readonly Router         $router
    )
    {
    }

    private function adminCheck(): void
    {
        if (!$this->user->isAdmin()) {
            $this->template->render(templateName: 'status/403.html.php', vars: [
                'user'   => $this->user,
                'router' => $this->router
            ]);
        }
    }


    public function admin(): never
    {
        $this->adminCheck();
        $this->template->render(templateName: 'admin/index.html.php', vars: [
            'user'   => $this->user,
            'router' => $this->router
        ]);
    }


    public function adminUser(): never
    {
        $this->adminCheck();

        $users = $this->userRepository->findAll();

        $this->template->render(templateName: 'admin/users.html.php', vars: [
            'user'   => $this->user,
            'users'  => $users,
            'router' => $this->router
        ]);
    }

    public function adminUserEdit(array $parameters): never
    {
        $this->adminCheck();

        if (!empty($_POST)) {
            if (!empty($_POST['is_admin'])) {
                $isAdmin = 1;
            } else {
                $isAdmin = 0;
            }

            if (empty($_POST['new_password'])) {
                $current = $this->userRepository->findByID(id: $_POST['id']);
                $password = $current->getPassword();
                $updateUser = new User(nick: $_POST['nick'], password: $password, first: $_POST['first'], last: $_POST['last'], id: $_POST['id'], isAdmin: $isAdmin);
            } else {
                $password = $_POST['new_password'];
                $updateUser = new User(nick: $_POST['nick'], newPassword: $password, first: $_POST['first'], last: $_POST['last'], id: $_POST['id'], isAdmin: $isAdmin);
            }

            $this->userRepository->update(user: $updateUser);

            $users = $this->userRepository->findAll();

            $this->template->render(templateName: 'admin/users.html.php', vars: [
                'user'   => $this->user,
                'users'  => $users,
                'router' => $this->router
            ]);
        }

        $editUser = $this->userRepository->findByNick(nick: $parameters['nick']);


        $this->template->render(templateName: 'admin/users_edit.html.php', vars: [
            'user'     => $this->user,
            'editUser' => $editUser,
            'router'   => $this->router
        ]);
    }

    public function adminUserAdd(): never
    {
        $this->adminCheck();

        // TODO currently breaks on inserting a duplicate nick
        if (!empty($_POST)) {
            $isAdmin = empty($_POST['is_admin']) ? 0 : 1;
            $user = new User(nick: $_POST['nick'], newPassword: $_POST['new_password'], first: $_POST['first'], last: $_POST['last'], isAdmin: $isAdmin);

            if ($this->userRepository->insert(user: $user)) {
                $users = $this->userRepository->findAll();

                $this->template->render(templateName: 'admin/users.html.php', vars: [
                    'user'   => $this->user,
                    'users'  => $users,
                    'router' => $this->router
                ]);
            } else {
                die("Error inserting user");
            }

        }

        $this->template->render(templateName: 'admin/users_add.html.php', vars: [
            'user'   => $this->user,
            'router' => $this->router
        ]);
    }

    public function adminUserDelete(array $parameters): never
    {
        $this->adminCheck();

        $nick = $parameters['nick'];
        if ($user = $this->userRepository->findByNick(nick: $nick)) {
            if ($this->userRepository->delete(user: $user)) {
                $users = $this->userRepository->findAll();

                $this->template->render(templateName: 'admin/users.html.php', vars: [
                    'user'   => $this->user,
                    'users'  => $users,
                    'router' => $this->router
                ]);
            } else {
                die("Error deleting user");
            }
        } else {
            // TODO use 404
            die("Nick: $nick not found");
        }
    }

}