<?php

namespace App\Http\Controllers\Api\V1\Carousel;

use App\Http\Controllers\Api\V1\Carousel\Requests\CarouselIndexRequest;
use App\Http\Controllers\Api\V1\Carousel\Resources\CarouselResource;
use App\Http\Controllers\Controller;
use App\Models\CMS\Media\Carousel;
use Illuminate\Http\JsonResponse;

class CarouselController extends Controller
{
    /**
     * (Media) Carousels (index)
     */
    public function index(CarouselIndexRequest $request): JsonResponse
    {
        return response()->rest_paginate(
            CarouselResource::collection(
                Carousel::with('media')
                    ->where('is_visible', true)
                    ->when($request->place, fn ($query, $place) => $query->where('place', $place))
                    ->when($request->app, fn ($query, $app) => $query->where('app', $app))
                    ->ordered()
                    ->simplePaginate(
                        perPage: $request->perPage,
                        columns: $request->fields,
                    )
            )
        );
    }
}
