<?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\Core\Http\RedirectPathResolver;
use App\Modules\Auth\AuthService;
use Throwable;

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

    public function index(Request $request): Response
    {
        $settings = $this->repository->getSettings();

        $html = $this->template->render('settings/inpost', [
            'title' => $this->translator->get('settings.inpost.title'),
            'activeMenu' => 'settings',
            'activeSettings' => 'inpost',
            'user' => $this->auth->user(),
            'csrfToken' => Csrf::token(),
            'settings' => $settings,
            'errorMessage' => (string) Flash::get('settings_error', ''),
            'successMessage' => (string) Flash::get('settings_success', ''),
        ], 'layouts/app');

        return Response::html($html);
    }

    public function save(Request $request): Response
    {
        $redirectTo = RedirectPathResolver::resolve((string) $request->input('return_to', '/settings/integrations/inpost'), ['/settings/integrations'], '/settings/integrations/inpost');

        if (!Csrf::validate((string) $request->input('_token', ''))) {
            Flash::set('settings_error', $this->translator->get('auth.errors.csrf_expired'));
            return Response::redirect($redirectTo);
        }

        try {
            $this->repository->saveSettings([
                'api_token' => trim((string) $request->input('api_token', '')),
                'organization_id' => trim((string) $request->input('organization_id', '')),
                'environment' => trim((string) $request->input('environment', 'sandbox')),
                'default_dispatch_method' => trim((string) $request->input('default_dispatch_method', 'pop')),
                'default_dispatch_point' => trim((string) $request->input('default_dispatch_point', '')),
                'default_insurance' => $request->input('default_insurance', ''),
                'default_locker_size' => trim((string) $request->input('default_locker_size', 'small')),
                'default_courier_length' => $request->input('default_courier_length', 20),
                'default_courier_width' => $request->input('default_courier_width', 15),
                'default_courier_height' => $request->input('default_courier_height', 8),
                'label_format' => trim((string) $request->input('label_format', 'Pdf')),
                'weekend_delivery' => $request->input('weekend_delivery', ''),
                'auto_insurance_value' => $request->input('auto_insurance_value', ''),
                'multi_parcel' => $request->input('multi_parcel', ''),
            ]);
            Flash::set('settings_success', $this->translator->get('settings.inpost.flash.saved'));
        } catch (Throwable $exception) {
            Flash::set(
                'settings_error',
                $this->translator->get('settings.inpost.flash.save_failed') . ' ' . $exception->getMessage()
            );
        }

        return Response::redirect($redirectTo);
    }

}
