<?php

namespace App\Http\Controllers;

use App\Clients\ItgApiClient;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Inertia\Inertia;
use Inertia\Response;

class WelcomeController extends Controller
{
    public function __construct(private readonly ItgApiClient $itgApiClient)
    {
    }

    public function view(Request $request):
    Application|Redirector|Response|RedirectResponse|\Illuminate\Contracts\Foundation\Application {
        $messageFlash = $request->session()->get('message_flash');
        $sessionLang = $request->session()->get('locale');
        if (!$request->session()->get('keep_session')) {
            $this->flushData($request);
        }
        $request->session()->put('locale', $sessionLang);
        $code = $request->get('c');
        $expositionUrl = $this->getSavedExpositionUrl($code);
        if ($expositionUrl) {
            return redirect($expositionUrl);
        }
        $fair = $this->itgApiClient->getExhibifyFairByCode($code);
        $request->session()->put('fair', $fair);
        return Inertia::render('Welcome', [
            'fair' => $fair,
            'message_flash' => $messageFlash,
        ]);
    }
}
