<?php

namespace App\Http\Controllers;

use App\Clients\ItgApiClient;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
use Inertia\Response;

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

    public function view(Request $request): Response
    {
        if ($request->gallery_id) {
            $request->session()->put('gallery_id', $request->gallery_id);
        }

        if ($request->user_id) {
            $request->session()->put('user_id', $request->user_id);
        }

        if ($request->gallery_id && $request->user_id) {
            $accessToken = $this->itgApiClient->authenticateAsUser($request->user_id);
            $request->session()->put('itg_access_token', $accessToken);
        }
        Log::debug('Session lang [Exposition Controller]: ' . session()->get('locale'));
        return Inertia::render('ExpositionDataForm', [
            'user_id' => $request->user_id ?? $request->session()->get('user_id'),
            'gallery_id' => $request->gallery_id ?? $request->session()->get('gallery_id'),
        ]);
    }

    public function store(Request $request): bool|string
    {
        $data = $request->all();

        $image = $request->file('exposition_main_image');
        $fair = $this->itgApiClient->getExhibifyFairByCode($data['fair_code']);
        $account = $request->session()->get('account');
        if ($image) {
            $imageName = $image->getClientOriginalName();
            $image->move(public_path('uploads'), $imageName);
            $imageUrl = 'public/uploads/' . $imageName;
        }

        $defaultExpositionData = $this->getDefaultExpositionDataForFair($fair, $account['language']);
        if (!empty($data['exposition_id'])) {
            $userId = $request->session()->get('user_id');
            $this->itgApiClient->setAccessToken(
                $request->session()->has('itg_access_token')
                    ? $request->session()->get('itg_access_token')
                    : $this->itgApiClient->authenticateAsUser($userId)
            );
            $exposition = $this->itgApiClient->getExposition($data['exposition_id']);
            $expositionData = [
                'id' => $exposition->id,
                'name' => $exposition->name,
                'description' => $exposition->description,
                'main_image' => $exposition->main_image,
                'opening_date' => $exposition->opening_date,
                'closing_date' => $exposition->closing_date,
                'language' => $defaultExpositionData['language']
            ];
        } else {
            $expositionData = [
                'name' => $data['exposition_name'] ?? $defaultExpositionData['name'],
                'main_image' => $imageUrl ?? $defaultExpositionData['main_image'],
                'main_image_url' => !empty($imageUrl) ? asset(str_replace('public/', '', $imageUrl))
                    : $defaultExpositionData['main_image'],
                'description' => $data['exposition_description'],
                'opening_date' => $defaultExpositionData['opening_date'],
                'closing_date' => $defaultExpositionData['closing_date'],
                'language' => $defaultExpositionData['language'],
            ];
        }
        $request->session()->put('exposition', $expositionData);
        return json_encode(['redirect_url' => '/works_data']);
    }

    public function searchItgExpositions(Request $request)
    {
        $userId = $request->session()->get('user_id');
        $galleryId = $request->session()->get('gallery_id');
        $search = $request->search;
        $this->itgApiClient->setAccessToken(
            $request->session()->has('itg_access_token')
                ? $request->session()->get('itg_access_token')
                : $this->itgApiClient->authenticateAsUser($userId),
        );
        $expositions = $this->itgApiClient->getExpositionsForGallery($galleryId, $search);
        return \response()->json($expositions);
    }
}
