<?php

namespace App\Http\Controllers\Api\Admin;

use App\Actions\Schedule\CreateScheduleAction;
use App\Actions\Schedule\DeleteScheduleAction;
use App\Actions\Schedule\UpdateScheduleAction;
use App\DTOs\ScheduleData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Schedule\StoreScheduleRequest;
use App\Http\Requests\Schedule\UpdateScheduleRequest;
use App\Http\Resources\CourseScheduleResource;
use App\Models\CourseSchedule;
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/admin/schedules',
        summary: 'Takvimleri listele (Admin)',
        tags: ['Admin - Schedules'],
        security: [['sanctum' => []]],
        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\Post(
        path: '/api/admin/schedules',
        summary: 'Yeni takvim oluştur',
        tags: ['Admin - Schedules'],
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            required: ['course_id', 'start_date', 'location', 'quota'],
            properties: [
                new OA\Property(property: 'course_id', type: 'integer'),
                new OA\Property(property: 'start_date', type: 'string', format: 'date'),
                new OA\Property(property: 'end_date', type: 'string', format: 'date'),
                new OA\Property(property: 'location', type: 'string'),
                new OA\Property(property: 'instructor', type: 'string'),
                new OA\Property(property: 'quota', type: 'integer'),
                new OA\Property(property: 'enrolled_count', type: 'integer'),
                new OA\Property(property: 'price_override', type: 'number'),
                new OA\Property(property: 'status', type: 'string'),
                new OA\Property(property: 'notes', type: 'string'),
            ],
        )),
        responses: [
            new OA\Response(response: 201, description: 'Takvim oluşturuldu'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function store(StoreScheduleRequest $request, CreateScheduleAction $action): JsonResponse
    {
        $dto = ScheduleData::fromArray($request->validated());
        $schedule = $action->execute($dto);

        return response()->json(new CourseScheduleResource($schedule->load('course')), 201);
    }

    #[OA\Get(
        path: '/api/admin/schedules/{schedule}',
        summary: 'Takvim detayı (Admin)',
        tags: ['Admin - Schedules'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'schedule', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Takvim detayı')],
    )]
    public function show(CourseSchedule $schedule): JsonResponse
    {
        return response()->json(new CourseScheduleResource($schedule->load('course')));
    }

    #[OA\Put(
        path: '/api/admin/schedules/{schedule}',
        summary: 'Takvim güncelle',
        tags: ['Admin - Schedules'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'schedule', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            properties: [
                new OA\Property(property: 'course_id', type: 'integer'),
                new OA\Property(property: 'start_date', type: 'string', format: 'date'),
                new OA\Property(property: 'end_date', type: 'string', format: 'date'),
                new OA\Property(property: 'location', type: 'string'),
                new OA\Property(property: 'instructor', type: 'string'),
                new OA\Property(property: 'quota', type: 'integer'),
                new OA\Property(property: 'status', type: 'string'),
            ],
        )),
        responses: [
            new OA\Response(response: 200, description: 'Takvim güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(UpdateScheduleRequest $request, CourseSchedule $schedule, UpdateScheduleAction $action): JsonResponse
    {
        $dto = ScheduleData::fromArray(array_merge($schedule->toArray(), $request->validated()));
        $schedule = $action->execute($schedule, $dto);

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

    #[OA\Delete(
        path: '/api/admin/schedules/{schedule}',
        summary: 'Takvim sil',
        tags: ['Admin - Schedules'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'schedule', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Takvim silindi')],
    )]
    public function destroy(CourseSchedule $schedule, DeleteScheduleAction $action): JsonResponse
    {
        $action->execute($schedule);

        return response()->json(['message' => 'Takvim silindi.']);
    }
}
