<?php
declare(strict_types=1);

namespace App\Modules\Settings;

use App\Core\Http\Request;
use App\Core\Http\Response;
use App\Core\I18n\Translator;
use App\Core\Security\Csrf;
use App\Core\Support\Flash;
use App\Core\View\Template;
use App\Modules\Auth\AuthService;
use Throwable;

final class CompanySettingsController
{
    public function __construct(
        private readonly Template $template,
        private readonly Translator $translator,
        private readonly AuthService $auth,
        private readonly CompanySettingsRepository $repository
    ) {
    }

    public function index(Request $request): Response
    {
        $html = $this->template->render('settings/company', [
            'title' => $this->translator->get('settings.company.title'),
            'activeMenu' => 'settings',
            'activeSettings' => 'company',
            'user' => $this->auth->user(),
            'csrfToken' => Csrf::token(),
            'settings' => $this->repository->getSettings(),
            'errorMessage' => (string) Flash::get('settings_error', ''),
            'successMessage' => (string) Flash::get('settings_success', ''),
        ], 'layouts/app');

        return Response::html($html);
    }

    public function save(Request $request): Response
    {
        $token = (string) $request->input('_token', '');
        if (!Csrf::validate($token)) {
            Flash::set('settings_error', $this->translator->get('auth.errors.csrf_expired'));
            return Response::redirect('/settings/company');
        }

        try {
            $this->repository->saveSettings([
                'company_name' => (string) $request->input('company_name', ''),
                'person_name' => (string) $request->input('person_name', ''),
                'sender_contact_person' => (string) $request->input('sender_contact_person', ''),
                'street' => (string) $request->input('street', ''),
                'city' => (string) $request->input('city', ''),
                'postal_code' => (string) $request->input('postal_code', ''),
                'country_code' => (string) $request->input('country_code', 'PL'),
                'phone' => (string) $request->input('phone', ''),
                'email' => (string) $request->input('email', ''),
                'tax_number' => (string) $request->input('tax_number', ''),
                'bank_account' => (string) $request->input('bank_account', ''),
                'bank_owner_name' => (string) $request->input('bank_owner_name', ''),
                'bdo_number' => (string) $request->input('bdo_number', ''),
                'regon' => (string) $request->input('regon', ''),
                'court_register' => (string) $request->input('court_register', ''),
                'logo_path' => (string) $request->input('logo_path', ''),
                'default_package_length_cm' => (string) $request->input('default_package_length_cm', '25'),
                'default_package_width_cm' => (string) $request->input('default_package_width_cm', '20'),
                'default_package_height_cm' => (string) $request->input('default_package_height_cm', '8'),
                'default_package_weight_kg' => (string) $request->input('default_package_weight_kg', '1'),
                'default_label_format' => (string) $request->input('default_label_format', 'PDF'),
            ]);
            Flash::set('settings_success', $this->translator->get('settings.company.flash.saved'));
        } catch (Throwable $exception) {
            Flash::set('settings_error', $this->translator->get('settings.company.flash.save_failed') . ' ' . $exception->getMessage());
        }

        return Response::redirect('/settings/company');
    }
}
