<?php

namespace App\Http\Controllers;

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

class SitemapController extends Controller
{
    public function status(): array
    {
        $filename = Settings::get('seo-sitemap-split') ? 'sitemap-index.xml' : 'sitemap.xml';
        $path = tenant('id') . '/' . $filename;
        $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']);
    }
}
