"""Verifier for wfl-001: Conditional File Processing."""

import json
from pathlib import Path

import pytest


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


@pytest.fixture
def config(workspace):
    """Load the config file."""
    path = workspace / "config.json"
    assert path.exists(), "config.json not found in workspace"
    return json.loads(path.read_text())


@pytest.fixture
def input_text(workspace):
    """Read the input text."""
    path = workspace / "input.txt"
    assert path.exists(), "input.txt not found in workspace"
    return path.read_text()


@pytest.fixture
def output_text(workspace):
    """Read the output text."""
    path = workspace / "output.txt"
    assert path.exists(), "output.txt not found in workspace"
    return path.read_text()


def test_output_file_exists(workspace):
    """output.txt must exist in the workspace."""
    assert (workspace / "output.txt").exists(), "output.txt was not created"


def test_output_not_empty(output_text):
    """Output file must not be empty."""
    assert len(output_text.strip()) > 0, "output.txt is empty"


def test_uppercase_mode_applied(config, input_text, output_text):
    """If mode is uppercase, all text should be uppercased."""
    if config["mode"] != "uppercase":
        pytest.skip("Mode is not uppercase")
    expected = input_text.upper()
    assert output_text.strip() == expected.strip(), (
        "Uppercase transformation not correctly applied"
    )


def test_lowercase_mode_applied(config, input_text, output_text):
    """If mode is lowercase, all text should be lowercased."""
    if config["mode"] != "lowercase":
        pytest.skip("Mode is not lowercase")
    expected = input_text.lower()
    assert output_text.strip() == expected.strip(), (
        "Lowercase transformation not correctly applied"
    )


def test_reverse_mode_applied(config, input_text, output_text):
    """If mode is reverse, lines should be in reverse order."""
    if config["mode"] != "reverse":
        pytest.skip("Mode is not reverse")
    input_lines = input_text.strip().split("\n")
    output_lines = output_text.strip().split("\n")
    input_lines.reverse()
    assert output_lines == input_lines, "Reverse transformation not correctly applied"


def test_line_count_preserved(config, input_text, output_text):
    """The number of non-empty lines should be preserved."""
    input_lines = [l for l in input_text.strip().split("\n") if l.strip()]
    output_lines = [l for l in output_text.strip().split("\n") if l.strip()]
    assert len(output_lines) == len(input_lines), (
        f"Expected {len(input_lines)} lines, got {len(output_lines)}"
    )


def test_correct_transformation_for_default_config(config, output_text):
    """Verify the default config (uppercase) produces expected content."""
    if config["mode"] == "uppercase":
        assert "HELLO WORLD" in output_text, "Expected 'HELLO WORLD' in uppercase output"
        assert "THIS IS A TEST FILE" in output_text, (
            "Expected 'THIS IS A TEST FILE' in uppercase output"
        )
