<?php

declare(strict_types=1);

namespace App\Modules\Administration\Controllers;

use App\Controllers\Controller;
use App\Modules\Administration\Services\AuthenticationService;
use App\Modules\Administration\Services\AuthorizationService;
use App\Modules\Administration\Services\StateService;
use RuntimeException;
use System\Http\Request;
use System\View\View;

final class StateController extends Controller
{
    public function __construct(
        private AuthenticationService $authentication,
        private AuthorizationService $authorization,
        private StateService $stateService,
        private Request $request,
        View $view
    ) {
        parent::__construct($view);
    }

    private function protect(): void
    {
        if (!$this->authentication->check()) {
            header('Location: /public/login');
            exit;
        }
    }

    private function requireAdministrator(): void
    {
        if (!$this->authorization->isAdministrator()) {
            header('Location: /public/403');
            exit;
        }
    }

    public function index(): void
    {
        $this->protect();
        $this->requireAdministrator();

        $this->render(
            'states/index',
            [
                'title' => 'State Management',

                'states' =>
                    $this->stateService->all(),

                'created' =>
                    $this->request->input('created'),

                'updated' =>
                    $this->request->input('updated'),

                'statusChanged' =>
                    $this->request->input(
                        'status_changed'
                    ),

                'error' =>
                    $this->request->input('error')
            ]
        );
    }

    public function create(): void
    {
        $this->protect();
        $this->requireAdministrator();

        $this->render(
            'states/create',
            [
                'title' =>
                    'Create State / Administrative Area'
            ]
        );
    }

    public function store(): void
    {
        $this->protect();
        $this->requireAdministrator();

        try {

            $this->stateService->create(
                $this->request->all()
            );

            header(
                'Location: /public/states?created=1'
            );

            exit;

        } catch (\Throwable $exception) {

            $this->render(
                'states/create',
                [
                    'title' =>
                        'Create State / Administrative Area',

                    'error' =>
                        $exception->getMessage(),

                    'old' =>
                        $this->request->all()
                ]
            );
        }
    }

    public function edit(): void
    {
        $this->protect();
        $this->requireAdministrator();

        $id = (int) $this->request->input(
            'id',
            0
        );

        $state = $this->stateService->find($id);

        if ($state === null) {
            throw new RuntimeException(
                'State not found.'
            );
        }

        $this->render(
            'states/edit',
            [
                'title' =>
                    'Edit State / Administrative Area',

                'state' => $state
            ]
        );
    }

    public function update(): void
    {
        $this->protect();
        $this->requireAdministrator();

        $id = (int) $this->request->input(
            'id',
            0
        );

        try {

            $this->stateService->update(
                $id,
                $this->request->all()
            );

            header(
                'Location: /public/states?updated=1'
            );

            exit;

        } catch (\Throwable $exception) {

            $this->render(
                'states/edit',
                [
                    'title' =>
                        'Edit State / Administrative Area',

                    'state' =>
                        $this->stateService->find($id),

                    'error' =>
                        $exception->getMessage()
                ]
            );
        }
    }

    public function toggleStatus(): void
    {
        $this->protect();
        $this->requireAdministrator();

        $id = (int) $this->request->input(
            'id',
            0
        );

        try {

            $this->stateService
                ->toggleStatus($id);

            header(
                'Location: /public/states?status_changed=1'
            );

        } catch (\Throwable $exception) {

            header(
                'Location: /public/states?error='
                . urlencode(
                    $exception->getMessage()
                )
            );
        }

        exit;
    }
}