<?php

namespace App\Http\Controllers\Api\Admin;

use App\Actions\Course\CreateCourseAction;
use App\Actions\Course\DeleteCourseAction;
use App\Actions\Course\UpdateCourseAction;
use App\DTOs\CourseData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Course\StoreCourseRequest;
use App\Http\Requests\Course\UpdateCourseRequest;
use App\Http\Resources\CourseResource;
use App\Models\Course;
use App\Repositories\Contracts\CourseRepositoryInterface;
use Illuminate\Http\JsonResponse;
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/admin/courses',
        summary: 'Eğitimleri listele (Admin)',
        tags: ['Admin - Courses'],
        security: [['sanctum' => []]],
        parameters: [
            new OA\Parameter(name: 'category', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
            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')),
            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\Post(
        path: '/api/admin/courses',
        summary: 'Yeni eğitim oluştur',
        tags: ['Admin - Courses'],
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            required: ['category_id', 'title', 'slug', 'desc', 'long_desc', 'duration'],
            properties: [
                new OA\Property(property: 'category_id', type: 'integer', description: 'Kategori ID'),
                new OA\Property(property: 'slug', type: 'string', description: 'URL slug (unique)'),
                new OA\Property(property: 'title', type: 'string', description: 'Eğitim başlığı'),
                new OA\Property(property: 'sub', type: 'string', nullable: true, description: 'Alt başlık. Örn: STCW II/1'),
                new OA\Property(property: 'desc', type: 'string', description: 'Kısa açıklama'),
                new OA\Property(property: 'long_desc', type: 'string', description: 'Detaylı açıklama'),
                new OA\Property(property: 'duration', type: 'string', description: 'Süre. Örn: 5 Gün'),
                new OA\Property(property: 'students', type: 'integer', description: 'Öğrenci sayısı'),
                new OA\Property(property: 'rating', type: 'number', format: 'float', description: 'Puan (0-5)'),
                new OA\Property(property: 'badge', type: 'string', nullable: true, description: 'Rozet. Örn: Simülatör'),
                new OA\Property(property: 'image', type: 'string', nullable: true, description: 'Görsel path'),
                new OA\Property(property: 'price', type: 'string', nullable: true, description: 'Fiyat. Örn: 5.000 TL'),
                new OA\Property(property: 'includes', type: 'array', items: new OA\Items(type: 'string'), nullable: true, description: 'Fiyata dahil olanlar'),
                new OA\Property(property: 'requirements', type: 'array', items: new OA\Items(type: 'string'), nullable: true, description: 'Katılım koşulları'),
                new OA\Property(property: 'meta_title', type: 'string', nullable: true, description: 'SEO Title'),
                new OA\Property(property: 'meta_description', type: 'string', nullable: true, description: 'SEO Description'),
                new OA\Property(property: 'scope', type: 'array', items: new OA\Items(type: 'string'), nullable: true, description: 'Eğitim kapsamı konu başlıkları'),
                new OA\Property(property: 'standard', type: 'string', nullable: true, description: 'Uyum standardı. Örn: STCW / IMO Uyumlu'),
                new OA\Property(property: 'language', type: 'string', nullable: true, description: 'Eğitim dili. Varsayılan: Türkçe'),
                new OA\Property(property: 'location', type: 'string', nullable: true, description: 'Varsayılan lokasyon. Örn: Kadıköy, İstanbul'),
            ],
        )),
        responses: [
            new OA\Response(response: 201, description: 'Eğitim oluşturuldu'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function store(StoreCourseRequest $request, CreateCourseAction $action): JsonResponse
    {
        $dto = CourseData::fromArray($request->validated());
        $course = $action->execute($dto);

        return (new CourseResource($course->load('category')))
            ->response()
            ->setStatusCode(201);
    }

    #[OA\Get(
        path: '/api/admin/courses/{course}',
        summary: 'Eğitim detayı (Admin)',
        tags: ['Admin - Courses'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'course', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Eğitim detayı')],
    )]
    public function show(Course $course): CourseResource
    {
        return new CourseResource($course->load(['category', 'schedules', 'blocks']));
    }

    #[OA\Put(
        path: '/api/admin/courses/{course}',
        summary: 'Eğitim güncelle',
        tags: ['Admin - Courses'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'course', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            required: ['category_id', 'title', 'slug', 'desc', 'long_desc', 'duration'],
            properties: [
                new OA\Property(property: 'category_id', type: 'integer', description: 'Kategori ID'),
                new OA\Property(property: 'slug', type: 'string', description: 'URL slug (unique)'),
                new OA\Property(property: 'title', type: 'string', description: 'Eğitim başlığı'),
                new OA\Property(property: 'sub', type: 'string', nullable: true, description: 'Alt başlık'),
                new OA\Property(property: 'desc', type: 'string', description: 'Kısa açıklama'),
                new OA\Property(property: 'long_desc', type: 'string', description: 'Detaylı açıklama'),
                new OA\Property(property: 'duration', type: 'string', description: 'Süre. Örn: 5 Gün'),
                new OA\Property(property: 'students', type: 'integer', description: 'Öğrenci sayısı'),
                new OA\Property(property: 'rating', type: 'number', format: 'float', description: 'Puan (0-5)'),
                new OA\Property(property: 'badge', type: 'string', nullable: true, description: 'Rozet'),
                new OA\Property(property: 'image', type: 'string', nullable: true, description: 'Görsel path'),
                new OA\Property(property: 'price', type: 'string', nullable: true, description: 'Fiyat'),
                new OA\Property(property: 'includes', type: 'array', items: new OA\Items(type: 'string'), nullable: true, description: 'Fiyata dahil olanlar'),
                new OA\Property(property: 'requirements', type: 'array', items: new OA\Items(type: 'string'), nullable: true, description: 'Katılım koşulları'),
                new OA\Property(property: 'meta_title', type: 'string', nullable: true, description: 'SEO Title'),
                new OA\Property(property: 'meta_description', type: 'string', nullable: true, description: 'SEO Description'),
                new OA\Property(property: 'scope', type: 'array', items: new OA\Items(type: 'string'), nullable: true, description: 'Eğitim kapsamı konu başlıkları'),
                new OA\Property(property: 'standard', type: 'string', nullable: true, description: 'Uyum standardı'),
                new OA\Property(property: 'language', type: 'string', nullable: true, description: 'Eğitim dili'),
                new OA\Property(property: 'location', type: 'string', nullable: true, description: 'Varsayılan lokasyon'),
            ],
        )),
        responses: [
            new OA\Response(response: 200, description: 'Eğitim güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(UpdateCourseRequest $request, Course $course, UpdateCourseAction $action): CourseResource
    {
        $dto = CourseData::fromArray($request->validated());
        $course = $action->execute($course, $dto);

        return new CourseResource($course->load('category'));
    }

    #[OA\Delete(
        path: '/api/admin/courses/{course}',
        summary: 'Eğitim sil',
        tags: ['Admin - Courses'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'course', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Eğitim silindi')],
    )]
    public function destroy(Course $course, DeleteCourseAction $action): JsonResponse
    {
        $action->execute($course);

        return response()->json(['message' => 'Eğitim başarıyla silindi.']);
    }
}
