<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\Announcement;
use App\Models\Course;
use App\Models\Page;
use Illuminate\Http\JsonResponse;
use OpenApi\Attributes as OA;

class SitemapController extends Controller
{
    #[OA\Get(
        path: '/api/v1/sitemap-data',
        summary: 'SEO sitemap verisi',
        description: 'Yayındaki tüm kurs, duyuru ve sayfa slug+updated_at bilgilerini döner.',
        tags: ['Sitemap'],
        responses: [new OA\Response(response: 200, description: 'Sitemap verisi')],
    )]
    public function index(): JsonResponse
    {
        $courses = Course::query()
            ->select('slug', 'updated_at')
            ->get()
            ->map(fn (Course $c) => [
                'loc' => '/courses/'.$c->slug,
                'lastmod' => $c->updated_at?->toISOString(),
            ]);

        $announcements = Announcement::query()
            ->select('slug', 'updated_at')
            ->get()
            ->map(fn (Announcement $a) => [
                'loc' => '/announcements/'.$a->slug,
                'lastmod' => $a->updated_at?->toISOString(),
            ]);

        $pages = Page::query()
            ->where('is_active', true)
            ->select('slug', 'updated_at')
            ->get()
            ->map(fn (Page $p) => [
                'loc' => '/'.$p->slug,
                'lastmod' => $p->updated_at?->toISOString(),
            ]);

        return response()->json([
            'courses' => $courses,
            'announcements' => $announcements,
            'pages' => $pages,
        ]);
    }
}
