<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class AgentSystemReferenceContractTest extends TestCase
{
    use AgentSystemContractSupport;
    use ProjectFileAssertionSupport;

    public function testTemplatesReferenceKnownGuardAndGateIds(): void
    {
        $catalog = $this->decodeAgentJson('.agents/checks/guard-catalog.json');
        $gates = $this->decodeAgentJson('.agents/checks/quality-gates.json');
        $guardIds = array_map(
            static fn (array $guard): string => (string) ($guard['id'] ?? ''),
            is_array($catalog['guards'] ?? null) ? $catalog['guards'] : []
        );
        $gateIds = array_map(
            static fn (array $gate): string => (string) ($gate['id'] ?? ''),
            is_array($gates['gates'] ?? null) ? $gates['gates'] : []
        );

        $planTemplate = $this->decodeAgentJson('.agents/templates/plan.template.json');
        $planGuardIds = is_array($planTemplate['guardrails']['required_guard_ids'] ?? null)
            ? $planTemplate['guardrails']['required_guard_ids']
            : [];
        $planGateIds = is_array($planTemplate['guardrails']['required_quality_gate_ids'] ?? null)
            ? $planTemplate['guardrails']['required_quality_gate_ids']
            : [];
        foreach ($planGuardIds as $id) {
            $this->assertContains($id, $guardIds, 'Unknown guard id in plan template: ' . (string) $id);
        }
        foreach ($planGateIds as $id) {
            $this->assertContains($id, $gateIds, 'Unknown gate id in plan template: ' . (string) $id);
        }

        $reviewCodeTemplate = $this->decodeAgentJson('.agents/templates/review-code.template.json');
        $reviewGuardIds = is_array($reviewCodeTemplate['checked_guard_ids'] ?? null)
            ? $reviewCodeTemplate['checked_guard_ids']
            : [];
        $reviewGateIds = is_array($reviewCodeTemplate['checked_quality_gate_ids'] ?? null)
            ? $reviewCodeTemplate['checked_quality_gate_ids']
            : [];
        foreach ($reviewGuardIds as $id) {
            $this->assertContains($id, $guardIds, 'Unknown guard id in review-code template: ' . (string) $id);
        }
        foreach ($reviewGateIds as $id) {
            $this->assertContains($id, $gateIds, 'Unknown gate id in review-code template: ' . (string) $id);
        }

        $reviewSecurityTemplate = $this->decodeAgentJson('.agents/templates/review-security.template.json');
        $secGuardIds = is_array($reviewSecurityTemplate['checked_guard_ids'] ?? null)
            ? $reviewSecurityTemplate['checked_guard_ids']
            : [];
        foreach ($secGuardIds as $id) {
            $this->assertContains($id, $guardIds, 'Unknown guard id in review-security template: ' . (string) $id);
        }
    }

    public function testRolePromptsReferenceGuardCatalog(): void
    {
        $plannerPrompt = $this->readProjectFile('.agents/prompts/planner.md');
        $executorPrompt = $this->readProjectFile('.agents/prompts/executor.md');
        $codeReviewerPrompt = $this->readProjectFile('.agents/prompts/reviewer-code.md');
        $securityReviewerPrompt = $this->readProjectFile('.agents/prompts/reviewer-security.md');

        $this->assertStringContainsString('.agents/checks/guard-catalog.json', $plannerPrompt);
        $this->assertStringContainsString('.agents/checks/quality-gates.json', $plannerPrompt);
        $this->assertStringContainsString('.agents/checks/guard-catalog.json', $executorPrompt);
        $this->assertStringContainsString('.agents/checks/quality-gates.json', $executorPrompt);
        $this->assertStringContainsString('.agents/checks/guard-catalog.json', $codeReviewerPrompt);
        $this->assertStringContainsString('.agents/checks/guard-catalog.json', $securityReviewerPrompt);
    }
}
