from __future__ import annotations

import json
import tempfile
import unittest
from io import BytesIO
from pathlib import Path

from reportlab.pdfgen import canvas

from us_cpa.renderers import render_case_forms
from us_cpa.sources import TaxYearCorpus


class RendererTests(unittest.TestCase):
    def test_render_case_forms_prefers_fillable_pdf_fields_when_available(self) -> None:
        with tempfile.TemporaryDirectory() as temp_dir:
            case_dir = Path(temp_dir) / "case"
            (case_dir / "output").mkdir(parents=True)
            corpus = TaxYearCorpus(cache_root=Path(temp_dir) / "cache")
            irs_dir = corpus.paths_for_year(2025).irs_dir
            irs_dir.mkdir(parents=True, exist_ok=True)

            buffer = BytesIO()
            pdf = canvas.Canvas(buffer)
            form = pdf.acroForm
            pdf.drawString(72, 720, "Name")
            form.textfield(name="taxpayer_full_name", x=120, y=710, width=200, height=20)
            pdf.drawString(72, 680, "Wages")
            form.textfield(name="wages", x=120, y=670, width=200, height=20)
            pdf.save()
            (irs_dir / "f1040.pdf").write_bytes(buffer.getvalue())

            normalized = {
                "taxYear": 2025,
                "requiredForms": ["f1040"],
                "taxpayer": {"fullName": "Jane Doe"},
                "filingStatus": "single",
                "income": {"wages": 50000.0, "taxableInterest": 100.0, "businessIncome": 0.0, "capitalGainLoss": 0.0, "rentalIncome": 0.0},
                "deductions": {"standardDeduction": 15750.0, "deductionType": "standard", "deductionAmount": 15750.0},
                "adjustments": {"hsaContribution": 0.0},
                "credits": {"educationCredit": 0.0, "foreignTaxCredit": 0.0, "energyCredit": 0.0},
                "taxes": {"totalTax": 3883.5, "additionalMedicareTax": 0.0, "netInvestmentIncomeTax": 0.0, "alternativeMinimumTax": 0.0, "additionalTaxPenalty": 0.0},
                "payments": {"federalWithholding": 6000.0},
                "business": {"qualifiedBusinessIncome": 0.0},
                "basis": {"traditionalIraBasis": 0.0},
                "depreciation": {"depreciationExpense": 0.0},
                "assetSales": {"section1231GainLoss": 0.0},
                "totals": {"adjustedGrossIncome": 50100.0, "taxableIncome": 34350.0, "refund": 2116.5, "balanceDue": 0.0},
            }

            artifacts = render_case_forms(case_dir, corpus, normalized)

            self.assertEqual(artifacts["artifacts"][0]["renderMethod"], "field_fill")
            self.assertFalse(artifacts["artifacts"][0]["reviewRequired"])

    def test_render_case_forms_writes_overlay_artifacts_and_flags_review(self) -> None:
        with tempfile.TemporaryDirectory() as temp_dir:
            case_dir = Path(temp_dir) / "case"
            (case_dir / "output").mkdir(parents=True)
            corpus = TaxYearCorpus(cache_root=Path(temp_dir) / "cache")
            irs_dir = corpus.paths_for_year(2025).irs_dir
            irs_dir.mkdir(parents=True, exist_ok=True)

            buffer = BytesIO()
            pdf = canvas.Canvas(buffer)
            pdf.drawString(72, 720, "Template")
            pdf.save()
            (irs_dir / "f1040.pdf").write_bytes(buffer.getvalue())

            normalized = {
                "taxYear": 2025,
                "requiredForms": ["f1040"],
                "taxpayer": {"fullName": "Jane Doe"},
                "filingStatus": "single",
                "income": {"wages": 50000.0, "taxableInterest": 100.0, "businessIncome": 0.0, "capitalGainLoss": 0.0, "rentalIncome": 0.0},
                "deductions": {"standardDeduction": 15750.0, "deductionType": "standard", "deductionAmount": 15750.0},
                "adjustments": {"hsaContribution": 0.0},
                "credits": {"educationCredit": 0.0, "foreignTaxCredit": 0.0, "energyCredit": 0.0},
                "taxes": {"totalTax": 3883.5, "additionalMedicareTax": 0.0, "netInvestmentIncomeTax": 0.0, "alternativeMinimumTax": 0.0, "additionalTaxPenalty": 0.0},
                "payments": {"federalWithholding": 6000.0},
                "business": {"qualifiedBusinessIncome": 0.0},
                "basis": {"traditionalIraBasis": 0.0},
                "depreciation": {"depreciationExpense": 0.0},
                "assetSales": {"section1231GainLoss": 0.0},
                "totals": {"adjustedGrossIncome": 50100.0, "taxableIncome": 34350.0, "refund": 2116.5, "balanceDue": 0.0},
            }

            artifacts = render_case_forms(case_dir, corpus, normalized)

            self.assertEqual(artifacts["artifactCount"], 1)
            self.assertEqual(artifacts["artifacts"][0]["renderMethod"], "overlay")
            self.assertTrue(artifacts["artifacts"][0]["reviewRequired"])
            self.assertTrue((case_dir / "output" / "forms" / "f1040.pdf").exists())
            manifest = json.loads((case_dir / "output" / "artifacts.json").read_text())
            self.assertEqual(manifest["artifacts"][0]["formCode"], "f1040")


if __name__ == "__main__":
    unittest.main()
