import base64

import pytest
from fastapi.testclient import TestClient

from src.main import app


@pytest.fixture
def client():
    return TestClient(app)


def test_extract_valid_zugferd(client):
    with open("tests/fixtures/EN16931_Einfach.pdf", "rb") as f:
        pdf_base64 = base64.b64encode(f.read()).decode()

    response = client.post("/extract", json={"pdf_base64": pdf_base64})
    assert response.status_code == 200
    data = response.json()
    assert data["is_zugferd"] is True
    assert data["zugferd_profil"] == "EN16931"
    assert "xml_raw" in data
    assert "xml_data" in data
    assert "pdf_text" in data
    assert "extraction_meta" in data


def test_extract_non_zugferd(client):
    with open("tests/fixtures/EmptyPDFA1.pdf", "rb") as f:
        pdf_base64 = base64.b64encode(f.read()).decode()

    response = client.post("/extract", json={"pdf_base64": pdf_base64})
    assert response.status_code == 200
    data = response.json()
    assert data["is_zugferd"] is False
    assert data["zugferd_profil"] is None
    assert "pdf_text" in data
    assert "extraction_meta" in data


def test_extract_invalid_base64(client):
    response = client.post("/extract", json={"pdf_base64": "invalid!!!"})
    assert response.status_code == 400
    data = response.json()
    assert data["error"] == "invalid_base64"
    assert "message" in data


def test_extract_non_pdf(client):
    pdf_base64 = base64.b64encode(b"Hello World").decode()
    response = client.post("/extract", json={"pdf_base64": pdf_base64})
    assert response.status_code == 400
    data = response.json()
    assert "error" in data


def test_validate_pflichtfelder(client):
    response = client.post(
        "/validate",
        json={
            "xml_data": {
                "invoice_number": "RE-001",
                "invoice_date": "2025-02-04",
                "supplier": {"name": "Test GmbH", "vat_id": "DE123456789"},
                "buyer": {"name": "Kunde AG"},
                "totals": {
                    "net": 100.0,
                    "gross": 119.0,
                    "vat_total": 19.0,
                    "line_total_sum": 100.0,
                },
                "line_items": [
                    {
                        "position": 1,
                        "description": "Test",
                        "quantity": 1.0,
                        "unit": "Stück",
                        "unit_price": 100.0,
                        "line_total": 100.0,
                    }
                ],
            },
            "checks": ["pflichtfelder"],
        },
    )
    assert response.status_code == 200
    data = response.json()
    assert "result" in data
    assert "is_valid" in data["result"]


def test_validate_empty_checks(client):
    response = client.post("/validate", json={"xml_data": {}, "checks": []})
    assert response.status_code == 200
