<?php

namespace App\Http\Controllers\Api\V1;

use App\Domain\Event\Models\Event;
use App\Domain\Training\Models\TrainingProgram;
use App\Domain\Website\Models\Media;
use App\Domain\Website\Models\WebsiteNews;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AcademyController extends Controller
{
    public function news(Request $request): JsonResponse
    {
        $news = WebsiteNews::whereNotNull('published_at')
            ->where('published_at', '<=', now())
            ->orderByDesc('published_at')
            ->with('image')
            ->paginate(15);

        return response()->json([
            'data' => $news->map(fn ($item) => [
                'id' => $item->id,
                'uuid' => $item->uuid,
                'title' => $item->title,
                'excerpt' => $item->excerpt,
                'category' => $item->category,
                'image_url' => $item->image?->url ? asset('storage/' . $item->image->url) : null,
                'published_at' => $item->published_at?->toIso8601String(),
                'is_featured' => $item->is_featured,
            ]),
            'meta' => [
                'current_page' => $news->currentPage(),
                'last_page' => $news->lastPage(),
                'per_page' => $news->perPage(),
                'total' => $news->total(),
            ],
        ]);
    }

    public function newsShow(string $uuid): JsonResponse
    {
        $article = WebsiteNews::where('uuid', $uuid)
            ->whereNotNull('published_at')
            ->with('image')
            ->firstOrFail();

        return response()->json([
            'data' => [
                'id' => $article->id,
                'uuid' => $article->uuid,
                'title' => $article->title,
                'body' => $article->body,
                'category' => $article->category,
                'image_url' => $article->image?->url ? asset('storage/' . $article->image->url) : null,
                'published_at' => $article->published_at?->toIso8601String(),
            ],
        ]);
    }

    public function programs(): JsonResponse
    {
        $programs = TrainingProgram::active()
            ->with(['activity'])
            ->orderBy('name_ar')
            ->get();

        return response()->json([
            'data' => $programs->map(fn ($p) => [
                'id' => $p->id,
                'uuid' => $p->uuid,
                'name_ar' => $p->name_ar,
                'name' => $p->name,
                'description_ar' => $p->description_ar,
                'activity' => [
                    'id' => $p->activity?->id,
                    'name_ar' => $p->activity?->name_ar,
                    'category' => $p->activity?->category,
                ],
                'age_min' => $p->age_min,
                'age_max' => $p->age_max,
                'gender' => $p->gender,
            ]),
        ]);
    }

    public function events(): JsonResponse
    {
        $events = Event::where('status', 'published')
            ->where('starts_at', '>=', now())
            ->orderBy('starts_at')
            ->with('cover')
            ->paginate(15);

        return response()->json([
            'data' => $events->map(fn ($e) => [
                'id' => $e->id,
                'uuid' => $e->uuid,
                'title' => $e->title,
                'description' => $e->description,
                'type' => $e->type?->value,
                'starts_at' => $e->starts_at?->toIso8601String(),
                'ends_at' => $e->ends_at?->toIso8601String(),
                'location_name' => $e->location_name,
                'cover_url' => $e->cover?->url ? asset('storage/' . $e->cover->url) : null,
                'max_capacity' => $e->max_capacity,
                'registrations_count' => $e->registrations_count,
                'is_registration_open' => $e->isRegistrationOpen(),
                'spots_remaining' => $e->spotsRemaining(),
            ]),
            'meta' => [
                'current_page' => $events->currentPage(),
                'last_page' => $events->lastPage(),
                'per_page' => $events->perPage(),
                'total' => $events->total(),
            ],
        ]);
    }

    public function gallery(): JsonResponse
    {
        $media = Media::where('collection', 'gallery')
            ->orderByDesc('created_at')
            ->paginate(20);

        return response()->json([
            'data' => $media->map(fn ($m) => [
                'id' => $m->id,
                'url' => $m->url ? asset('storage/' . $m->url) : null,
                'caption' => $m->caption,
                'type' => $m->type,
                'created_at' => $m->created_at?->toIso8601String(),
            ]),
            'meta' => [
                'current_page' => $media->currentPage(),
                'last_page' => $media->lastPage(),
                'per_page' => $media->perPage(),
                'total' => $media->total(),
            ],
        ]);
    }
}
