<?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;

class PreloadController extends Controller
{
    public function __construct(
        private readonly ItgApiClient $itgApiClient
    ) {
    }
    public function receiveData(Request $request):
    Application|Redirector|RedirectResponse|\Illuminate\Contracts\Foundation\Application
    {
        $this->flushData($request);
        $data = $request->all();

        $fairCode = $data['fair_code'];
        $expositionUrl = $this->getSavedExpositionUrl($data['fair_code']);
        if ($expositionUrl) {
            return redirect($expositionUrl);
        }
        $fair = $this->itgApiClient->getExhibifyFairByCode($fairCode);
        $request->session()->put('fair', $fair);
        $defaultExpositionData = $this->getDefaultExpositionDataForFair($fair, $data['account_language']);
        if (!empty($data['exposition_name']) || !empty($data['exposition_description'])
            || !empty($data['exposition_start_date']) || !empty($data['exposition_end_date'])
            || !empty($data['exposition_main_image'])) {
            $expositionData = [
                'name' => !empty($data['exposition_name'])
                    ? $data['exposition_name']
                    : $defaultExpositionData['name'],
                'description' => !empty($data['exposition_description'])
                    ? $data['exposition_description']
                    : $defaultExpositionData['description'],
                'opening_date' => !empty($data['exposition_start_date'])
                    ? $data['exposition_start_date']
                    : $defaultExpositionData['opening_date'],
                'closing_date' => !empty($data['exposition_end_date'])
                    ? $data['exposition_end_date']
                    : $defaultExpositionData['closing_date'],
                'main_image' => !empty($data['exposition_main_image'])
                    ? $data['exposition_main_image']
                    : $defaultExpositionData['main_image'],
                'language' => !empty($data['exposition_language'])
                    ? $data['exposition_language']
                    : $defaultExpositionData['language'],
            ];
            $request->session()->put('exposition', $expositionData);
        }

        $accountData = [
            'email' => $data['account_email'] ?? '',
            'gallery_name' => $data['account_gallery_name'] ?? '',
            'gallery_description' => $data['account_gallery_description'] ?? '',
            'gallery_city' => $data['account_gallery_city'] ?? '',
            'gallery_country' => $data['account_gallery_country'] ?? '',
            'gallery_address' => $data['account_gallery_address'] ?? '',
            'language' => $data['account_language'] ?? '',
        ];
        $request->session()->put('account', $accountData);

        $worksData = [];
        if (!empty($data['works'])) {
            foreach ($data['works'] as $workData) {
                $worksData[] = [
                    'name' => $workData['name'],
                    'description' => $workData['description'] ?? '',
                    'main_image' => $workData['main_image'] ?? '',
                    'artist_name' => $workData['artist_name'] ?? '',
                    'year' => $workData['year'] ?? '',
                    'technique' => $workData['technique'] ?? '',
                    'dimensions' => array_map(function ($dimension) {
                        return [
                            'width' => $dimension['width'],
                            'height' => $dimension['height'],
                            'depth' => $dimension['depth'],
                            'unit' => $dimension['unit'],
                        ];
                    }, $workData['dimensions'] ?? []),
                    'prices' => array_map(function ($price) {
                        return [
                            'price' => $price['price'],
                            'currency' => $price['currency'],
                        ];
                    }, $workData['prices'] ?? []),
                    'selected_price' => [
                        'price' => $workData['prices'][0]['price'],
                        'currency' => $workData['prices'][0]['currency'],
                    ]
                ];
            }
        }
        $request->session()->put('works', $worksData);
        $request->session()->flash('keep_session');
        return redirect('/?c=' . $fairCode);
    }
}
