<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Resources\CourseScheduleResource;
use App\Repositories\Contracts\ScheduleRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class ScheduleController extends Controller
{
    public function __construct(private ScheduleRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/v1/schedules',
        summary: 'Eğitim takvimini listele',
        tags: ['Schedules'],
        parameters: [
            new OA\Parameter(name: 'course_id', in: 'query', required: false, schema: new OA\Schema(type: 'integer')),
            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: 'Takvim listesi')],
    )]
    public function index(Request $request): AnonymousResourceCollection
    {
        $schedules = $this->repository->paginate(
            $request->only(['course_id']),
            $request->integer('per_page', 15),
        );

        return CourseScheduleResource::collection($schedules);
    }

    #[OA\Get(
        path: '/api/v1/schedules/upcoming',
        summary: 'Yaklaşan eğitimleri listele',
        tags: ['Schedules'],
        responses: [new OA\Response(response: 200, description: 'Yaklaşan eğitimler')],
    )]
    public function upcoming(): AnonymousResourceCollection
    {
        $schedules = $this->repository->upcoming(20);

        return CourseScheduleResource::collection($schedules);
    }

    #[OA\Get(
        path: '/api/v1/schedules/{id}',
        summary: 'Takvim detayı',
        tags: ['Schedules'],
        parameters: [new OA\Parameter(name: 'id', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [
            new OA\Response(response: 200, description: 'Takvim detayı'),
            new OA\Response(response: 404, description: 'Takvim bulunamadı'),
        ],
    )]
    public function show(int $id): JsonResponse
    {
        $schedule = $this->repository->findById($id);

        if (! $schedule) {
            return response()->json(['message' => 'Takvim bulunamadı.'], 404);
        }

        $schedule->load('course.category');

        return response()->json(new CourseScheduleResource($schedule));
    }
}
