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

final class BranchController extends Controller
{
    public function __construct(
        private AuthenticationService $authentication,
        private AuthorizationService $authorization,
        private BranchService $branchService,
        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(
            'branches/index',
            [
                'title' =>
                    'Branch Management',

                'branches' =>
                    $this->branchService->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(
            'branches/create',
            [
                'title' =>
                    'Create Branch',

                'organizations' =>
                    $this->branchService
                        ->organizations()
            ]
        );
    }

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

        try {

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

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

            exit;

        } catch (\Throwable $exception) {

            $this->render(
                'branches/create',
                [
                    'title' =>
                        'Create Branch',

                    'organizations' =>
                        $this->branchService
                            ->organizations(),

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

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

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

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

        $branch =
            $this->branchService->find($id);

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

        $this->render(
            'branches/edit',
            [
                'title' =>
                    'Edit Branch',

                'branch' =>
                    $branch,

                'organizations' =>
                    $this->branchService
                        ->organizations()
            ]
        );
    }

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

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

        try {

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

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

            exit;

        } catch (\Throwable $exception) {

            $this->render(
                'branches/edit',
                [
                    'title' =>
                        'Edit Branch',

                    'branch' =>
                        $this->branchService
                            ->find($id),

                    'organizations' =>
                        $this->branchService
                            ->organizations(),

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

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

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

        try {

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

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

        } catch (\Throwable $exception) {

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

        exit;
    }
}