<?php

namespace App\Http\Controllers\Api\PublicApi;

use App\Http\Controllers\Controller;
use App\Models\BlogPost;
use App\Models\Company;
use App\Models\ConsultantProfile;
use App\Models\Listing;

/**
 * Frontend sitemap.xml üretimi için hafif slug listeleri.
 * Yalnızca public yüzeyde görünen kayıtlar (yayında/aktif/onaylı) döner.
 */
class SitemapController extends Controller
{
    public function index()
    {
        $mapSlugs = fn ($rows) => $rows
            ->map(fn ($row) => [
                'slug' => $row->slug,
                'updated_at' => $row->updated_at?->toISOString(),
            ])
            ->values();

        return response()->json([
            'data' => [
                'listings' => $mapSlugs(
                    Listing::query()->published()
                        ->orderByDesc('published_at')
                        ->get(['slug', 'updated_at'])
                ),
                'blog_posts' => $mapSlugs(
                    BlogPost::query()->published()
                        ->orderByDesc('published_at')
                        ->get(['slug', 'updated_at'])
                ),
                'consultants' => $mapSlugs(
                    ConsultantProfile::query()->active()
                        ->get(['slug', 'updated_at'])
                ),
                'companies' => $mapSlugs(
                    Company::query()->approved()->active()
                        ->get(['slug', 'updated_at'])
                ),
            ],
        ]);
    }
}
