<?php

namespace App\Http\Controllers;

use App\Models\AppSetting;
use Illuminate\Http\Response;

class RobotsController extends Controller
{
    public function index(): Response
    {
        $baseUrl = config('app.url');
        $settings = AppSetting::getAllCached();

        $robots = "User-agent: *\n";
        $robots .= "Allow: /\n";
        $robots .= "Disallow: /admin/\n";
        $robots .= "Disallow: /api/\n";
        $robots .= "Disallow: /auth/\n";
        $robots .= "Disallow: /dashboard/\n";
        $robots .= "Disallow: /*?*\n";
        $robots .= "Disallow: /*.json$\n";
        $robots .= "Disallow: /search\n";
        $robots .= "\n";

        // Sitemap location
        $robots .= "Sitemap: {$baseUrl}/sitemap.xml\n";

        // Google Search Console
        if (!empty($settings['google_search_console'])) {
            $robots .= "\n# Google Search Console Verification\n";
            $robots .= "# " . $settings['google_search_console'] . "\n";
        }

        // Crawl delay for non-Googlebot crawlers
        $robots .= "\nUser-agent: *\n";
        $robots .= "Crawl-delay: 1\n";

        return response($robots, 200)
            ->header('Content-Type', 'text/plain');
    }
}
