<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cookie;
use Inertia\Inertia;

class Controller extends BaseController
{
    use AuthorizesRequests, ValidatesRequests;

    /**
     * @param mixed $fair
     * @param $language
     * @return array
     */
    public function getDefaultExpositionDataForFair(mixed $fair, $language): array
    {
        return [
            "name" => $fair->name,
            "main_image" => $fair->logo_url,
            "opening_date" => Carbon::createFromFormat('Y-m-d H:i:s', $fair->date_start)->format('Y-m-d'),
            "closing_date" => Carbon::createFromFormat('Y-m-d H:i:s', $fair->date_end)->format('Y-m-d'),
            'language' => $language,
        ];
    }

    /**
     * @param Request $request
     * @return void
     */
    protected function flushData(Request $request): void
    {
        $request->session()->flush();
        Inertia::flushShared();
    }

    /**
     * @param mixed $code
     * @return array|string|null
     */
    protected function getSavedExpositionUrl(mixed $code): string|array|null
    {
        return request()->cookie($code);
    }

    /**
     * @param mixed $fair
     * @param mixed $exposition
     * @return void
     */
    protected function saveExpositionUrl(mixed $fair, mixed $exposition): void
    {
        Cookie::queue(cookie($fair->code, $exposition->view_url));
    }
}
