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

final class EmployeeFamilyController extends Controller
{
    public function __construct(
        private AuthenticationService $authentication,
        private AuthorizationService $authorization,
        private EmployeeFamilyService $familyService,
        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 store(): void
    {
        $this->protect();
        $this->requireAdministrator();

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

        try {
            $this->familyService->create(
                $employeeId,
                $this->request->all()
            );

            header(
                'Location: /public/employees/show?id='
                . $employeeId
                . '&family_created=1'
            );

            exit;
        } catch (\Throwable $exception) {
            header(
                'Location: /public/employees/show?id='
                . $employeeId
                . '&family_error='
                . urlencode($exception->getMessage())
            );

            exit;
        }
    }

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

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

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

        try {
            $this->familyService->update(
                $id,
                $this->request->all()
            );

            header(
                'Location: /public/employees/show?id='
                . $employeeId
                . '&family_updated=1'
            );

            exit;
        } catch (\Throwable $exception) {
            header(
                'Location: /public/employees/show?id='
                . $employeeId
                . '&family_error='
                . urlencode($exception->getMessage())
            );

            exit;
        }
    }

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

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

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

        try {
            $this->familyService->deactivate($id);

            header(
                'Location: /public/employees/show?id='
                . $employeeId
                . '&family_deactivated=1'
            );

            exit;
        } catch (\Throwable $exception) {
            header(
                'Location: /public/employees/show?id='
                . $employeeId
                . '&family_error='
                . urlencode($exception->getMessage())
            );

            exit;
        }
    }
}