<?php

namespace Lucent\Http\Controller;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Session\Session;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Lucent\Account\AccountService;
use Lucent\Account\AuthService;
use Lucent\Channel\ChannelService;
use Lucent\LucentException;
use Lucent\Svelte\Svelte;
use function Lucent\Response\fail;
use function Lucent\Response\ok;


class AuthController extends Controller
{
    public function __construct(
        private readonly AuthService    $authService,
        private readonly AccountService $accountService,
        private readonly ChannelService $channelService,
        private readonly Session        $session,
        private readonly Svelte         $svelte,
    )
    {

    }

    public function register(Request $request): View|RedirectResponse
    {
        if ($this->accountService->countUsers() > 0) {
            return redirect($this->channelService->channel->lucentUrl . "/login");
        }


        return $this->svelte->render(
            layout: "account",
            view: "register",
            title: "Create an account",

        );
    }


    public function postRegister(Request $request): Response
    {

        if ($this->accountService->countUsers() > 0) {
            abort(400);
        }

        try {
            $this->authService->registerAdmin(
                name: $request->input("name"),
                email: $request->input("email"),
            );
        } catch (LucentException $th) {
            return fail($th);
        }

        return ok();
    }

    public function login(): View|RedirectResponse
    {

        if ($this->accountService->countUsers() == 0) {
            return redirect($this->channelService->channel->lucentUrl . "/register");
        }

        return $this->svelte->render(
            layout: "account",
            view: "login",
            title: "Log in"
        );
    }

    public function postLogin(Request $request): Response
    {
        try {
            $this->authService->sendLoginEmail($request->input("email"));
        } catch (LucentException $th) {
            return fail($th);
        }

        return ok();
    }

    public function verify(Request $request): View
    {
        return $this->svelte->render(
            layout: "account",
            view: "verify",
            title: "Verify and enter",
            data: [
                "email" => $request->input("email"),
                "token" => $request->input("token"),
            ]
        );
    }

    public function postVerify(Request $request): Response
    {
        try {
            $this->authService->login($request->input("email"), $request->input("token"));
        } catch (LucentException $th) {
            return fail($th);
        }
        return ok();
    }


    public function logout(): RedirectResponse
    {
        $this->session->flush();
        return redirect($this->channelService->channel->lucentUrl . "/login");
    }


}
