"""
Kohl's Rewards Visa (Capital One) statement parser tests — Story 9.8.

Fixture: tests/fixtures/pdfs/kohls_visa_0532.pdf
Source:  Kohl's Visa ending 0532, period Apr 26 – May 26, 2026
"""
import pytest
from decimal import Decimal
from datetime import date

FIXTURE_PATH = "tests/fixtures/pdfs/kohls_visa_0532.pdf"


@pytest.fixture(scope="module")
def parsed():
    from app.services.pdf_parsers.kohls import parse
    return parse(FIXTURE_PATH)


class TestKohlsParserBasics:
    def test_no_errors(self, parsed):
        _, errors = parsed
        assert errors == [], f"Unexpected errors: {errors}"

    def test_transaction_count(self, parsed):
        txns, _ = parsed
        assert len(txns) == 3

    def test_issuer_is_kohls(self, parsed):
        txns, _ = parsed
        for t in txns:
            assert t.issuer == "kohls"

    def test_all_dates_are_2026(self, parsed):
        txns, _ = parsed
        for t in txns:
            assert t.date.year == 2026

    def test_all_amounts_positive(self, parsed):
        txns, _ = parsed
        for t in txns:
            assert t.amount > Decimal("0")

    def test_all_amounts_are_decimal(self, parsed):
        txns, _ = parsed
        for t in txns:
            assert isinstance(t.amount, Decimal)

    def test_all_have_dedup_hash(self, parsed):
        txns, _ = parsed
        for t in txns:
            assert t.dedup_hash and len(t.dedup_hash) == 64


class TestKohlsParserTransactions:
    def test_chunky_restaurant_charge(self, parsed):
        txns, _ = parsed
        chunky = next(t for t in txns if "CHUNKYS" in t.merchant_raw.upper())
        assert chunky.amount == Decimal("80.09")
        assert chunky.date == date(2026, 4, 29)
        assert chunky.is_credit is False

    def test_chunky_normalized_name(self, parsed):
        txns, _ = parsed
        chunky = next(t for t in txns if "CHUNKYS" in t.merchant_raw.upper())
        # Should strip the " - ManchesterNH" location artifact
        assert "Manchester" not in chunky.merchant_normalized
        assert "NH" not in chunky.merchant_normalized

    def test_walgreens_charge(self, parsed):
        txns, _ = parsed
        wg = next(t for t in txns if "WALGREENS" in t.merchant_raw.upper())
        assert wg.amount == Decimal("30.00")
        assert wg.date == date(2026, 5, 1)
        assert wg.merchant_normalized == "Walgreens"

    def test_kohls_charge(self, parsed):
        txns, _ = parsed
        kohls = next(t for t in txns if "KOHLS" in t.merchant_raw.upper())
        assert kohls.amount == Decimal("29.63")
        assert kohls.date == date(2026, 5, 7)
        assert kohls.merchant_normalized == "Kohl's"

    def test_no_payments_extracted(self, parsed):
        """Statement has $0 payments — no credits should appear."""
        txns, _ = parsed
        credits = [t for t in txns if t.is_credit]
        assert credits == []

    def test_total_matches_statement(self, parsed):
        """Statement shows Transactions: $139.72."""
        txns, _ = parsed
        total = sum(t.amount for t in txns if not t.is_credit)
        assert total == Decimal("139.72")


class TestKohlsIssuerDetection:
    def test_detects_kohls_from_pdf(self):
        import pdfplumber
        from app.services.pdf_parsers.detector import detect_issuer
        with pdfplumber.open(FIXTURE_PATH) as pdf:
            text = pdf.pages[0].extract_text() or ""
        issuer, conf = detect_issuer(text)
        assert issuer == "kohls"
        assert conf >= 0.70
