<?php

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

use App\Http\Controllers\Api\V1\Banner\Requests\BannerIndexRequest;
use App\Http\Controllers\Api\V1\Banner\Resources\BannerResource;
use App\Http\Controllers\Controller;
use App\Models\CMS\Media\Banner;
use Illuminate\Http\JsonResponse;

class BannerController extends Controller
{
    /**
     * (Media) Banners (index)
     */
    public function index(BannerIndexRequest $request): JsonResponse
    {
        return response()->rest_paginate(
            BannerResource::collection(
                Banner::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,
                    )
            )
        );
    }
}
