"""
test_qc_integration.py - Phase 1-3 통합 테스트 (아르고스 작성)

테스트 항목:
1. team_prompts normal 레벨: selfcheck + QC-RULES.md 참조 확인
2. team_prompts critical 레벨: 마아트 키워드 + QC-RULES.md 참조
3. team_prompts security 레벨: 마아트 + 로키 키워드 + QC-RULES.md 참조
4. 기존 테스트 회귀: tests/test_team_prompts.py 전체 통과 확인
5. qc_verify.py end-to-end: task-4.4로 실행하여 JSON 출력 확인

Note: 경량화 리팩토링(task-210.1) 이후 _build_verification_section()은
인라인 대신 QC-RULES.md 파일 참조 + 레벨별 요약 1줄만 출력합니다.
"""

import json
import os
import subprocess
import sys
from pathlib import Path

import pytest

_WORKSPACE = Path(os.environ.get("WORKSPACE_ROOT", "/home/jay/workspace"))

if str(_WORKSPACE) not in sys.path:
    sys.path.insert(0, str(_WORKSPACE))

import prompts.team_prompts as tp

_QC_VERIFY_PATH = str(_WORKSPACE / "teams/dev1/qc/qc_verify.py")
_TEST_TEAM_PROMPTS_PATH = str(_WORKSPACE / "tests/test_team_prompts.py")


# ---------------------------------------------------------------------------
# 1. team_prompts normal 레벨
# ---------------------------------------------------------------------------


class TestVerificationSectionNormal:
    """normal 레벨: selfcheck + QC-RULES.md 참조"""

    def test_normal_contains_selfcheck(self):
        result = tp._build_verification_section("normal")
        assert "셀프 QC" in result

    def test_normal_contains_qc_rules_ref(self):
        result = tp._build_verification_section("normal")
        assert "QC-RULES.md" in result

    def test_normal_contains_auto_verify_keyword(self):
        result = tp._build_verification_section("normal")
        assert "자동 검증" in result

    def test_normal_no_maat_no_loki(self):
        result = tp._build_verification_section("normal")
        # gate_text 이전의 base 부분만 검증 (게이트 지시는 별도 모듈의 책임)
        base_section = result.split("## 게이트 지시")[0] if "## 게이트 지시" in result else result
        assert "마아트" not in base_section
        assert "로키" not in base_section


# ---------------------------------------------------------------------------
# 2. team_prompts critical 레벨
# ---------------------------------------------------------------------------


class TestVerificationSectionCritical:
    """critical 레벨: 마아트 + 독립 검증 + QC-RULES.md 참조"""

    def test_critical_contains_maat(self):
        result = tp._build_verification_section("critical")
        assert "마아트" in result

    def test_critical_contains_independent_verification(self):
        result = tp._build_verification_section("critical")
        assert "독립 검증" in result

    def test_critical_contains_qc_rules_ref(self):
        result = tp._build_verification_section("critical")
        assert "QC-RULES.md" in result

    def test_critical_no_loki(self):
        result = tp._build_verification_section("critical")
        assert "로키" not in result

    def test_critical_contains_selfcheck(self):
        result = tp._build_verification_section("critical")
        assert "셀프 QC" in result


# ---------------------------------------------------------------------------
# 3. team_prompts security 레벨
# ---------------------------------------------------------------------------


class TestVerificationSectionSecurity:
    """security 레벨: 마아트 + 로키 + QC-RULES.md 참조"""

    def test_security_contains_maat(self):
        result = tp._build_verification_section("security")
        assert "마아트" in result

    def test_security_contains_loki(self):
        result = tp._build_verification_section("security")
        assert "로키" in result

    def test_security_contains_audit_keyword(self):
        result = tp._build_verification_section("security")
        assert "보안 감사" in result

    def test_security_contains_qc_rules_ref(self):
        result = tp._build_verification_section("security")
        assert "QC-RULES.md" in result

    def test_security_contains_selfcheck(self):
        result = tp._build_verification_section("security")
        assert "셀프 QC" in result


# ---------------------------------------------------------------------------
# 4. 기존 테스트 회귀: test_team_prompts.py 전체 통과
# ---------------------------------------------------------------------------


class TestRegressionTeamPrompts:
    """기존 test_team_prompts.py 전체가 여전히 통과하는지 subprocess로 검증"""

    def test_team_prompts_tests_all_pass(self):
        proc = subprocess.run(
            ["python3", "-m", "pytest", _TEST_TEAM_PROMPTS_PATH, "-v", "--tb=short"],
            capture_output=True,
            text=True,
            timeout=120,
            cwd=str(_WORKSPACE),
        )
        assert proc.returncode == 0, (
            f"기존 test_team_prompts.py 실패!\n" f"stdout: {proc.stdout[-1000:]}\n" f"stderr: {proc.stderr[-300:]}"
        )
        assert "failed" not in proc.stdout.lower() or "0 failed" in proc.stdout.lower()


# ---------------------------------------------------------------------------
# 5. qc_verify.py end-to-end: task-4.4
# ---------------------------------------------------------------------------


class TestEndToEndQCVerify:
    """qc_verify.py --task-id task-4.4 --skip api_health,test_runner 실행 및 JSON 검증"""

    @pytest.fixture(scope="class")
    def qc_result(self):
        proc = subprocess.run(
            [
                "python3",
                _QC_VERIFY_PATH,
                "--task-id",
                "task-4.4",
                "--skip",
                "api_health,test_runner,full_suite_check",
            ],
            capture_output=True,
            text=True,
            timeout=30,
        )
        return proc, json.loads(proc.stdout.strip())

    def test_e2e_outputs_valid_json(self, qc_result):
        proc, parsed = qc_result
        assert isinstance(parsed, dict), "stdout가 유효한 JSON이어야 함"

    def test_e2e_task_id_correct(self, qc_result):
        _, parsed = qc_result
        assert parsed["task_id"] == "task-4.4"

    def test_e2e_has_all_check_keys(self, qc_result):
        _, parsed = qc_result
        checks = parsed.get("checks", {})
        for check_name in ("api_health", "file_check", "data_integrity", "test_runner"):
            assert check_name in checks, f"checks에 '{check_name}' 누락"

    def test_e2e_skipped_checks_are_skip(self, qc_result):
        _, parsed = qc_result
        checks = parsed["checks"]
        assert checks["api_health"]["status"] == "SKIP"
        # test_runner는 MANDATORY이므로 --skip 시도 시 차단되어 FAIL 반환
        assert checks["test_runner"]["status"] in ("FAIL", "SKIP"), \
            f"test_runner는 MANDATORY — skip 차단으로 FAIL 또는 정상 실행 SKIP 기대, got: {checks['test_runner']['status']}"

    def test_e2e_data_integrity_is_fail(self, qc_result):
        """task-4.4는 completed지만 .done 파일 없음 → data_integrity=FAIL"""
        _, parsed = qc_result
        assert parsed["checks"]["data_integrity"]["status"] == "FAIL"

    def test_e2e_overall_is_fail(self, qc_result):
        """FAIL 항목이 있으므로 overall=FAIL"""
        _, parsed = qc_result
        assert parsed["overall"] == "FAIL"

    def test_e2e_summary_contains_fail(self, qc_result):
        _, parsed = qc_result
        assert "FAIL" in parsed.get("summary", "")

    def test_e2e_exit_code_nonzero(self, qc_result):
        proc, parsed = qc_result
        if parsed["overall"] == "FAIL":
            assert proc.returncode == 1
