<?php

namespace App\Http\Controllers;

use App\Http\Requests\VehicleContentFilterOptionsRequest;
use App\Http\Requests\VehicleContentReviewsRequest;
use App\Http\Requests\VehicleContentVideosRequest;
use App\Http\Resources\VehicleContentCollection;
use App\Services\VehicleContentService;
use App\Traits\CacheObject;

class VehicleContentController extends Controller
{
    use CacheObject;

    public function __construct(
        private VehicleContentService $service
    ) {
    }

    public function videos(VehicleContentVideosRequest $request): array
    {
        $makeSlug = $request->input('make');
        $modelSlug = $request->input('model');
        $perPage = $request->input('per_page');
        $page = $request->input('page', 1);
        $singleImagePlaceholder = $request->boolean('single_image_placeholder');

        $cacheKey = 'vehicle-content-videos-' . md5(json_encode([
            'make' => $makeSlug,
            'model' => $modelSlug,
            'per_page' => $perPage,
            'page' => $page,
            'single_image_placeholder' => $singleImagePlaceholder,
        ]));

        return $this->cache(
            $cacheKey,
            5,
            function () use ($makeSlug, $modelSlug, $perPage, $page, $singleImagePlaceholder) {
                $videos = $this->service->getVideos(
                    $makeSlug,
                    $modelSlug,
                    $perPage,
                    $page,
                    $singleImagePlaceholder
                );

                return (new VehicleContentCollection($videos, 'videos'))->toArray(request());
            }
        );
    }

    public function reviews(VehicleContentReviewsRequest $request): array
    {
        $provider = $request->input('provider');
        $makeSlug = $request->input('make');
        $modelSlug = $request->input('model');
        $perPage = $request->input('per_page');
        $page = $request->input('page', 1);

        $cacheKey = 'vehicle-content-reviews-' . md5(json_encode([
            'provider' => $provider,
            'make' => $makeSlug,
            'model' => $modelSlug,
            'per_page' => $perPage,
            'page' => $page,
        ]));

        return $this->cache($cacheKey, 5, function () use ($provider, $makeSlug, $modelSlug, $perPage, $page) {
            $reviews = $this->service->getReviews($provider, $makeSlug, $modelSlug, $perPage, $page);

            return (new VehicleContentCollection($reviews, 'reviews'))->toArray(request());
        });
    }

    public function makes(VehicleContentFilterOptionsRequest $request): array
    {
        $provider = $request->input('provider');

        $cacheKey = 'vehicle-content-makes-' . md5($provider);

        return $this->cache($cacheKey, 5, fn () => $this->service->getMakes($provider));
    }

    public function models(VehicleContentFilterOptionsRequest $request): array
    {
        $provider = $request->input('provider');
        $make = $request->input('make');

        $cacheKey = 'vehicle-content-models-' . md5(json_encode(['provider' => $provider, 'make' => $make]));

        return $this->cache($cacheKey, 5, fn () => $this->service->getModels($provider, $make));
    }
}
