<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\DealerResource;
use App\Models\Dealer;
use Illuminate\Http\Request;

class DealerApiController extends Controller
{
    public function index(Request $request)
    {
        $this->authorize('viewAny', Dealer::class);

        $dealers = Dealer::query()
            ->when($request->filled('status'), fn ($q) => $q->where('status', $request->status))
            ->paginate($request->integer('per_page', 20));

        return DealerResource::collection($dealers);
    }

    public function show(Dealer $dealer)
    {
        $this->authorize('view', $dealer);

        return new DealerResource($dealer);
    }
}
