<?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\PositionService;
use RuntimeException;
use System\Http\Request;
use System\View\View;

final class PositionController extends Controller
{
    public function __construct(
        private AuthenticationService $authentication,
        private AuthorizationService $authorization,
        private PositionService $positionService,
        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(
            'positions/index',
            [
                'title' =>
                    'Position Management',

                'positions' =>
                    $this->positionService->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(
            'positions/create',
            [
                'title' =>
                    'Create Position',

                'departments' =>
                    $this->positionService
                        ->departments()
            ]
        );
    }

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

        try {

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

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

            exit;

        } catch (\Throwable $exception) {

            $this->render(
                'positions/create',
                [
                    'title' =>
                        'Create Position',

                    'departments' =>
                        $this->positionService
                            ->departments(),

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

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

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

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

        $position =
            $this->positionService->find($id);

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

        $this->render(
            'positions/edit',
            [
                'title' =>
                    'Edit Position',

                'position' =>
                    $position,

                'departments' =>
                    $this->positionService
                        ->departments()
            ]
        );
    }

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

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

        try {

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

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

            exit;

        } catch (\Throwable $exception) {

            $this->render(
                'positions/edit',
                [
                    'title' =>
                        'Edit Position',

                    'position' =>
                        $this->positionService
                            ->find($id),

                    'departments' =>
                        $this->positionService
                            ->departments(),

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

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

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

        try {

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

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

        } catch (\Throwable $exception) {

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

        exit;
    }
}