# -*- coding: utf-8 -*-
"""task-2673 PR watcher classifier — pytest fixtures (poll #12 박제 등)."""
from __future__ import annotations

import sys
from pathlib import Path
from typing import Any, Dict, List

import pytest

# worktree root 를 sys.path 에 주입 (기존 tests/lifecycle_guards 패턴 참고)
_WORKTREE = Path(__file__).resolve().parents[2]
if str(_WORKTREE) not in sys.path:
    sys.path.insert(0, str(_WORKTREE))

EXPECTED_HEAD = "4bb627fe9252acacc1c32007211807fe9905809f"
HEAD_COMMITTED_UTC = 1779714136  # 2026-05-25T13:02:16Z epoch
GEMINI_LOGIN = "gemini-code-assist"


def _critical_success() -> List[Dict[str, Any]]:
    return [
        {"name": n, "conclusion": "SUCCESS"}
        for n in (
            "cancel-kill-switch",
            "qc-check",
            "hidden-path-audit",
            "lock-in-check",
            "merge-safety-check",
            "ci/guard",
            "guard",
        )
    ]


@pytest.fixture
def expected_head() -> str:
    return EXPECTED_HEAD


@pytest.fixture
def fresh_gemini_review() -> Dict[str, Any]:
    return {
        "author": {"login": GEMINI_LOGIN},
        "submittedAt": "2026-05-25T13:39:36Z",
        "commit": {"oid": EXPECTED_HEAD},
    }


@pytest.fixture
def stale_gemini_review() -> Dict[str, Any]:
    return {
        "author": {"login": GEMINI_LOGIN},
        "submittedAt": "2026-05-25T12:00:00Z",
        "commit": {"oid": "0" * 40},
    }


@pytest.fixture
def poll_12_pr_data(fresh_gemini_review) -> Dict[str, Any]:
    """본 사고 박제 — poll #12 (t+1336s) 의 정확한 PR JSON 단면."""
    checks = _critical_success() + [
        {"name": "gemini-review-gate", "conclusion": "FAILURE"},
        {"name": "phase3-merge-gate", "conclusion": "FAILURE"},
    ]
    return {
        "number": 149,
        "headRefOid": EXPECTED_HEAD,
        "mergeable": "MERGEABLE",
        "mergeStateStatus": "BLOCKED",
        "reviewDecision": "",
        "statusCheckRollup": checks,
        "reviews": [fresh_gemini_review],
    }


@pytest.fixture
def poll_12_th_data() -> Dict[str, Any]:
    return {
        "data": {
            "repository": {
                "pullRequest": {
                    "reviewThreads": {
                        "nodes": [
                            {"id": "th1", "isResolved": False},
                            {"id": "th2", "isResolved": False},
                            {"id": "th3", "isResolved": False},
                        ]
                    }
                }
            }
        }
    }


@pytest.fixture
def merge_ready_pr_data(fresh_gemini_review) -> Dict[str, Any]:
    checks = _critical_success() + [
        {"name": "gemini-review-gate", "conclusion": "SUCCESS"},
        {"name": "phase3-merge-gate", "conclusion": "SUCCESS"},
        {"name": "doc-only-check", "conclusion": "SUCCESS"},
        {"name": "deploy-preview", "conclusion": "SUCCESS"},
    ]
    return {
        "number": 149,
        "headRefOid": EXPECTED_HEAD,
        "mergeable": "MERGEABLE",
        "mergeStateStatus": "CLEAN",
        "reviewDecision": "APPROVED",
        "statusCheckRollup": checks,
        "reviews": [fresh_gemini_review],
    }


@pytest.fixture
def all_resolved_th_data() -> Dict[str, Any]:
    return {
        "data": {
            "repository": {
                "pullRequest": {
                    "reviewThreads": {
                        "nodes": [
                            {"id": "th1", "isResolved": True},
                            {"id": "th2", "isResolved": True},
                        ]
                    }
                }
            }
        }
    }


@pytest.fixture
def empty_th_data() -> Dict[str, Any]:
    return {
        "data": {
            "repository": {
                "pullRequest": {
                    "reviewThreads": {"nodes": []}
                }
            }
        }
    }


@pytest.fixture
def critical_failure_pr_data() -> Dict[str, Any]:
    checks = _critical_success()
    for c in checks:
        if c["name"] == "qc-check":
            c["conclusion"] = "FAILURE"
            break
    return {
        "number": 149,
        "headRefOid": EXPECTED_HEAD,
        "mergeable": "UNKNOWN",
        "mergeStateStatus": "BLOCKED",
        "reviewDecision": "",
        "statusCheckRollup": checks,
        "reviews": [],
    }


@pytest.fixture
def head_drift_pr_data(fresh_gemini_review) -> Dict[str, Any]:
    return {
        "number": 149,
        "headRefOid": "f" * 40,  # 다른 head
        "mergeable": "UNKNOWN",
        "mergeStateStatus": "BEHIND",
        "reviewDecision": "",
        "statusCheckRollup": _critical_success(),
        "reviews": [fresh_gemini_review],
    }
