"""Verifier for eml-001: Parse Email Headers."""

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 headers(workspace):
    """Load and return the headers.json contents."""
    headers_path = workspace / "headers.json"
    assert headers_path.exists(), "headers.json not found in workspace"
    with open(headers_path) as f:
        return json.load(f)


def test_headers_file_exists(workspace):
    """headers.json must exist in the workspace."""
    assert (workspace / "headers.json").exists()


def test_from_field(headers):
    """From field must contain the correct sender."""
    assert "from" in headers, "Missing 'from' field"
    assert headers["from"] == "alice.johnson@techcorp.com"


def test_to_field(headers):
    """To field must contain the correct recipient."""
    assert "to" in headers, "Missing 'to' field"
    assert headers["to"] == "bob.smith@techcorp.com"


def test_subject_field(headers):
    """Subject field must contain the correct subject."""
    assert "subject" in headers, "Missing 'subject' field"
    assert headers["subject"] == "Q1 Budget Review Meeting"


def test_date_field(headers):
    """Date field must be present and contain the correct date."""
    assert "date" in headers, "Missing 'date' field"
    assert "2026" in headers["date"]
    assert "Mar" in headers["date"] or "03" in headers["date"]


def test_cc_field(headers):
    """CC field must contain both CC recipients."""
    assert "cc" in headers, "Missing 'cc' field"
    cc = headers["cc"]
    assert isinstance(cc, list), "CC should be an array"
    assert len(cc) == 2, f"Expected 2 CC recipients, got {len(cc)}"
    assert "carol.white@techcorp.com" in cc
    assert "dave.brown@techcorp.com" in cc


def test_all_fields_present(headers):
    """All five required fields must be present."""
    required = {"from", "to", "subject", "date", "cc"}
    assert required.issubset(headers.keys()), f"Missing fields: {required - set(headers.keys())}"
