<?php

namespace App\Http\Controllers;

use App\Http\Resources\LegalDocumentResource;
use App\Models\LegalDocument;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class LegalDocumentController extends Controller
{
    public function index(Request $request): AnonymousResourceCollection
    {
        $query = LegalDocument::query()
            ->public()
            ->latest('published_at')
            ->latest('version');

        if ($request->filled('type')) {
            $query->where('type', $request->string('type')->toString());
        }

        return LegalDocumentResource::collection($query->get());
    }

    public function show(string $slug): LegalDocumentResource
    {
        $document = LegalDocument::publicForSlug($slug);

        abort_if(! $document, 404, __('api.legal_documents.not_found'));

        return new LegalDocumentResource($document);
    }
}
