<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Storage;

class SitemapController extends Controller
{
    public function status(): array
    {
        $path = tenant('id') . '/sitemap.xml';
        $exists = Storage::disk('file-storage')->exists($path);
        $lastModified = $exists ? Storage::disk('file-storage')->lastModified($path) : null;

        return [
            'exists' => $exists,
            'last_modified' => $lastModified,
            'url' => $exists ? Storage::disk('file-storage')->url($path) . '?t=' . $lastModified : null,
        ];
    }

    public function generate(): Response
    {
        Artisan::call('make:sitemap');

        return response(['success' => true, 'message' => 'Sitemap generated successfully']);
    }
}
