<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class CodexAutomationContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    public function testCodexSkillsSyncScriptSupportsCheckAndApply(): void
    {
        $script = $this->readProjectFile('bin/codex-skills-sync.sh');
        $this->assertStringContainsString('--check', $script);
        $this->assertStringContainsString('--apply', $script);
        $this->assertStringContainsString('mode="check"', $script);
        $this->assertStringContainsString('"core-guardrails"', $script);
        $this->assertStringContainsString('"starterkit-planner"', $script);
        $this->assertStringContainsString('"starterkit-grid-standards"', $script);
        $this->assertStringContainsString('"starterkit-php-style-ci"', $script);
    }

    public function testDocsLinkCheckScriptExcludesHistoricalRunArtifactsByDefault(): void
    {
        $script = $this->readProjectFile('bin/docs-link-check.sh');
        $this->assertStringContainsString("!.agents/runs/**", $script);
    }

    public function testQualityGatesIncludeDocsLinksAndSkillsSyncChecks(): void
    {
        $qualityGates = json_decode($this->readProjectFile('.agents/checks/quality-gates.json'), true);
        $this->assertIsArray($qualityGates, 'Invalid quality-gates.json');
        $gates = is_array($qualityGates['gates'] ?? null) ? $qualityGates['gates'] : [];

        $byId = [];
        foreach ($gates as $gate) {
            $id = (string) ($gate['id'] ?? '');
            $byId[$id] = $gate;
        }

        $this->assertArrayHasKey('QG-008', $byId);
        $this->assertArrayHasKey('QG-009', $byId);
        $this->assertSame('bin/docs-link-check.sh', (string) ($byId['QG-008']['command'] ?? ''));
        $this->assertSame('bin/codex-skills-sync.sh --check', (string) ($byId['QG-009']['command'] ?? ''));
    }
}
