<?php

namespace App\SiteReadiness\Checks;

use App\SiteReadiness\CheckResult;
use App\SiteReadiness\ReadinessCheck;
use App\SiteReadiness\Status;

class RobotsTxtCheck implements ReadinessCheck
{
    public function group(): string { return 'Integrations'; }
    public function label(): string { return 'robots.txt allows crawling'; }
    public function hint(): string { return 'Allow crawling in robots.txt before go-live.'; }

    public function run(): CheckResult
    {
        $path = public_path('robots.txt');

        if (!is_file($path)) {
            return new CheckResult(Status::WARN, 'No robots.txt present.');
        }

        $contents  = (string) file_get_contents($path);
        $blocksAll = (bool) preg_match('/^\s*Disallow:\s*\/\s*$/mi', $contents);

        if ($blocksAll) {
            $prod = config('app.env') === 'production';
            return new CheckResult(
                $prod ? Status::FAIL : Status::WARN,
                'robots.txt is blocking all crawling (Disallow: /).',
            );
        }

        return new CheckResult(Status::OK, 'Crawling allowed.');
    }
}
