"""Verifier for mem-002: Multi-Step Instruction Following."""

import pytest
from pathlib import Path


@pytest.fixture
def workspace(request):
    """Resolve the workspace path from the --workspace CLI flag."""
    return Path(request.config.getoption("--workspace"))


# The 3rd words from words.txt are:
# brown, lazy, seashells, crimson, magnificent, faithful, ocean, step
# Sorted case-insensitive: brown, crimson, faithful, lazy, magnificent, ocean, seashells, step
EXPECTED_SORTED = ["brown", "crimson", "faithful", "lazy", "magnificent", "ocean", "seashells", "step"]
# Total chars: 5+7+8+4+11+5+9+4 = 53
EXPECTED_CHAR_COUNT = 53
EXPECTED_REMAINDER = 53 % 7  # = 4


def test_steps_file_exists(workspace):
    """steps.txt must exist."""
    assert (workspace / "steps.txt").exists(), "steps.txt not found"


def test_steps_file_has_all_steps(workspace):
    """steps.txt must contain entries for all 5 steps."""
    content = (workspace / "steps.txt").read_text().strip()
    lines = content.splitlines()
    assert any("STEP 1" in line for line in lines), "STEP 1 not recorded"
    assert any("STEP 2" in line for line in lines), "STEP 2 not recorded"
    assert any("STEP 3" in line for line in lines), "STEP 3 not recorded"
    assert any("STEP 4" in line for line in lines), "STEP 4 not recorded"
    assert any("STEP 5" in line for line in lines), "STEP 5 not recorded"


def test_steps_in_order(workspace):
    """Steps must appear in order 1-5 in steps.txt."""
    content = (workspace / "steps.txt").read_text().strip()
    lines = content.splitlines()
    step_nums = []
    for line in lines:
        for i in range(1, 6):
            if f"STEP {i}" in line:
                step_nums.append(i)
                break
    assert step_nums == [1, 2, 3, 4, 5], f"Steps not in order: {step_nums}"


def test_extracted_file_exists(workspace):
    """extracted.txt must exist."""
    assert (workspace / "extracted.txt").exists(), "extracted.txt not found"


def test_extracted_sorted_correctly(workspace):
    """extracted.txt must contain sorted 3rd words."""
    content = (workspace / "extracted.txt").read_text().strip()
    words = [w.strip() for w in content.splitlines() if w.strip()]
    assert len(words) == 8, f"Expected 8 words, got {len(words)}"
    # Check that sorting is correct (case-insensitive)
    assert words == sorted(words, key=str.lower), (
        f"Words not sorted correctly: {words}"
    )
    # Check actual words are correct
    assert set(words) == set(EXPECTED_SORTED), (
        f"Expected words {set(EXPECTED_SORTED)}, got {set(words)}"
    )


def test_char_count_file(workspace):
    """char_count.txt must contain the correct character count."""
    path = workspace / "char_count.txt"
    assert path.exists(), "char_count.txt not found"
    content = path.read_text().strip()
    assert content == str(EXPECTED_CHAR_COUNT), (
        f"Expected char count '{EXPECTED_CHAR_COUNT}', got '{content}'"
    )


def test_remainder_file(workspace):
    """remainder.txt must contain the correct remainder."""
    path = workspace / "remainder.txt"
    assert path.exists(), "remainder.txt not found"
    content = path.read_text().strip()
    assert content == str(EXPECTED_REMAINDER), (
        f"Expected remainder '{EXPECTED_REMAINDER}', got '{content}'"
    )


def test_summary_file(workspace):
    """summary.txt must contain first word, last word, and remainder."""
    path = workspace / "summary.txt"
    assert path.exists(), "summary.txt not found"
    content = path.read_text().strip()
    lines = content.splitlines()
    assert len(lines) == 3, f"Expected 3 lines in summary.txt, got {len(lines)}"
    assert lines[0].strip() == EXPECTED_SORTED[0], (
        f"First word should be '{EXPECTED_SORTED[0]}', got '{lines[0].strip()}'"
    )
    assert lines[1].strip() == EXPECTED_SORTED[-1], (
        f"Last word should be '{EXPECTED_SORTED[-1]}', got '{lines[1].strip()}'"
    )
    assert lines[2].strip() == str(EXPECTED_REMAINDER), (
        f"Remainder should be '{EXPECTED_REMAINDER}', got '{lines[2].strip()}'"
    )
