"""
test_symbol_existence_check.py - symbol_existence_check verifier 테스트

이리스(Iris) dev1팀 프론트엔드 개발자 작성
task-1882
"""

import sys

sys.path.insert(0, "/home/jay/workspace/teams/shared")

from verifiers.symbol_existence_check import verify


# ──────────────────────────────────────────────────────────
# 헬퍼 함수
# ──────────────────────────────────────────────────────────


def _create_report(base_dir, task_id, content: str) -> str:
    """보고서 파일 생성 헬퍼."""
    reports_dir = base_dir / "memory" / "reports"
    reports_dir.mkdir(parents=True, exist_ok=True)
    report_path = reports_dir / f"{task_id}.md"
    report_path.write_text(content, encoding="utf-8")
    return str(report_path)


def _create_python_file(base_dir, rel_path: str, content: str) -> str:
    """Python 파일 생성 헬퍼."""
    full_path = base_dir / rel_path.lstrip("/")
    full_path.parent.mkdir(parents=True, exist_ok=True)
    full_path.write_text(content, encoding="utf-8")
    return str(full_path)


# ──────────────────────────────────────────────────────────
# 테스트 케이스
# ──────────────────────────────────────────────────────────


def test_pass_symbol_exists(tmp_path):
    """보고서에 def verify 언급 + 실제 파일에 존재 → PASS"""
    task_id = "task-test-symbol-9901"

    # 실제 Python 파일 생성
    py_file_path = str(tmp_path / "home" / "jay" / "workspace" / "teams" / "shared" / "verifiers" / "my_check.py")
    py_rel = "home/jay/workspace/teams/shared/verifiers/my_check.py"
    _create_python_file(tmp_path, py_rel, "def verify(task_id):\n    return {'status': 'PASS'}\n")

    # 보고서 생성 (수정 파일 섹션 + 코드 블록에 def verify 포함)
    abs_path = str(tmp_path / py_rel)
    report_content = f"""# {task_id} 작업 보고서

## 수정 파일

| 파일 경로 | 변경 내용 |
|-----------|-----------|
| {abs_path} | verify 함수 추가 |

## 구현 내용

다음과 같이 verify 함수를 구현하였습니다:

```python
def verify(task_id):
    return {{'status': 'PASS'}}
```
"""
    _create_report(tmp_path, task_id, report_content)

    result = verify(task_id=task_id, workspace_root=str(tmp_path))

    assert result["status"] == "PASS", f"Expected PASS, got {result['status']}: {result['details']}"


def test_fail_symbol_missing(tmp_path):
    """보고서에 있지만 파일에 없는 심볼 → FAIL"""
    task_id = "task-test-symbol-9902"

    # 실제 Python 파일 생성 (심볼 없음)
    py_rel = "home/jay/workspace/teams/shared/verifiers/empty_check.py"
    _create_python_file(tmp_path, py_rel, "# 빈 파일\nx = 1\n")

    abs_path = str(tmp_path / py_rel)
    report_content = f"""# {task_id} 작업 보고서

## 수정 파일

| 파일 경로 | 변경 내용 |
|-----------|-----------|
| {abs_path} | nonexistent_function 추가 |

## 구현 내용

다음과 같이 nonexistent_function 함수를 구현하였습니다:

```python
def nonexistent_function(task_id):
    return {{'status': 'PASS'}}
```
"""
    _create_report(tmp_path, task_id, report_content)

    result = verify(task_id=task_id, workspace_root=str(tmp_path))

    assert result["status"] == "FAIL", f"Expected FAIL, got {result['status']}: {result['details']}"
    # MISSING 메시지가 details에 포함되어야 함
    details_str = " ".join(result["details"])
    assert "MISSING" in details_str or "nonexistent_function" in details_str


def test_skip_no_modified_files_section(tmp_path):
    """보고서에 수정 파일 섹션 없음 → SKIP"""
    task_id = "task-test-symbol-9903"

    report_content = f"""# {task_id} 작업 보고서

## 개요

이 작업에서는 특별한 파일 수정이 없었습니다.

## 구현 내용

개요만 작성하였습니다.
"""
    _create_report(tmp_path, task_id, report_content)

    result = verify(task_id=task_id, workspace_root=str(tmp_path))

    assert result["status"] == "SKIP", f"Expected SKIP, got {result['status']}: {result['details']}"
    # SKIP 사유가 details에 포함되어야 함
    details_str = " ".join(result["details"])
    assert "수정 파일" in details_str or "섹션" in details_str or "section" in details_str.lower()
