<?php

namespace App\Http\Controllers;

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

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

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

            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());
        });
    }
}
