<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Resources\CourseResource;
use App\Repositories\Contracts\CourseRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class CourseController extends Controller
{
    public function __construct(private CourseRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/v1/courses',
        summary: 'Eğitimleri listele',
        description: 'Tüm eğitimleri sayfalanmış olarak döndürür. Kategori, arama ve sıralama filtresi destekler.',
        tags: ['Courses'],
        parameters: [
            new OA\Parameter(name: 'category', in: 'query', required: false, schema: new OA\Schema(type: 'string'), description: 'Kategori slug filtresi'),
            new OA\Parameter(name: 'search', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
            new OA\Parameter(name: 'sort', in: 'query', required: false, schema: new OA\Schema(type: 'string'), description: 'Örn: -created_at, title'),
            new OA\Parameter(name: 'per_page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 15)),
        ],
        responses: [
            new OA\Response(response: 200, description: 'Eğitim listesi'),
        ],
    )]
    public function index(Request $request): AnonymousResourceCollection
    {
        $courses = $this->repository->paginate(
            filters: $request->only('category', 'search', 'sort'),
            perPage: $request->integer('per_page', 15),
        );

        return CourseResource::collection($courses);
    }

    #[OA\Get(
        path: '/api/v1/courses/{slug}',
        summary: 'Eğitim detayı',
        description: 'Slug ile eğitim detayını döndürür.',
        tags: ['Courses'],
        parameters: [
            new OA\Parameter(name: 'slug', in: 'path', required: true, schema: new OA\Schema(type: 'string')),
        ],
        responses: [
            new OA\Response(response: 200, description: 'Eğitim detayı'),
            new OA\Response(response: 404, description: 'Eğitim bulunamadı'),
        ],
    )]
    public function show(string $slug): CourseResource
    {
        $course = $this->repository->findBySlug($slug);

        abort_if(! $course, 404, 'Eğitim bulunamadı.');

        return new CourseResource($course);
    }
}
