Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

115 KB
Raw Blame
import uuid
from datetime import date, datetime, timedelta, timezone
from decimal import Decimal
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
from zoneinfo import ZoneInfo

import pytest
from fastapi import HTTPException
from sqlalchemy.ext.asyncio import AsyncSession

from app.models.account import Account
from app.models.asset import Asset
from app.models.asset_value import AssetValue
from app.models.bank_connection import BankConnection
from app.models.transaction import Transaction
from app.models.user import User
from app.schemas.report import (
    CategoryTrendItem,
    ReportDataPoint,
    ReportMeta,
    ReportResponse,
    ReportSummary,
)
from app.services import report_service
from app.api.reports import (
    _add_years,
    _financial_year_start_month,
    _reject_unsupported_fiscal_year_report,
    _resolve_custom_range,
)
from app.core.app_clock import today_in, use_resolved_timezone
from app.services.report_service import (
    _add_months,
    _asset_value_at,
    _date_points,
    _format_date_label,
    _net_worth_at,
    _report_start_date,
    get_cash_flow_report,
    get_net_worth_report,
)
from app.models.recurring_transaction import RecurringTransaction
from app.core.workspace_context import current_workspace
from app.main import app


# ---------------------------------------------------------------------------
# Pure-function tests: _format_date_label
# ---------------------------------------------------------------------------


def test_format_date_label_daily():
    assert _format_date_label(date(2025, 6, 15), "daily") == "2025-06-15"


def test_format_date_label_weekly():
    # 2025-06-16 is a Monday in ISO week 25
    assert _format_date_label(date(2025, 6, 16), "weekly") == "2025-W25"


def test_format_date_label_monthly():
    assert _format_date_label(date(2025, 1, 20), "monthly") == "2025-01"


def test_format_date_label_yearly():
    assert _format_date_label(date(2025, 3, 1), "yearly") == "2025"


def test_format_date_label_unknown_falls_back_to_iso():
    assert _format_date_label(date(2025, 3, 1), "unknown") == "2025-03-01"


# ---------------------------------------------------------------------------
# Pure-function tests: _date_points
# ---------------------------------------------------------------------------


def test_date_points_daily():
    start = date(2025, 1, 1)
    end = date(2025, 1, 5)
    points = _date_points(start, end, "daily")
    assert len(points) == 5
    assert points[0] == start
    assert points[-1] == end


def test_date_points_weekly():
    start = date(2025, 1, 1)
    end = date(2025, 1, 22)
    points = _date_points(start, end, "weekly")
    # 1st, 8th, 15th, 22nd = 4 points
    assert len(points) == 4
    assert points[0] == start
    assert points[-1] == end


def test_date_points_monthly():
    start = date(2025, 1, 1)
    end = date(2025, 6, 15)
    points = _date_points(start, end, "monthly")
    assert points[0] == date(2025, 1, 31)  # end of first month
    assert points[-1] == end               # last point capped at end
    assert len(points) == 6


def test_date_points_monthly_end_of_month_snapshots():
    """Monthly points: one EOM snapshot per month, last one capped at end."""
    start = date(2025, 1, 1)
    end = date(2025, 4, 15)
    points = _date_points(start, end, "monthly")
    assert points == [
        date(2025, 1, 31),  # end of Jan
        date(2025, 2, 28),  # end of Feb
        date(2025, 3, 31),  # end of Mar
        date(2025, 4, 15),  # capped at end (Apr 30 → Apr 15)
    ]


def test_date_points_monthly_december_start():
    """December start must not crash with month rollover."""
    start = date(2024, 12, 1)
    end = date(2025, 2, 15)
    points = _date_points(start, end, "monthly")
    assert points == [
        date(2024, 12, 31),
        date(2025, 1, 31),
        date(2025, 2, 15),
    ]


def test_date_points_monthly_feb_leap_year():
    """End-of-month for February respects leap years."""
    start = date(2024, 1, 1)
    end = date(2024, 3, 1)
    points = _date_points(start, end, "monthly")
    assert points == [
        date(2024, 1, 31),
        date(2024, 2, 29),
        date(2024, 3, 1),
    ]


def test_date_points_yearly():
    start = date(2023, 1, 1)
    end = date(2025, 6, 15)
    points = _date_points(start, end, "yearly")
    assert points[0] == start
    assert points[-1] == end


def test_date_points_empty_range():
    """Start after end returns single point."""
    start = date(2025, 1, 1)
    end = date(2025, 1, 1)
    points = _date_points(start, end, "daily")
    assert len(points) == 1


def test_date_points_unknown_interval_defaults_to_monthly():
    start = date(2025, 1, 1)
    end = date(2025, 3, 15)
    points = _date_points(start, end, "something_else")
    monthly = _date_points(start, end, "monthly")
    assert points == monthly


def test_date_points_last_point_replaces_same_period():
    """End date mid-month caps the last snapshot; no duplicate month labels."""
    start = date(2025, 1, 1)
    end = date(2025, 3, 15)
    points = _date_points(start, end, "monthly")
    assert points == [
        date(2025, 1, 31),
        date(2025, 2, 28),
        date(2025, 3, 15),  # Mar 31 capped at end
    ]
    labels = [_format_date_label(p, "monthly") for p in points]
    assert len(labels) == len(set(labels))


# ---------------------------------------------------------------------------
# Pure-function tests: _report_start_date
# ---------------------------------------------------------------------------


def test_report_start_date_month_range_aligns_to_month_start():
    assert _report_start_date(date(2025, 6, 15), 6) == date(2024, 12, 1)


def test_report_start_date_ytd_uses_current_year_start():
    assert _report_start_date(date(2025, 6, 15), 24, period="ytd") == date(2025, 1, 1)


def test_report_start_date_ytd_uses_april_for_indian_financial_year():
    assert _report_start_date(
        date(2026, 3, 31), 12, period="ytd", financial_year_start_month=4
    ) == date(2025, 4, 1)
    assert _report_start_date(
        date(2026, 4, 1), 12, period="ytd", financial_year_start_month=4
    ) == date(2026, 4, 1)


def test_report_start_date_ytd_rejects_invalid_financial_year_month():
    with pytest.raises(ValueError, match="financial_year_start_month"):
        _report_start_date(
            date(2025, 6, 15), 12, period="ytd", financial_year_start_month=13
        )


def test_report_start_date_days_window_is_exact_and_inclusive():
    # 30 days ending today (inclusive), not the month-aligned window months gives.
    assert _report_start_date(date(2025, 7, 7), 1, days=30) == date(2025, 6, 8)
    assert _report_start_date(date(2025, 3, 1), 1, days=30) == date(2025, 1, 31)


def test_report_start_date_days_overrides_months_and_period():
    assert _report_start_date(date(2025, 6, 15), 24, period="ytd", days=7) == date(2025, 6, 9)


# ---------------------------------------------------------------------------
# Service-level tests: get_net_worth_report (works with SQLite)
# ---------------------------------------------------------------------------


async def _create_manual_account(
    session: AsyncSession, user_id: uuid.UUID, name: str, balance: float = 0
) -> Account:
    account = Account(
        id=uuid.uuid4(),
        user_id=user_id,
        name=name,
        type="checking",
        balance=Decimal(str(balance)),
        currency="BRL",
        is_closed=False,
    )
    session.add(account)
    await session.commit()
    await session.refresh(account)
    return account


async def _create_transaction(
    session: AsyncSession,
    user_id: uuid.UUID,
    account_id: uuid.UUID,
    amount: float,
    txn_type: str,
    txn_date: date,
    source: str = "manual",
) -> Transaction:
    from datetime import datetime, timezone

    txn = Transaction(
        id=uuid.uuid4(),
        user_id=user_id,
        account_id=account_id,
        description=f"Test {txn_type} {amount}",
        amount=Decimal(str(amount)),
        date=txn_date,
        type=txn_type,
        source=source,
        currency="BRL",
        created_at=datetime.now(timezone.utc),
    )
    session.add(txn)
    await session.commit()
    await session.refresh(txn)
    return txn


@pytest.mark.asyncio
async def test_net_worth_report_structure(session: AsyncSession, test_user, test_workspace):
    """Net worth report returns correct ReportResponse structure."""
    account = await _create_manual_account(session, test_user.id, "NW Test")
    await _create_transaction(
        session, test_user.id, account.id, 5000, "credit", date.today(), source="opening_balance"
    )

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=6, interval="monthly")

    assert report.meta.type == "net_worth"
    assert report.meta.series_keys == ["accounts", "assets", "liabilities"]
    assert report.meta.currency == "BRL"
    assert report.meta.interval == "monthly"
    assert report.summary.primary_value is not None
    assert len(report.summary.breakdowns) == 3
    assert len(report.trend) > 0

    # Each trend point has the expected breakdown keys
    for dp in report.trend:
        assert "accounts" in dp.breakdowns
        assert "assets" in dp.breakdowns
        assert "liabilities" in dp.breakdowns


@pytest.mark.asyncio
async def test_net_worth_report_reflects_balance(session: AsyncSession, test_user, test_workspace):
    """Net worth report reflects actual account balance."""
    account = await _create_manual_account(session, test_user.id, "NW Balance Test")
    await _create_transaction(
        session, test_user.id, account.id, 10000, "credit", date.today(), source="opening_balance"
    )
    await _create_transaction(
        session, test_user.id, account.id, 3000, "debit", date.today()
    )

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=1, interval="monthly")

    # Current net worth should be 10000 - 3000 = 7000
    assert report.summary.primary_value == 7000.0
    assert report.summary.breakdowns[0].key == "accounts"
    assert report.summary.breakdowns[0].value == 7000.0


@pytest.mark.asyncio
async def test_net_worth_report_change_amount(session: AsyncSession, test_user, test_workspace):
    """Net worth report computes change between first and last trend points."""
    account = await _create_manual_account(session, test_user.id, "NW Change Test")

    # Add a transaction 3 months ago
    three_months_ago = date.today() - timedelta(days=90)
    await _create_transaction(
        session, test_user.id, account.id, 1000, "credit", three_months_ago, source="opening_balance"
    )
    # Add more income recently
    await _create_transaction(
        session, test_user.id, account.id, 2000, "credit", date.today()
    )

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=6, interval="monthly")

    # change_amount = last.value - first.value; should be positive
    assert report.summary.change_amount >= 0


@pytest.mark.asyncio
async def test_net_worth_report_excludes_closed_accounts(session: AsyncSession, test_user, test_workspace):
    """Closed accounts are excluded from net worth."""
    # Open account with 5000
    open_acct = await _create_manual_account(session, test_user.id, "NW Open")
    await _create_transaction(
        session, test_user.id, open_acct.id, 5000, "credit", date.today(), source="opening_balance"
    )

    # Closed account with 3000
    closed_acct = Account(
        id=uuid.uuid4(),
        user_id=test_user.id,
        name="NW Closed",
        type="checking",
        balance=Decimal("3000.00"),
        currency="BRL",
        is_closed=True,
    )
    session.add(closed_acct)
    await session.commit()
    await _create_transaction(
        session, test_user.id, closed_acct.id, 3000, "credit", date.today(), source="opening_balance"
    )

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=1, interval="monthly")

    # Should only include the open account
    assert report.summary.primary_value == 5000.0


@pytest.mark.asyncio
async def test_net_worth_report_intervals(session: AsyncSession, test_user, test_workspace):
    """Net worth report works with different interval options."""
    account = await _create_manual_account(session, test_user.id, "NW Interval Test")
    await _create_transaction(
        session, test_user.id, account.id, 1000, "credit", date.today(), source="opening_balance"
    )

    for interval in ["daily", "weekly", "monthly", "yearly"]:
        report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=6, interval=interval)
        assert report.meta.interval == interval
        assert len(report.trend) > 0


@pytest.mark.asyncio
async def test_net_worth_report_ytd_starts_at_current_year(
    session: AsyncSession, test_user, test_workspace
):
    """YTD report window starts at Jan 1 while preserving granularity."""
    report = await get_net_worth_report(
        session, test_workspace.id, test_user.id, months=24, interval="monthly", period="ytd"
    )

    assert report.trend[0].date == f"{date.today().year}-01"
    assert all(point.date.startswith(str(date.today().year)) for point in report.trend)


@pytest.mark.asyncio
@pytest.mark.parametrize("interval", ["daily", "weekly", "monthly", "yearly"])
async def test_net_worth_report_custom_range_pins_window(
    session: AsyncSession, test_user, test_workspace, interval
):
    """Explicit start_date/end_date override the preset window."""
    start = date(2022, 3, 1)
    end = date(2022, 6, 30)
    report = await get_net_worth_report(
        session, test_workspace.id, test_user.id,
        months=6, interval=interval,
        start_date=start, end_date=end,
    )

    expected = [_format_date_label(point, interval)
                for point in _date_points(start, end, interval)]
    assert [point.date for point in report.trend] == expected
    assert report.trend[-1].date == _format_date_label(end, interval)


# ---------------------------------------------------------------------------
# API-level tests: /reports/net-worth
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_net_worth_api_endpoint(client, auth_headers, test_transactions):
    """GET /reports/net-worth returns valid response."""
    response = await client.get(
        "/api/reports/net-worth",
        params={"months": 6, "interval": "monthly"},
        headers=auth_headers,
    )
    assert response.status_code == 200
    data = response.json()

    assert data["meta"]["type"] == "net_worth"
    assert "summary" in data
    assert "trend" in data
    assert isinstance(data["summary"]["primary_value"], (int, float))
    assert isinstance(data["trend"], list)


@pytest.mark.asyncio
async def test_net_worth_api_accepts_ytd_period(client, auth_headers):
    """GET /reports/net-worth accepts period=ytd."""
    response = await client.get(
        "/api/reports/net-worth",
        params={"period": "ytd", "interval": "monthly"},
        headers=auth_headers,
    )
    assert response.status_code == 200
    data = response.json()

    assert data["trend"][0]["date"] == f"{date.today().year}-01"


@pytest.mark.asyncio
async def test_net_worth_api_validation(client, auth_headers):
    """GET /reports/net-worth validates query params."""
    # Invalid interval
    resp = await client.get(
        "/api/reports/net-worth",
        params={"interval": "invalid"},
        headers=auth_headers,
    )
    assert resp.status_code == 422

    # Invalid period
    resp = await client.get(
        "/api/reports/net-worth",
        params={"period": "rolling"},
        headers=auth_headers,
    )
    assert resp.status_code == 422

    # Months out of range
    resp = await client.get(
        "/api/reports/net-worth",
        params={"months": 0},
        headers=auth_headers,
    )
    assert resp.status_code == 422


@pytest.mark.asyncio
async def test_net_worth_api_requires_auth(client):
    """GET /reports/net-worth requires authentication."""
    resp = await client.get("/api/reports/net-worth")
    assert resp.status_code == 401


# ---------------------------------------------------------------------------
# API-level tests: /reports/income-expenses
# ---------------------------------------------------------------------------


    # Just verify the endpoint works — transfer exclusion is enforced by the SQL filter


@pytest.mark.asyncio
async def test_income_expenses_api_validation(client, auth_headers):
    """GET /reports/income-expenses validates query params."""
    resp = await client.get(
        "/api/reports/income-expenses",
        params={"interval": "invalid"},
        headers=auth_headers,
    )
    assert resp.status_code == 422

    resp = await client.get(
        "/api/reports/income-expenses",
        params={"months": 25},
        headers=auth_headers,
    )
    assert resp.status_code == 422

    resp = await client.get(
        "/api/reports/income-expenses",
        params={"period": "rolling"},
        headers=auth_headers,
    )
    assert resp.status_code == 422


@pytest.mark.asyncio
async def test_income_expenses_api_accepts_ytd_period(client, auth_headers, monkeypatch):
    """GET /reports/income-expenses passes period=ytd to service."""

    async def fake_report(
        session, workspace_id, user_id, months, interval, currency,
        account_ids=None, period=None, days=None, financial_year_start_month=1,
        start_date=None, end_date=None,
    ):
        assert months == 12
        assert interval == "monthly"
        assert period == "ytd"
        assert days is None
        assert financial_year_start_month == 1
        return ReportResponse(
            summary=ReportSummary(
                primary_value=0,
                change_amount=0,
                change_percent=None,
                breakdowns=[],
            ),
            trend=[],
            meta=ReportMeta(
                type="income_expenses",
                series_keys=["income", "expenses", "projectedIncome", "projectedExpenses"],
                currency=currency,
                interval=interval,
            ),
            composition=[],
            category_trend=[],
        )

    monkeypatch.setattr(report_service, "get_income_expenses_report", fake_report)

    resp = await client.get(
        "/api/reports/income-expenses",
        params={"period": "ytd", "interval": "monthly"},
        headers=auth_headers,
    )
    assert resp.status_code == 200


@pytest.mark.asyncio
async def test_income_expenses_api_forwards_days_window(client, auth_headers, monkeypatch):
    """GET /reports/income-expenses passes the exact day window to the service."""
    seen: dict = {}

    async def fake_report(
        session, workspace_id, user_id, months, interval, currency,
        account_ids=None, period=None, days=None, financial_year_start_month=1,
        start_date=None, end_date=None,
    ):
        seen["days"] = days
        return ReportResponse(
            summary=ReportSummary(
                primary_value=0,
                change_amount=0,
                change_percent=None,
                breakdowns=[],
            ),
            trend=[],
            meta=ReportMeta(
                type="income_expenses",
                series_keys=["income", "expenses", "projectedIncome", "projectedExpenses"],
                currency=currency,
                interval=interval,
            ),
            composition=[],
            category_trend=[],
        )

    monkeypatch.setattr(report_service, "get_income_expenses_report", fake_report)

    resp = await client.get(
        "/api/reports/income-expenses",
        params={"months": 1, "interval": "monthly", "days": 30},
        headers=auth_headers,
    )
    assert resp.status_code == 200
    assert seen["days"] == 30

    resp = await client.get(
        "/api/reports/income-expenses",
        params={"days": 0},
        headers=auth_headers,
    )
    assert resp.status_code == 422


@pytest.mark.asyncio
async def test_income_expenses_api_forwards_custom_range(client, auth_headers, monkeypatch):
    """start_date/end_date override presets and reach the service."""
    seen: dict = {}

    async def fake_report(
        session, workspace_id, user_id, months, interval, currency,
        account_ids=None, period=None, days=None, financial_year_start_month=1,
        start_date=None, end_date=None,
    ):
        seen["start"] = start_date
        seen["end"] = end_date
        return ReportResponse(
            summary=ReportSummary(primary_value=0, change_amount=0, change_percent=None, breakdowns=[]),
            trend=[],
            meta=ReportMeta(type="income_expenses", series_keys=[], currency=currency, interval=interval),
            composition=[],
            category_trend=[],
        )

    monkeypatch.setattr(report_service, "get_income_expenses_report", fake_report)

    resp = await client.get(
        "/api/reports/income-expenses",
        params={"start_date": "2022-01-01", "end_date": "2022-06-30"},
        headers=auth_headers,
    )
    assert resp.status_code == 200
    assert seen["start"] == date(2022, 1, 1)
    assert seen["end"] == date(2022, 6, 30)


@pytest.mark.asyncio
async def test_income_expenses_api_rejects_partial_custom_range(client, auth_headers):
    """Both start_date and end_date must be provided together."""
    resp = await client.get(
        "/api/reports/income-expenses",
        params={"start_date": "2022-01-01"},
        headers=auth_headers,
    )
    assert resp.status_code == 422


@pytest.mark.asyncio
async def test_income_expenses_api_rejects_reversed_custom_range(client, auth_headers):
    """end_date must be on or after start_date."""
    resp = await client.get(
        "/api/reports/income-expenses",
        params={"start_date": "2022-06-30", "end_date": "2022-01-01"},
        headers=auth_headers,
    )
    assert resp.status_code == 422


def test_financial_year_start_month_uses_india_jurisdiction():
    assert _financial_year_start_month("IN") == 4
    assert _financial_year_start_month("in") == 4
    assert _financial_year_start_month("BR") == 1
    assert _financial_year_start_month(None) == 1


def test_indian_yearly_ytd_reports_are_rejected_until_fiscal_buckets_exist():
    with pytest.raises(HTTPException, match="Yearly YTD reports"):
        _reject_unsupported_fiscal_year_report("ytd", "yearly", 4)
    _reject_unsupported_fiscal_year_report("ytd", "yearly", 1)


@pytest.mark.asyncio
@pytest.mark.parametrize(("tax_jurisdiction", "expected_month"), [("IN", 4), ("BR", 1)])
async def test_report_api_forwards_workspace_financial_year(
    client, auth_headers, monkeypatch, tax_jurisdiction, expected_month
):
    context = SimpleNamespace(
        workspace=SimpleNamespace(id=uuid.uuid4(), tax_jurisdiction=tax_jurisdiction),
        user_id=uuid.uuid4(),
        user=SimpleNamespace(primary_currency="INR"),
    )

    async def override_workspace():
        return context

    empty_report = ReportResponse(
        summary=ReportSummary(
            primary_value=0, change_amount=0, change_percent=None, breakdowns=[]
        ),
        trend=[],
        meta=ReportMeta(
            type="income_expenses",
            series_keys=["income", "expenses"],
            currency="INR",
            interval="monthly",
        ),
        composition=[],
        category_trend=[],
    )
    net_worth = AsyncMock(return_value=empty_report)
    income_expenses = AsyncMock(return_value=empty_report)
    app.dependency_overrides[current_workspace] = override_workspace
    monkeypatch.setattr(report_service, "get_net_worth_report", net_worth)
    monkeypatch.setattr(report_service, "get_income_expenses_report", income_expenses)
    try:
        net_response = await client.get(
            "/api/reports/net-worth", params={"period": "ytd"}, headers=auth_headers
        )
        income_response = await client.get(
            "/api/reports/income-expenses", params={"period": "ytd"}, headers=auth_headers
        )
    finally:
        app.dependency_overrides.pop(current_workspace, None)

    assert net_response.status_code == 200
    assert income_response.status_code == 200
    assert net_worth.call_args.kwargs["financial_year_start_month"] == expected_month
    assert income_expenses.call_args.kwargs["financial_year_start_month"] == expected_month


@pytest.mark.asyncio
async def test_income_expenses_api_requires_auth(client):
    """GET /reports/income-expenses requires authentication."""
    resp = await client.get("/api/reports/income-expenses")
    assert resp.status_code == 401


# ---------------------------------------------------------------------------
# Schema tests: CategoryTrendItem & category_trend on ReportResponse
# ---------------------------------------------------------------------------


def test_category_trend_item_schema():
    """CategoryTrendItem can be constructed with valid data."""
    item = CategoryTrendItem(
        key="cat-1",
        label="Groceries",
        color="#10B981",
        total=1500.0,
        group="expenses",
        series=[
            ReportDataPoint(date="2025-01", value=500.0, breakdowns={}),
            ReportDataPoint(date="2025-02", value=450.0, breakdowns={}),
            ReportDataPoint(date="2025-03", value=550.0, breakdowns={}),
        ],
    )
    assert item.key == "cat-1"
    assert item.group == "expenses"
    assert len(item.series) == 3
    assert item.total == 1500.0


def test_report_response_includes_category_trend():
    """ReportResponse includes category_trend field, defaulting to empty."""
    from app.schemas.report import ReportMeta, ReportSummary, ReportBreakdown

    response = ReportResponse(
        summary=ReportSummary(
            primary_value=1000.0,
            change_amount=100.0,
            change_percent=10.0,
            breakdowns=[ReportBreakdown(key="a", label="A", value=1000.0, color="#000")],
        ),
        trend=[ReportDataPoint(date="2025-01", value=1000.0, breakdowns={})],
        meta=ReportMeta(type="test", series_keys=["a"], currency="BRL", interval="monthly"),
    )
    # Default is empty list
    assert response.category_trend == []

    # Can also be set explicitly
    item = CategoryTrendItem(
        key="cat-1", label="Food", color="#F00", total=500.0,
        group="expenses", series=[],
    )
    response2 = ReportResponse(
        summary=response.summary,
        trend=response.trend,
        meta=response.meta,
        category_trend=[item],
    )
    assert len(response2.category_trend) == 1
    assert response2.category_trend[0].label == "Food"


# ---------------------------------------------------------------------------
# Service-level test: net_worth returns empty category_trend
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_net_worth_report_has_empty_category_trend(session: AsyncSession, test_user, test_workspace):
    """Net worth report returns empty category_trend (only used by income_expenses)."""
    account = await _create_manual_account(session, test_user.id, "NW CatTrend Test")
    await _create_transaction(
        session, test_user.id, account.id, 1000, "credit", date.today(), source="opening_balance"
    )

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=3, interval="monthly")
    assert report.category_trend == []


# ---------------------------------------------------------------------------
# API-level test: income-expenses includes category_trend
# ---------------------------------------------------------------------------


# ---------------------------------------------------------------------------
# _asset_value_at
# ---------------------------------------------------------------------------


async def _make_manual_account(session, user_id, name, currency="BRL", acct_type="checking"):
    acct = Account(
        id=uuid.uuid4(), user_id=user_id, name=name,
        type=acct_type, balance=Decimal("0"), currency=currency,
    )
    session.add(acct)
    await session.commit()
    await session.refresh(acct)
    return acct


async def _add_txn(session, user_id, account_id, amount, txn_type, txn_date, source="manual"):
    txn = Transaction(
        id=uuid.uuid4(), user_id=user_id, account_id=account_id,
        description=f"Test {txn_type}", amount=Decimal(str(amount)),
        date=txn_date, type=txn_type, source=source, currency="BRL",
        created_at=datetime.now(timezone.utc),
    )
    session.add(txn)
    await session.commit()
    return txn


@pytest.mark.asyncio
async def test_asset_value_at_with_entries(session: AsyncSession, test_user, test_workspace: User):
    asset = Asset(
        id=uuid.uuid4(), user_id=test_user.id, name="House",
        type="real_estate", currency="BRL",
    )
    session.add(asset)
    await session.flush()

    v1 = AssetValue(
        id=uuid.uuid4(), asset_id=asset.id,
        amount=Decimal("100000"), date=date.today() - timedelta(days=30),
    )
    v2 = AssetValue(
        id=uuid.uuid4(), asset_id=asset.id,
        amount=Decimal("110000"), date=date.today(),
    )
    session.add_all([v1, v2])
    await session.commit()

    total = await _asset_value_at(session, test_workspace.id, date.today(), "BRL")
    assert total == 110000.0


@pytest.mark.asyncio
async def test_asset_value_at_fallback_purchase_price(session: AsyncSession, test_user, test_workspace: User):
    asset = Asset(
        id=uuid.uuid4(), user_id=test_user.id, name="Car",
        type="vehicle", currency="BRL",
        purchase_price=Decimal("50000"),
        purchase_date=date.today() - timedelta(days=60),
    )
    session.add(asset)
    await session.commit()

    total = await _asset_value_at(session, test_workspace.id, date.today(), "BRL")
    assert total == 50000.0


@pytest.mark.asyncio
async def test_asset_value_at_excludes_archived(session: AsyncSession, test_user, test_workspace: User):
    asset = Asset(
        id=uuid.uuid4(), user_id=test_user.id, name="Sold Car",
        type="vehicle", currency="BRL",
        purchase_price=Decimal("30000"), is_archived=True,
    )
    session.add(asset)
    await session.commit()

    total = await _asset_value_at(session, test_workspace.id, date.today(), "BRL")
    assert total == 0.0


@pytest.mark.asyncio
async def test_asset_value_at_excludes_sold(session: AsyncSession, test_user, test_workspace: User):
    asset = Asset(
        id=uuid.uuid4(), user_id=test_user.id, name="Sold Asset",
        type="vehicle", currency="BRL",
        purchase_price=Decimal("20000"),
        sell_date=date.today() - timedelta(days=10),
    )
    session.add(asset)
    await session.commit()

    total = await _asset_value_at(session, test_workspace.id, date.today(), "BRL")
    assert total == 0.0


@pytest.mark.asyncio
async def test_asset_value_at_purchase_date_after_cutoff(session: AsyncSession, test_user, test_workspace: User):
    asset = Asset(
        id=uuid.uuid4(), user_id=test_user.id, name="Future Asset",
        type="other", currency="BRL",
        purchase_price=Decimal("5000"),
        purchase_date=date.today() + timedelta(days=30),
    )
    session.add(asset)
    await session.commit()

    total = await _asset_value_at(session, test_workspace.id, date.today(), "BRL")
    assert total == 0.0


# ---------------------------------------------------------------------------
# _net_worth_at
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_net_worth_with_credit_card(session: AsyncSession, test_user, test_workspace: User):
    checking = await _make_manual_account(session, test_user.id, "NW Check")
    await _add_txn(session, test_user.id, checking.id, 5000, "credit", date.today())

    conn = BankConnection(
        id=uuid.uuid4(), user_id=test_user.id, provider="test",
        external_id="ext-cc-nw", institution_name="CC",
        credentials={}, status="active",
        last_sync_at=datetime.now(timezone.utc),
        created_at=datetime.now(timezone.utc),
    )
    session.add(conn)
    await session.flush()
    cc = Account(
        id=uuid.uuid4(), user_id=test_user.id, connection_id=conn.id,
        name="CC", type="credit_card", balance=Decimal("1000"), currency="BRL",
    )
    session.add(cc)
    await session.commit()

    dp = await _net_worth_at(session, test_workspace.id, date.today(), "BRL")
    assert dp.breakdowns["accounts"] == 5000.0
    assert dp.breakdowns["liabilities"] == 1000.0
    assert dp.value == 4000.0


async def _make_shared_credit_cards(session, user_id, workspace_id, balance):
    conn = BankConnection(
        id=uuid.uuid4(), user_id=user_id, provider="pluggy",
        external_id="ext-shared-line", institution_name="Shared line",
        credentials={}, status="active",
        last_sync_at=datetime.now(timezone.utc),
        created_at=datetime.now(timezone.utc),
    )
    session.add(conn)
    await session.flush()
    cards = []
    for name in ("Shared Visa", "Shared Mastercard"):
        card = Account(
            id=uuid.uuid4(), user_id=user_id, workspace_id=workspace_id, connection_id=conn.id,
            name=name, type="credit_card", balance=Decimal(balance), currency="BRL",
            shared_balance_group=f"{conn.id}:shared-credit",
        )
        session.add(card)
        cards.append(card)
    await session.commit()
    return cards


@pytest.mark.asyncio
async def test_net_worth_counts_shared_credit_line_once(
    session: AsyncSession, test_user, test_workspace: User,
):
    checking = await _make_manual_account(session, test_user.id, "NW Shared Check")
    await _add_txn(session, test_user.id, checking.id, 5000, "credit", date.today())
    await _make_shared_credit_cards(session, test_user.id, test_workspace.id, "1000")

    dp = await _net_worth_at(session, test_workspace.id, date.today(), "BRL")

    assert dp.breakdowns["liabilities"] == 1000.0
    assert dp.value == 4000.0
    liabilities = [c for c in dp.composition if c.group == "liabilities"]
    assert len(liabilities) == 1
    assert liabilities[0].value == 1000.0
    assert liabilities[0].label == "Shared Visa + Shared Mastercard"


@pytest.mark.asyncio
async def test_net_worth_backtracks_shared_credit_line_across_all_cards(
    session: AsyncSession, test_user, test_workspace: User,
):
    first, second = await _make_shared_credit_cards(
        session, test_user.id, test_workspace.id, "300"
    )
    # The line's debt today (300) includes 50 + 25 charged today on the two cards.
    await _add_txn(session, test_user.id, first.id, 50, "debit", date.today())
    await _add_txn(session, test_user.id, second.id, 25, "debit", date.today())

    dp = await _net_worth_at(session, test_workspace.id, date.today() - timedelta(days=1), "BRL")

    assert dp.breakdowns["liabilities"] == 225.0


@pytest.mark.asyncio
async def test_net_worth_does_not_merge_a_retyped_shared_card(
    session: AsyncSession, test_user, test_workspace: User,
):
    first, second = await _make_shared_credit_cards(
        session, test_user.id, test_workspace.id, "500"
    )
    second.type = "checking"
    await session.commit()

    dp = await _net_worth_at(session, test_workspace.id, date.today(), "BRL")

    # The card owes 500; the retyped account reads as +500 checking.
    assert dp.breakdowns["liabilities"] == 500.0
    assert dp.breakdowns["accounts"] == 500.0


@pytest.mark.asyncio
async def test_net_worth_with_assets(session: AsyncSession, test_user, test_workspace: User):
    checking = await _make_manual_account(session, test_user.id, "NW Assets Check")
    await _add_txn(session, test_user.id, checking.id, 3000, "credit", date.today())

    asset = Asset(
        id=uuid.uuid4(), user_id=test_user.id, name="Apartment",
        type="real_estate", currency="BRL",
        purchase_price=Decimal("200000"),
        purchase_date=date.today() - timedelta(days=30),
    )
    session.add(asset)
    await session.commit()

    dp = await _net_worth_at(session, test_workspace.id, date.today(), "BRL")
    assert dp.breakdowns["accounts"] == 3000.0
    assert dp.breakdowns["assets"] == 200000.0
    assert dp.value == 203000.0


@pytest.mark.asyncio
async def test_net_worth_negative_manual_balance(session: AsyncSession, test_user, test_workspace: User):
    acct = await _make_manual_account(session, test_user.id, "NW Negative")
    await _add_txn(session, test_user.id, acct.id, 1000, "debit", date.today())

    dp = await _net_worth_at(session, test_workspace.id, date.today(), "BRL")
    assert dp.breakdowns["accounts"] == 0.0
    assert dp.breakdowns["liabilities"] == 1000.0
    assert dp.value == -1000.0


@pytest.mark.asyncio
async def test_net_worth_negative_account_in_composition(session: AsyncSession, test_user, test_workspace: User):
    acct = await _make_manual_account(session, test_user.id, "Overdrawn Acct")
    await _add_txn(session, test_user.id, acct.id, 500, "debit", date.today())

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=1, interval="monthly")
    liability_items = [c for c in report.composition if c.group == "liabilities"]
    labels = [c.label for c in liability_items]
    assert "Overdrawn Acct" in labels


# ---------------------------------------------------------------------------
# get_net_worth_report — composition and intervals
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_net_worth_composition_includes_accounts(session: AsyncSession, test_user, test_workspace: User):
    acct = await _make_manual_account(session, test_user.id, "Comp Acct")
    await _add_txn(session, test_user.id, acct.id, 10000, "credit", date.today())

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=1, interval="monthly")
    comp_labels = [c.label for c in report.composition]
    assert "Comp Acct" in comp_labels


@pytest.mark.asyncio
async def test_net_worth_composition_includes_assets(session: AsyncSession, test_user, test_workspace: User):
    asset = Asset(
        id=uuid.uuid4(), user_id=test_user.id, name="Comp Asset",
        type="investment", currency="BRL",
        purchase_price=Decimal("5000"),
        purchase_date=date.today() - timedelta(days=5),
    )
    session.add(asset)
    await session.commit()

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=1, interval="monthly")
    comp_labels = [c.label for c in report.composition]
    assert "Comp Asset" in comp_labels


@pytest.mark.asyncio
async def test_net_worth_composition_uses_display_name(session: AsyncSession, test_user, test_workspace: User):
    """Composition labels must use display_name when set, falling back to name."""
    acct = await _make_manual_account(session, test_user.id, "Provider Name")
    acct.display_name = "My Nickname"
    await session.commit()
    await _add_txn(session, test_user.id, acct.id, 10000, "credit", date.today())

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=1, interval="monthly")
    comp_labels = [c.label for c in report.composition]
    assert "My Nickname" in comp_labels
    assert "Provider Name" not in comp_labels


@pytest.mark.asyncio
async def test_net_worth_intermediate_trend_points_have_composition(
    session: AsyncSession, test_user, test_workspace: User
):
    """Non-last trend points carry per-item composition, not just the current snapshot."""
    acct = await _make_manual_account(session, test_user.id, "History Acct")
    # 135 days back lands before the first monthly cutoff (end of month-4),
    # so the first trend point carries this account in composition.
    await _add_txn(session, test_user.id, acct.id, 8000, "credit", date.today() - timedelta(days=135))

    report = await get_net_worth_report(
        session, test_workspace.id, test_user.id, months=4, interval="monthly"
    )
    assert len(report.trend) >= 2
    # The first trend point predates today and must still have composition populated
    first = report.trend[0]
    assert len(first.composition) > 0
    assert any(c.label == "History Acct" for c in first.composition)


@pytest.mark.asyncio
async def test_net_worth_weekly_interval(session: AsyncSession, test_user, test_workspace: User):
    acct = await _make_manual_account(session, test_user.id, "Weekly Test")
    await _add_txn(session, test_user.id, acct.id, 1000, "credit", date.today())

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=2, interval="weekly")
    assert report.meta.interval == "weekly"
    assert len(report.trend) > 1


@pytest.mark.asyncio
async def test_net_worth_daily_interval(session: AsyncSession, test_user, test_workspace: User):
    acct = await _make_manual_account(session, test_user.id, "Daily Test")
    await _add_txn(session, test_user.id, acct.id, 500, "credit", date.today())

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=1, interval="daily")
    assert report.meta.interval == "daily"
    assert len(report.trend) > 10


@pytest.mark.asyncio
async def test_net_worth_change_percent_zero_previous(session: AsyncSession, test_user, test_workspace: User):
    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=1, interval="monthly")
    if report.summary.primary_value == 0:
        assert report.summary.change_percent is None


# ---------------------------------------------------------------------------
# Trend data point change field
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_trend_change_calculation(session: AsyncSession, test_user, test_workspace: User):
    """change is None on the first point and equals round(value - prev.value, 2) on all others.
    Uses activity spread over time so the trend contains positive, negative, and flat changes."""
    acct = await _make_manual_account(session, test_user.id, "Change Calc")
    today = date.today()
    await _add_txn(session, test_user.id, acct.id, 5000, "credit", today - timedelta(days=150))  # opening
    await _add_txn(session, test_user.id, acct.id, 1200, "credit", today - timedelta(days=90))   # income
    await _add_txn(session, test_user.id, acct.id, 300,  "debit",  today - timedelta(days=60))   # expense
    await _add_txn(session, test_user.id, acct.id, 800,  "credit", today - timedelta(days=30))   # income
    await _add_txn(session, test_user.id, acct.id, 450,  "debit",  today)                        # recent expense

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=6, interval="monthly")

    assert len(report.trend) > 1
    assert report.trend[0].change is None
    for prev, curr in zip(report.trend, report.trend[1:]):
        assert curr.change == round(curr.value - prev.value, 2)


@pytest.mark.asyncio
async def test_trend_change_zero_when_net_worth_unchanged(
    session: AsyncSession, test_user, test_workspace: User
):
    """When no activity occurs inside the report window, every non-first point has change == 0."""
    acct = await _make_manual_account(session, test_user.id, "Change Zero")
    two_years_ago = date.today() - timedelta(days=730)
    await _add_txn(session, test_user.id, acct.id, 4000, "credit", two_years_ago)

    report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=3, interval="monthly")

    for dp in report.trend[1:]:
        assert dp.change == 0.0


@pytest.mark.asyncio
async def test_net_worth_api_trend_points_include_change(client, auth_headers, test_transactions):
    """GET /reports/net-worth: change is None on the first point and present on all others."""
    response = await client.get(
        "/api/reports/net-worth",
        params={"months": 6, "interval": "monthly"},
        headers=auth_headers,
    )
    assert response.status_code == 200
    data = response.json()

    trend = data["trend"]
    assert len(trend) > 1
    assert trend[0]["change"] is None
    for point in trend[1:]:
        assert point["change"] is not None


# ---------------------------------------------------------------------------
# Pure-function tests: _add_months
# ---------------------------------------------------------------------------


def test_add_months_simple():
    assert _add_months(date(2025, 1, 15), 1) == date(2025, 2, 15)


def test_add_months_year_rollover():
    assert _add_months(date(2025, 11, 10), 3) == date(2026, 2, 10)


def test_add_months_clamps_to_last_day():
    # Jan 31 + 1 month → Feb 28 (or 29 in leap year)
    assert _add_months(date(2025, 1, 31), 1) == date(2025, 2, 28)
    assert _add_months(date(2024, 1, 31), 1) == date(2024, 2, 29)


def test_add_months_zero_returns_same():
    assert _add_months(date(2025, 6, 15), 0) == date(2025, 6, 15)


def test_add_months_twelve_keeps_same_day():
    assert _add_months(date(2025, 6, 15), 12) == date(2026, 6, 15)


# ---------------------------------------------------------------------------
# Cash flow report — service-level tests
# ---------------------------------------------------------------------------


async def _make_recurring(
    session: AsyncSession,
    user_id: uuid.UUID,
    account_id: uuid.UUID,
    amount: float,
    txn_type: str,
    frequency: str = "monthly",
    *,
    description: str | None = None,
    day_of_month: int | None = None,
    start_date: date | None = None,
    end_date: date | None = None,
    next_occurrence: date | None = None,
    is_active: bool = True,
    currency: str = "BRL",
    category_id: uuid.UUID | None = None,
) -> RecurringTransaction:
    today = date.today()
    start = start_date or today
    nxt = next_occurrence or (today + timedelta(days=1))
    rec = RecurringTransaction(
        id=uuid.uuid4(),
        user_id=user_id,
        account_id=account_id,
        category_id=category_id,
        description=description or f"Test {txn_type} {amount}",
        amount=Decimal(str(amount)),
        currency=currency,
        type=txn_type,
        frequency=frequency,
        day_of_month=day_of_month,
        start_date=start,
        end_date=end_date,
        is_active=is_active,
        next_occurrence=nxt,
    )
    session.add(rec)
    await session.commit()
    await session.refresh(rec)
    return rec


@pytest.mark.asyncio
async def test_cash_flow_report_structure(session: AsyncSession, test_user, test_workspace: User):
    """Cash flow report returns a well-formed ReportResponse."""
    account = await _make_manual_account(session, test_user.id, "CF Structure")
    await _add_txn(
        session, test_user.id, account.id, 5000, "credit",
        date.today(), source="opening_balance",
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=6, interval="daily")

    assert report.meta.type == "cash_flow"
    assert report.meta.series_keys == ["balance"]
    assert report.meta.currency == "BRL"
    assert report.meta.interval == "daily"
    assert len(report.trend) > 0
    # Summary breakdowns are exactly the four cash-flow keys, in order.
    keys = [b.key for b in report.summary.breakdowns]
    assert keys == ["startingBalance", "projectedIncome", "projectedExpenses", "endingBalance"]


@pytest.mark.asyncio
async def test_cash_flow_starting_balance_matches_today(
    session: AsyncSession, test_user, test_workspace: User
):
    """startingBalance breakdown == sum of account balances at today."""
    account = await _make_manual_account(session, test_user.id, "CF Start")
    await _add_txn(
        session, test_user.id, account.id, 1234.56, "credit",
        date.today(), source="opening_balance",
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")
    starting = next(b for b in report.summary.breakdowns if b.key == "startingBalance")
    assert starting.value == 1234.56


@pytest.mark.asyncio
async def test_cash_flow_no_data_is_flat_at_zero(
    session: AsyncSession, test_user, test_workspace: User
):
    """User with no accounts → flat line at 0."""
    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")
    assert report.summary.primary_value == 0.0
    assert report.summary.change_amount == 0.0
    assert report.summary.change_percent is None  # zero starting → percent undefined
    # Every trend point should be 0
    assert all(p.value == 0.0 for p in report.trend)
    assert report.composition == []


@pytest.mark.asyncio
async def test_cash_flow_no_data_with_balance_is_flat(
    session: AsyncSession, test_user, test_workspace: User
):
    """Account with balance but no recurring → flat line at starting balance."""
    account = await _make_manual_account(session, test_user.id, "CF Flat")
    await _add_txn(
        session, test_user.id, account.id, 2500, "credit",
        date.today(), source="opening_balance",
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")

    assert report.summary.primary_value == 2500.0
    assert report.summary.change_amount == 0.0
    # All trend points equal to starting balance
    assert all(p.value == 2500.0 for p in report.trend)


@pytest.mark.asyncio
async def test_cash_flow_recurring_credit_increases_balance(
    session: AsyncSession, test_user, test_workspace: User
):
    """Monthly salary credit should drive ending balance above starting."""
    account = await _make_manual_account(session, test_user.id, "CF Salary")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )

    today = date.today()
    # Anchored to the 1st rather than today's day-of-month: building next
    # month's date from `today.day` raises on the 31st whenever the month
    # that follows has 30 days, which made this test fail seven times a
    # year for reasons that had nothing to do with cash flow.
    next_month = date(today.year + (today.month // 12), (today.month % 12) + 1, 1)
    salary_day = next_month
    await _make_recurring(
        session, test_user.id, account.id,
        amount=3000, txn_type="credit", frequency="monthly",
        day_of_month=1, next_occurrence=salary_day,
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")

    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    ending = next(b for b in report.summary.breakdowns if b.key == "endingBalance")

    # 3 monthly occurrences over a 3-month window
    assert proj_income.value >= 3000.0  # at least one occurrence
    assert proj_exp.value == 0.0
    assert ending.value > 1000.0
    assert report.summary.change_amount > 0


@pytest.mark.asyncio
async def test_cash_flow_recurring_debit_decreases_balance(
    session: AsyncSession, test_user, test_workspace: User
):
    """Monthly rent debit should drive ending balance below starting."""
    account = await _make_manual_account(session, test_user.id, "CF Rent")
    await _add_txn(
        session, test_user.id, account.id, 10000, "credit",
        date.today(), source="opening_balance",
    )

    today = date.today()
    nxt = today + timedelta(days=2)
    await _make_recurring(
        session, test_user.id, account.id,
        amount=1200, txn_type="debit", frequency="monthly",
        day_of_month=nxt.day, next_occurrence=nxt,
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")

    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    ending = next(b for b in report.summary.breakdowns if b.key == "endingBalance")

    assert proj_exp.value > 0
    assert ending.value < 10000.0
    assert report.summary.change_amount < 0


@pytest.mark.asyncio
async def test_cash_flow_inactive_recurring_excluded(
    session: AsyncSession, test_user, test_workspace: User
):
    """is_active=False recurring should not contribute to projection."""
    account = await _make_manual_account(session, test_user.id, "CF Inactive")
    await _add_txn(
        session, test_user.id, account.id, 5000, "credit",
        date.today(), source="opening_balance",
    )

    today = date.today()
    await _make_recurring(
        session, test_user.id, account.id,
        amount=999, txn_type="debit", frequency="monthly",
        next_occurrence=today + timedelta(days=2), is_active=False,
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    assert proj_exp.value == 0.0


@pytest.mark.asyncio
async def test_cash_flow_recurring_end_date_stops_contribution(
    session: AsyncSession, test_user, test_workspace: User
):
    """Recurring with end_date in window should stop contributing past that date."""
    account = await _make_manual_account(session, test_user.id, "CF EndDate")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )

    today = date.today()
    # Recurring monthly +500 USD, ends in ~45 days (only 1-2 occurrences in 6mo window)
    end = today + timedelta(days=45)
    nxt = today + timedelta(days=2)
    await _make_recurring(
        session, test_user.id, account.id,
        amount=500, txn_type="credit", frequency="monthly",
        day_of_month=nxt.day, next_occurrence=nxt, end_date=end,
    )

    short_report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")
    long_report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=6, interval="daily")

    short_inc = next(b for b in short_report.summary.breakdowns if b.key == "projectedIncome")
    long_inc = next(b for b in long_report.summary.breakdowns if b.key == "projectedIncome")
    # Total projected income should not grow much when extending past end_date
    assert long_inc.value == short_inc.value


@pytest.mark.asyncio
async def test_cash_flow_future_dated_booked_transaction_included(
    session: AsyncSession, test_user, test_workspace: User
):
    """A Transaction with date > today is folded into the projection."""
    account = await _make_manual_account(session, test_user.id, "CF Future Tx")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    # Post-dated bonus 10 days from now
    await _add_txn(
        session, test_user.id, account.id, 750, "credit",
        date.today() + timedelta(days=10),
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    ending = next(b for b in report.summary.breakdowns if b.key == "endingBalance")

    assert proj_income.value >= 750.0
    assert ending.value >= 1750.0


@pytest.mark.asyncio
async def test_cash_flow_opening_balance_excluded_from_inflow(
    session: AsyncSession, test_user, test_workspace: User
):
    """opening_balance txns sit in starting balance and never count as inflow."""
    account = await _make_manual_account(session, test_user.id, "CF OB")
    # Future-dated opening-balance shouldn't show up as inflow
    await _add_txn(
        session, test_user.id, account.id, 9999, "credit",
        date.today() + timedelta(days=5), source="opening_balance",
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    assert proj_income.value == 0.0


@pytest.mark.asyncio
async def test_cash_flow_closed_account_excluded(
    session: AsyncSession, test_user, test_workspace: User
):
    """Future-dated transactions on closed accounts are excluded."""
    open_acct = await _make_manual_account(session, test_user.id, "CF Open")
    await _add_txn(
        session, test_user.id, open_acct.id, 1000, "credit",
        date.today(), source="opening_balance",
    )

    closed = Account(
        id=uuid.uuid4(), user_id=test_user.id, name="CF Closed",
        type="checking", balance=Decimal("0"), currency="BRL", is_closed=True,
    )
    session.add(closed)
    await session.commit()

    await _add_txn(
        session, test_user.id, closed.id, 5000, "credit",
        date.today() + timedelta(days=3),
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    assert proj_income.value == 0.0


@pytest.mark.asyncio
async def test_cash_flow_intervals(session: AsyncSession, test_user, test_workspace: User):
    """All supported intervals produce valid trends."""
    account = await _make_manual_account(session, test_user.id, "CF Intervals")
    await _add_txn(
        session, test_user.id, account.id, 2000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    await _make_recurring(
        session, test_user.id, account.id,
        amount=300, txn_type="debit", frequency="weekly",
        next_occurrence=today + timedelta(days=2),
    )

    intervals_seen = {}
    for interval in ["daily", "weekly", "monthly"]:
        rep = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval=interval)
        assert rep.meta.interval == interval
        assert len(rep.trend) > 0
        intervals_seen[interval] = rep.summary.breakdowns

    # The total projected expenses should be ~equal across intervals
    daily_exp = next(b.value for b in intervals_seen["daily"] if b.key == "projectedExpenses")
    weekly_exp = next(b.value for b in intervals_seen["weekly"] if b.key == "projectedExpenses")
    monthly_exp = next(b.value for b in intervals_seen["monthly"] if b.key == "projectedExpenses")
    # Allow tiny rounding variance
    assert abs(daily_exp - weekly_exp) < 1.0
    assert abs(daily_exp - monthly_exp) < 1.0


@pytest.mark.asyncio
async def test_cash_flow_months_range(session: AsyncSession, test_user, test_workspace: User):
    """Different `months` values produce trends ending at different dates."""
    account = await _make_manual_account(session, test_user.id, "CF Months")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )

    rep_1 = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=1, interval="daily")
    rep_6 = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=6, interval="daily")
    rep_12 = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=12, interval="daily")

    # Daily intervals → 1mo ≈ 30 points, 6mo ≈ 184, 12mo ≈ 366
    assert len(rep_1.trend) < len(rep_6.trend) < len(rep_12.trend)


@pytest.mark.asyncio
async def test_cash_flow_balance_is_running_cumulative(
    session: AsyncSession, test_user, test_workspace: User
):
    """Each subsequent trend point's value reflects accumulated flows."""
    account = await _make_manual_account(session, test_user.id, "CF Cumul")
    await _add_txn(
        session, test_user.id, account.id, 0, "credit",
        date.today(), source="opening_balance",
    )

    today = date.today()
    # +100 weekly so balance keeps rising
    await _make_recurring(
        session, test_user.id, account.id,
        amount=100, txn_type="credit", frequency="weekly",
        next_occurrence=today + timedelta(days=2),
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")
    # The trend should be monotonically non-decreasing (no debits, only credits)
    values = [p.value for p in report.trend]
    for prev, curr in zip(values, values[1:]):
        assert curr >= prev - 0.01  # tiny tolerance for floats


@pytest.mark.asyncio
async def test_cash_flow_composition_groups(session: AsyncSession, test_user, test_workspace: User):
    """Composition splits recurring projections into income/expenses groups."""
    account = await _make_manual_account(session, test_user.id, "CF Comp")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    await _make_recurring(
        session, test_user.id, account.id,
        amount=500, txn_type="credit", frequency="monthly",
        next_occurrence=today + timedelta(days=2),
        description="Income recurring",
    )
    await _make_recurring(
        session, test_user.id, account.id,
        amount=200, txn_type="debit", frequency="monthly",
        next_occurrence=today + timedelta(days=3),
        description="Expense recurring",
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")

    groups = {c.group for c in report.composition}
    assert "income" in groups
    assert "expenses" in groups
    # Each group has positive values
    for c in report.composition:
        assert c.value > 0


@pytest.mark.asyncio
async def test_cash_flow_change_percent_zero_starting(
    session: AsyncSession, test_user, test_workspace: User
):
    """When starting balance is 0, change_percent is None."""
    account = await _make_manual_account(session, test_user.id, "CF Zero")
    today = date.today()
    await _make_recurring(
        session, test_user.id, account.id,
        amount=100, txn_type="credit", frequency="monthly",
        next_occurrence=today + timedelta(days=2),
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")
    assert report.summary.change_percent is None


@pytest.mark.asyncio
async def test_cash_flow_weekly_recurring(session: AsyncSession, test_user, test_workspace: User):
    """Weekly frequency expands into multiple occurrences in window."""
    account = await _make_manual_account(session, test_user.id, "CF Weekly")
    await _add_txn(
        session, test_user.id, account.id, 0, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    await _make_recurring(
        session, test_user.id, account.id,
        amount=80, txn_type="debit", frequency="weekly",
        next_occurrence=today + timedelta(days=1),
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=1, interval="daily")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    # ~4-5 weekly occurrences in 1 month
    assert 240.0 <= proj_exp.value <= 480.0


@pytest.mark.asyncio
async def test_cash_flow_only_recurrings_after_today(
    session: AsyncSession, test_user, test_workspace: User
):
    """Recurring with next_occurrence == today is NOT counted (today is starting)."""
    account = await _make_manual_account(session, test_user.id, "CF Today")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )

    today = date.today()
    # next_occurrence is today — same day as starting balance, should be excluded
    await _make_recurring(
        session, test_user.id, account.id,
        amount=999, txn_type="credit", frequency="monthly",
        next_occurrence=today,
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    # Over 2 months: 3 raw monthly occurrences (today, +1mo, +2mo) but the
    # one ON today is excluded — so we expect exactly 2 × 999 = 1998.
    assert proj_income.value == 1998.0


# ---------------------------------------------------------------------------
# Cash flow API-level tests
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_cash_flow_api_endpoint(client, auth_headers, test_transactions):
    """GET /reports/cash-flow returns valid response."""
    response = await client.get(
        "/api/reports/cash-flow",
        params={"months": 6, "interval": "daily"},
        headers=auth_headers,
    )
    assert response.status_code == 200, response.text
    data = response.json()

    assert data["meta"]["type"] == "cash_flow"
    assert data["meta"]["series_keys"] == ["balance"]
    assert "summary" in data
    assert "trend" in data
    breakdown_keys = [b["key"] for b in data["summary"]["breakdowns"]]
    assert breakdown_keys == [
        "startingBalance", "projectedIncome",
        "projectedExpenses", "endingBalance",
    ]


@pytest.mark.asyncio
async def test_cash_flow_api_validation(client, auth_headers):
    """GET /reports/cash-flow rejects out-of-range params."""
    # months > 12
    resp = await client.get(
        "/api/reports/cash-flow",
        params={"months": 13},
        headers=auth_headers,
    )
    assert resp.status_code == 422

    # months < 1
    resp = await client.get(
        "/api/reports/cash-flow",
        params={"months": 0},
        headers=auth_headers,
    )
    assert resp.status_code == 422

    # yearly is not a supported interval for cash flow
    resp = await client.get(
        "/api/reports/cash-flow",
        params={"interval": "yearly"},
        headers=auth_headers,
    )
    assert resp.status_code == 422

    # arbitrary string interval
    resp = await client.get(
        "/api/reports/cash-flow",
        params={"interval": "garbage"},
        headers=auth_headers,
    )
    assert resp.status_code == 422


@pytest.mark.asyncio
async def test_cash_flow_api_requires_auth(client):
    """GET /reports/cash-flow requires authentication."""
    resp = await client.get("/api/reports/cash-flow")
    assert resp.status_code == 401


@pytest.mark.asyncio
async def test_cash_flow_api_default_params(client, auth_headers, test_transactions):
    """GET /reports/cash-flow with no params uses defaults (months=6, interval=daily)."""
    resp = await client.get("/api/reports/cash-flow", headers=auth_headers)
    assert resp.status_code == 200
    data = resp.json()
    assert data["meta"]["interval"] == "daily"
    # 1 month past + 6 months forward * ~30 days = ~210 trend points
    assert 180 <= len(data["trend"]) <= 230


@pytest.mark.asyncio
async def test_cash_flow_api_trend_point_shape(client, auth_headers, test_transactions):
    """Every trend point exposes inflow/outflow breakdowns."""
    resp = await client.get(
        "/api/reports/cash-flow",
        params={"months": 3, "interval": "daily"},
        headers=auth_headers,
    )
    assert resp.status_code == 200
    data = resp.json()
    for p in data["trend"]:
        assert "inflow" in p["breakdowns"]
        assert "outflow" in p["breakdowns"]
        assert "value" in p
        assert "date" in p


# ---------------------------------------------------------------------------
# Cash flow — credit card accounting mode (cash vs accrual)
# ---------------------------------------------------------------------------


async def _make_cc_account(
    session: AsyncSession, user_id: uuid.UUID, name: str, currency: str = "BRL",
) -> Account:
    acct = Account(
        id=uuid.uuid4(), user_id=user_id, name=name,
        type="credit_card", balance=Decimal("0"), currency=currency,
    )
    session.add(acct)
    await session.commit()
    await session.refresh(acct)
    return acct


async def _set_accounting_mode(session: AsyncSession, mode: str) -> None:
    """Set the global credit_card_accounting_mode app setting."""
    from app.models.app_settings import AppSetting
    existing = await session.get(AppSetting, "credit_card_accounting_mode")
    if existing:
        existing.value = mode
    else:
        session.add(AppSetting(key="credit_card_accounting_mode", value=mode))
    await session.commit()


@pytest.mark.asyncio
async def test_cash_flow_cash_mode_uses_transaction_date(
    session: AsyncSession, test_user, test_workspace: User
):
    """In cash mode: a CC purchase made today (effective_date in future) is
    already counted in starting balance via the CC liability — and is NOT
    re-projected on its effective_date."""
    await _set_accounting_mode(session, "cash")

    bank = await _make_manual_account(session, test_user.id, "CF CashMode Bank")
    cc = await _make_cc_account(session, test_user.id, "CF CashMode CC")
    # $1000 in bank
    await _add_txn(
        session, test_user.id, bank.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    # CC purchase today, bill due in 20 days
    today = date.today()
    cc_purchase = Transaction(
        id=uuid.uuid4(), user_id=test_user.id, account_id=cc.id,
        description="CC Purchase", amount=Decimal("100"),
        date=today, effective_date=today + timedelta(days=20),
        type="debit", source="manual", currency="BRL",
        created_at=datetime.now(timezone.utc),
    )
    session.add(cc_purchase)
    await session.commit()

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")

    starting = next(b for b in report.summary.breakdowns if b.key == "startingBalance")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    ending = next(b for b in report.summary.breakdowns if b.key == "endingBalance")

    # Cash mode: total balance = bank ($1000) - CC debt ($100) = $900
    assert starting.value == 900.0
    # No future flows queued — Transaction.date == today (not > today)
    assert proj_exp.value == 0.0
    assert ending.value == 900.0


@pytest.mark.asyncio
async def test_cash_flow_accrual_mode_projects_cc_on_effective_date(
    session: AsyncSession, test_user, test_workspace: User
):
    """In accrual mode: a CC purchase with effective_date > today is added
    back to starting balance (so it represents 'cash on hand right now') and
    re-projected as an outflow on the bill due date."""
    await _set_accounting_mode(session, "accrual")

    bank = await _make_manual_account(session, test_user.id, "CF AccrualMode Bank")
    cc = await _make_cc_account(session, test_user.id, "CF AccrualMode CC")
    await _add_txn(
        session, test_user.id, bank.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    cc_purchase = Transaction(
        id=uuid.uuid4(), user_id=test_user.id, account_id=cc.id,
        description="CC Purchase", amount=Decimal("100"),
        date=today, effective_date=today + timedelta(days=20),
        type="debit", source="manual", currency="BRL",
        created_at=datetime.now(timezone.utc),
    )
    session.add(cc_purchase)
    await session.commit()

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")

    starting = next(b for b in report.summary.breakdowns if b.key == "startingBalance")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    ending = next(b for b in report.summary.breakdowns if b.key == "endingBalance")

    # Accrual: pending CC ($100 debt) is added back → $900 + $100 = $1000.
    assert starting.value == 1000.0
    # Future outflow on day +20: $100.
    assert proj_exp.value == 100.0
    # Ending: $1000 - $100 = $900 (same wealth as cash mode, just timed differently).
    assert ending.value == 900.0

    # The dip happens on the bill due date.
    bill_label = (today + timedelta(days=20)).isoformat()
    bill_point = next(p for p in report.trend if p.date == bill_label)
    assert bill_point.breakdowns["outflow"] == 100.0
    assert bill_point.value == 900.0
    # The day before the bill, balance is still 1000.
    pre_bill_label = (today + timedelta(days=19)).isoformat()
    pre_bill_point = next(p for p in report.trend if p.date == pre_bill_label)
    assert pre_bill_point.value == 1000.0


@pytest.mark.asyncio
async def test_cash_flow_accrual_mode_no_double_count(
    session: AsyncSession, test_user, test_workspace: User
):
    """Accrual mode: the same CC purchase must not appear in both starting
    balance AND future flows — sum of (starting + inflow - outflow) over the
    window must equal final 'true' total balance (= what cash mode shows)."""
    await _set_accounting_mode(session, "accrual")

    bank = await _make_manual_account(session, test_user.id, "CF NoDouble Bank")
    cc = await _make_cc_account(session, test_user.id, "CF NoDouble CC")
    await _add_txn(
        session, test_user.id, bank.id, 5000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    # Two pending CC purchases with effective dates inside the window
    for offset, amt in [(15, 80), (40, 120)]:
        session.add(Transaction(
            id=uuid.uuid4(), user_id=test_user.id, account_id=cc.id,
            description=f"Purchase {amt}", amount=Decimal(str(amt)),
            date=today, effective_date=today + timedelta(days=offset),
            type="debit", source="manual", currency="BRL",
            created_at=datetime.now(timezone.utc),
        ))
    await session.commit()

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")

    starting = next(b for b in report.summary.breakdowns if b.key == "startingBalance")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    ending = next(b for b in report.summary.breakdowns if b.key == "endingBalance")

    # starting = 5000 (bank) - 200 (CC debt) + 200 (added back pending) = 5000
    assert starting.value == 5000.0
    # both purchases project as outflows
    assert proj_exp.value == 200.0
    # ending matches cash mode's "what you'd have if you paid all CC bills"
    assert ending.value == 4800.0


@pytest.mark.asyncio
async def test_cash_flow_accrual_mode_cc_purchase_outside_window_ignored(
    session: AsyncSession, test_user, test_workspace: User
):
    """A CC purchase whose effective_date falls past the projection window is
    not added back nor projected — its impact remains in starting balance."""
    await _set_accounting_mode(session, "accrual")

    bank = await _make_manual_account(session, test_user.id, "CF Outside Bank")
    cc = await _make_cc_account(session, test_user.id, "CF Outside CC")
    await _add_txn(
        session, test_user.id, bank.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    # Bill due 6 months out; we project only 2 months
    session.add(Transaction(
        id=uuid.uuid4(), user_id=test_user.id, account_id=cc.id,
        description="CC Purchase Far", amount=Decimal("75"),
        date=today, effective_date=today + timedelta(days=180),
        type="debit", source="manual", currency="BRL",
        created_at=datetime.now(timezone.utc),
    ))
    await session.commit()

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")

    starting = next(b for b in report.summary.breakdowns if b.key == "startingBalance")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")

    # CC debt is reflected in starting balance ($1000 - $75 = $925), no add-back
    # because the bill is outside the projection window.
    assert starting.value == 925.0
    assert proj_exp.value == 0.0


# ---------------------------------------------------------------------------
# Cash flow — multi-currency
# ---------------------------------------------------------------------------


async def _seed_fx_rate(
    session: AsyncSession, base: str, quote: str, rate: float, day: date | None = None,
) -> None:
    from app.models.fx_rate import FxRate
    session.add(FxRate(
        base_currency=base, quote_currency=quote,
        rate=Decimal(str(rate)), date=day or date.today(),
        source="test",
    ))
    await session.commit()


def _next_first_of_month(d: date) -> date:
    """Return the 1st day of the next month after `d` — useful to anchor
    a monthly recurring at a predictable day_of_month=1 cadence."""
    if d.month == 12:
        return date(d.year + 1, 1, 1)
    return date(d.year, d.month + 1, 1)


@pytest.mark.asyncio
async def test_cash_flow_recurring_in_foreign_currency(
    session: AsyncSession, test_user, test_workspace: User
):
    """A recurring in USD with primary BRL is converted via the FX rate."""
    # Primary currency for test_user is BRL. Seed USD->BRL = 5.0.
    # rate(USD->BRL) via cross = usd_to_BRL / usd_to_USD = 5.0 / 1 = 5.0.
    await _seed_fx_rate(session, "USD", "BRL", 5.0)

    account = await _make_manual_account(session, test_user.id, "CF MultiCcy")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    nxt = _next_first_of_month(today)
    # Recurring in USD: $200 on the 1st of each month → 1000 BRL @ rate 5.0
    await _make_recurring(
        session, test_user.id, account.id,
        amount=200, txn_type="credit", frequency="monthly",
        day_of_month=1, next_occurrence=nxt, currency="USD",
    )

    # 3-month window from today. Window covers exactly 3 "1st of month" anchors.
    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    # 3 occurrences × 200 USD × 5.0 = 3000 BRL
    assert proj_income.value == 3000.0


@pytest.mark.asyncio
async def test_cash_flow_mixed_currencies(session: AsyncSession, test_user, test_workspace: User):
    """Recurrings in multiple currencies converted correctly using cached rates."""
    # Primary is BRL. Seed cross rates via USD anchor.
    # USD->BRL = 5.0, USD->EUR = 0.5  ⇒  EUR->BRL = 5.0 / 0.5 = 10.0
    await _seed_fx_rate(session, "USD", "BRL", 5.0)
    await _seed_fx_rate(session, "USD", "EUR", 0.5)

    account = await _make_manual_account(session, test_user.id, "CF Mixed")
    await _add_txn(
        session, test_user.id, account.id, 0, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    nxt = _next_first_of_month(today)
    # 100 EUR on the 1st → 1000 BRL/month
    await _make_recurring(
        session, test_user.id, account.id,
        amount=100, txn_type="credit", frequency="monthly",
        day_of_month=1, next_occurrence=nxt, currency="EUR",
        description="EUR income",
    )
    # 100 BRL on the 1st (same anchor, no conversion)
    await _make_recurring(
        session, test_user.id, account.id,
        amount=100, txn_type="debit", frequency="monthly",
        day_of_month=1, next_occurrence=nxt, currency="BRL",
        description="BRL expense",
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")

    # Window covers exactly 2 "1st of month" anchors.
    # 2 EUR occurrences × 100 × 10.0 = 2000 BRL
    assert proj_income.value == 2000.0
    # 2 BRL occurrences × 100 = 200 BRL (no conversion)
    assert proj_exp.value == 200.0


@pytest.mark.asyncio
async def test_cash_flow_currency_conversion_uses_amount_primary_when_present(
    session: AsyncSession, test_user, test_workspace: User
):
    """A future-dated transaction with amount_primary set uses that value,
    skipping FX conversion."""
    account = await _make_manual_account(session, test_user.id, "CF AmtPrimary")
    await _add_txn(
        session, test_user.id, account.id, 0, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    # Tx in USD with amount_primary already stamped at a frozen rate
    txn = Transaction(
        id=uuid.uuid4(), user_id=test_user.id, account_id=account.id,
        description="Frozen-rate tx",
        amount=Decimal("100"), currency="USD",
        amount_primary=Decimal("777.77"),  # arbitrary stamped value
        date=today + timedelta(days=5), effective_date=today + timedelta(days=5),
        type="credit", source="manual",
        created_at=datetime.now(timezone.utc),
    )
    session.add(txn)
    await session.commit()

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=1, interval="daily")
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    assert proj_income.value == 777.77


# ---------------------------------------------------------------------------
# Cash flow — end_date stricter assertions
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_cash_flow_end_date_exact_occurrence_count(
    session: AsyncSession, test_user, test_workspace: User
):
    """When end_date falls mid-window, exactly the expected number of
    occurrences is projected — not more, not fewer."""
    account = await _make_manual_account(session, test_user.id, "CF EndDateExact")
    await _add_txn(
        session, test_user.id, account.id, 0, "credit",
        date.today(), source="opening_balance",
    )

    today = date.today()
    # Recurring weekly +$50 starting in 1 day, ending 21 days from now (so
    # exactly 3 occurrences fit: day+1, day+8, day+15; day+22 is past end).
    nxt = today + timedelta(days=1)
    end_d = today + timedelta(days=21)
    await _make_recurring(
        session, test_user.id, account.id,
        amount=50, txn_type="credit", frequency="weekly",
        next_occurrence=nxt, end_date=end_d,
    )

    # Window of 6 months — well past end_date
    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=6, interval="daily")
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    # Exactly 3 × 50 = 150
    assert proj_income.value == 150.0


@pytest.mark.asyncio
async def test_cash_flow_end_date_balance_freezes_after(
    session: AsyncSession, test_user, test_workspace: User
):
    """After the recurring's end_date, the running balance stops changing."""
    account = await _make_manual_account(session, test_user.id, "CF EndDateFreeze")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )

    today = date.today()
    nxt = today + timedelta(days=1)
    end_d = today + timedelta(days=21)
    await _make_recurring(
        session, test_user.id, account.id,
        amount=50, txn_type="credit", frequency="weekly",
        next_occurrence=nxt, end_date=end_d,
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=4, interval="daily")

    # Balance after end_date should match ending balance — it shouldn't keep
    # growing once the recurring stops contributing.
    after_end_label = (end_d + timedelta(days=10)).isoformat()
    after_end_point = next(p for p in report.trend if p.date == after_end_label)
    ending = next(b for b in report.summary.breakdowns if b.key == "endingBalance")
    assert after_end_point.value == ending.value
    # And the value equals starting (1000) + 3 × 50 = 1150
    assert ending.value == 1150.0


@pytest.mark.asyncio
async def test_cash_flow_end_date_at_exact_occurrence(
    session: AsyncSession, test_user, test_workspace: User
):
    """end_date == an occurrence date — that occurrence should still count."""
    account = await _make_manual_account(session, test_user.id, "CF EndDateExactDay")
    await _add_txn(
        session, test_user.id, account.id, 0, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    # Weekly: occurrences at day+7, day+14, day+21. end_date = day+14 includes 2.
    await _make_recurring(
        session, test_user.id, account.id,
        amount=10, txn_type="credit", frequency="weekly",
        next_occurrence=today + timedelta(days=7),
        end_date=today + timedelta(days=14),
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    # 2 occurrences × $10 = $20
    assert proj_income.value == 20.0


@pytest.mark.asyncio
async def test_cash_flow_end_date_in_past_no_contribution(
    session: AsyncSession, test_user, test_workspace: User
):
    """If end_date is in the past, no occurrences project."""
    account = await _make_manual_account(session, test_user.id, "CF EndDatePast")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    await _make_recurring(
        session, test_user.id, account.id,
        amount=999, txn_type="credit", frequency="monthly",
        next_occurrence=today + timedelta(days=2),
        end_date=today - timedelta(days=10),
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    assert proj_income.value == 0.0



@pytest.mark.asyncio
async def test_cash_flow_overdraft_crunch_point_goes_negative(
    session: AsyncSession, test_user, test_workspace: User
):
    """Cash crunch / overdraft: when projected outflows exceed inflows + start,
    the running balance must go negative (not be clamped at 0)."""
    account = await _make_manual_account(session, test_user.id, "CF Crunch")
    await _add_txn(
        session, test_user.id, account.id, 100, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    nxt = _next_first_of_month(today)
    # $200 monthly debit on the 1st — burns through the $100 starting balance
    await _make_recurring(
        session, test_user.id, account.id,
        amount=200, txn_type="debit", frequency="monthly",
        day_of_month=1, next_occurrence=nxt,
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=3, interval="daily")

    negative_points = [p for p in report.trend if p.value < 0]
    assert len(negative_points) > 0, "Trend never dips below zero — clamped?"
    ending = next(b for b in report.summary.breakdowns if b.key == "endingBalance")
    # 100 starting - 3*200 outflows = -500
    assert ending.value == -500.0


@pytest.mark.asyncio
async def test_cash_flow_paycheck_timing_dip(
    session: AsyncSession, test_user, test_workspace: User
):
    """Classic paycheck-timing dip: rent on day 5, salary on day 15. End of
    month is positive, but mid-month should drop below the starting balance
    (and possibly below 0). The trend must show that intra-month dip — not
    just the net monthly figure."""
    account = await _make_manual_account(session, test_user.id, "CF PayTiming")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()

    # Anchor rent and salary in NEXT month so they fall in the projection
    # window without ambiguity. day_of_month=5 for rent, =15 for salary.
    next_month_first = _next_first_of_month(today)
    rent_day = next_month_first.replace(day=5)
    salary_day = next_month_first.replace(day=15)

    await _make_recurring(
        session, test_user.id, account.id,
        amount=1500, txn_type="debit", frequency="monthly",
        day_of_month=5, next_occurrence=rent_day,
        description="Rent",
    )
    await _make_recurring(
        session, test_user.id, account.id,
        amount=3000, txn_type="credit", frequency="monthly",
        day_of_month=15, next_occurrence=salary_day,
        description="Salary",
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")

    # Fetch balance on rent day (after rent hit) and salary day (after salary hit)
    rent_pt = next(p for p in report.trend if p.date == rent_day.isoformat())
    salary_pt = next(p for p in report.trend if p.date == salary_day.isoformat())
    end_pt = report.trend[-1]

    # Day 5: 1000 - 1500 = -500 (overdraft mid-month!)
    assert rent_pt.value == -500.0
    # Day 15: -500 + 3000 = 2500
    assert salary_pt.value == 2500.0
    # End of window: net positive despite the mid-month dip
    assert end_pt.value > 0


@pytest.mark.asyncio
async def test_cash_flow_runway_is_first_negative_day_even_when_aggregated(
    session: AsyncSession, test_user, test_workspace: User
):
    """Rent on day 5 overdraws the account until salary lands on day 15.
    The runway is the rent day and the lowest point is its balance, also on
    a monthly chart where the month-end balance is positive again."""
    account = await _make_manual_account(session, test_user.id, "CF Runway Dip")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    next_month_first = _next_first_of_month(date.today())
    rent_day = next_month_first.replace(day=5)
    await _make_recurring(
        session, test_user.id, account.id,
        amount=1500, txn_type="debit", frequency="monthly",
        day_of_month=5, next_occurrence=rent_day, description="Rent",
    )
    await _make_recurring(
        session, test_user.id, account.id,
        amount=3000, txn_type="credit", frequency="monthly",
        day_of_month=15, next_occurrence=next_month_first.replace(day=15),
        description="Salary",
    )

    report = await get_cash_flow_report(
        session, test_workspace.id, test_user.id, months=2, interval="monthly",
    )

    assert all(p.value >= 0 for p in report.trend)
    assert report.meta.runway_date == rent_day.isoformat()
    assert report.meta.lowest_balance == -500.0
    assert report.meta.lowest_balance_date == rent_day.isoformat()


@pytest.mark.asyncio
async def test_cash_flow_runway_absent_when_balance_stays_positive(
    session: AsyncSession, test_user, test_workspace: User
):
    """A $200 monthly bill against $1000 never overdraws in 3 months: no
    runway date, and the lowest point is after the third bill."""
    account = await _make_manual_account(session, test_user.id, "CF Runway Safe")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    nxt = _next_first_of_month(date.today())
    await _make_recurring(
        session, test_user.id, account.id,
        amount=200, txn_type="debit", frequency="monthly",
        day_of_month=1, next_occurrence=nxt,
    )

    report = await get_cash_flow_report(
        session, test_workspace.id, test_user.id, months=3, interval="daily",
    )

    assert report.meta.runway_date is None
    assert report.meta.lowest_balance == 400.0
    third_bill = _next_first_of_month(_next_first_of_month(nxt))
    assert report.meta.lowest_balance_date == third_bill.isoformat()


@pytest.mark.asyncio
async def test_cash_flow_runway_is_today_when_already_negative(
    session: AsyncSession, test_user, test_workspace: User
):
    """An account that is overdrawn today has already run out: the runway
    is today, and with no flows the lowest point is today's balance."""
    account = await _make_manual_account(session, test_user.id, "CF Runway Overdrawn")
    await _add_txn(
        session, test_user.id, account.id, 250, "debit",
        date.today(), source="opening_balance",
    )

    report = await get_cash_flow_report(
        session, test_workspace.id, test_user.id, months=1, interval="daily",
    )

    assert report.meta.runway_date == date.today().isoformat()
    assert report.meta.lowest_balance == -250.0
    assert report.meta.lowest_balance_date == date.today().isoformat()


@pytest.mark.asyncio
async def test_cash_flow_large_one_off_purchase(
    session: AsyncSession, test_user, test_workspace: User
):
    """User wants to plan a single large purchase (e.g., new laptop on day +20).
    The chart must show a single dip on that exact day and recover for the
    rest of the window (no recurring pattern)."""
    account = await _make_manual_account(session, test_user.id, "CF Laptop")
    await _add_txn(
        session, test_user.id, account.id, 5000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    purchase_day = today + timedelta(days=20)
    await _add_txn(
        session, test_user.id, account.id, 1800, "debit", purchase_day,
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")

    # Day before purchase: full starting balance
    pre_pt = next(p for p in report.trend if p.date == (purchase_day - timedelta(days=1)).isoformat())
    purchase_pt = next(p for p in report.trend if p.date == purchase_day.isoformat())
    end_pt = report.trend[-1]

    assert pre_pt.value == 5000.0
    assert purchase_pt.value == 3200.0
    assert purchase_pt.breakdowns["outflow"] == 1800.0
    # No recurrings → balance stays at 3200 for rest of window
    assert end_pt.value == 3200.0


@pytest.mark.asyncio
async def test_cash_flow_multiple_recurrings_same_day(
    session: AsyncSession, test_user, test_workspace: User
):
    """Multiple bills that hit the same day must be summed into one outflow
    bucket on that day, not represented as separate days."""
    account = await _make_manual_account(session, test_user.id, "CF SameDay")
    await _add_txn(
        session, test_user.id, account.id, 5000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    nxt = _next_first_of_month(today)

    # Three different bills, all day 1
    for amt, desc in [(800, "Rent"), (120, "Internet"), (60, "Streaming")]:
        await _make_recurring(
            session, test_user.id, account.id,
            amount=amt, txn_type="debit", frequency="monthly",
            day_of_month=1, next_occurrence=nxt, description=desc,
        )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=1, interval="daily")
    bill_day = next(p for p in report.trend if p.date == nxt.isoformat())

    # All three bills stacked on the same day = 800 + 120 + 60 = 980
    assert bill_day.breakdowns["outflow"] == 980.0
    # No other day in the window has any outflow
    other_outflow_days = [
        p for p in report.trend
        if p.date != nxt.isoformat() and p.breakdowns["outflow"] > 0
    ]
    assert other_outflow_days == []


@pytest.mark.asyncio
async def test_cash_flow_yearly_recurring(session: AsyncSession, test_user, test_workspace: User):
    """Yearly recurring (e.g., annual car insurance): expansion in a 12-month
    window should yield exactly one occurrence."""
    account = await _make_manual_account(session, test_user.id, "CF Yearly")
    await _add_txn(
        session, test_user.id, account.id, 5000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    # Schedule it 30 days out so it falls inside both 1mo+ and 12mo windows
    nxt = today + timedelta(days=30)
    await _make_recurring(
        session, test_user.id, account.id,
        amount=1200, txn_type="debit", frequency="yearly",
        day_of_month=nxt.day, next_occurrence=nxt,
    )

    rep_12 = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=12, interval="daily")
    proj_exp_12 = next(b for b in rep_12.summary.breakdowns if b.key == "projectedExpenses")
    assert proj_exp_12.value == 1200.0  # exactly one occurrence in 12 months

    # In a 1-month window: also 1 (since nxt is +30 days)
    rep_1 = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=2, interval="daily")
    proj_exp_1 = next(b for b in rep_1.summary.breakdowns if b.key == "projectedExpenses")
    assert proj_exp_1.value == 1200.0


@pytest.mark.asyncio
async def test_cash_flow_recurring_starting_in_future(
    session: AsyncSession, test_user, test_workspace: User
):
    """A recurring whose start_date is in the future (e.g., a new subscription
    starting next month) must be projected from its start_date forward."""
    account = await _make_manual_account(session, test_user.id, "CF FutureStart")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    # Subscription starts at the 1st of the month after next, $20/month
    start = _next_first_of_month(_next_first_of_month(today))
    await _make_recurring(
        session, test_user.id, account.id,
        amount=20, txn_type="debit", frequency="monthly",
        day_of_month=1, start_date=start, next_occurrence=start,
        description="Future subscription",
    )

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=4, interval="daily")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")

    # The subscription starts 1-2 months out; at month=4 we expect ~2-3 hits.
    # Loose check: at least 1 occurrence and the very first day shows no impact.
    assert proj_exp.value >= 20.0
    first_pt = report.trend[0]
    assert first_pt.value == 1000.0  # starting balance untouched


@pytest.mark.asyncio
async def test_cash_flow_running_balance_arithmetic(
    session: AsyncSession, test_user, test_workspace: User
):
    """Strict arithmetic: balance on day N == starting + sum(flows up to day N).
    Anchors the math so refactors can't silently break the cumulative sum."""
    account = await _make_manual_account(session, test_user.id, "CF Math")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()

    # Place specific transactions on specific days
    flows = [
        (today + timedelta(days=3), 200, "credit"),   # +200 on day 3
        (today + timedelta(days=10), 50, "debit"),    # -50 on day 10
        (today + timedelta(days=15), 75, "debit"),    # -75 on day 15
        (today + timedelta(days=20), 500, "credit"),  # +500 on day 20
    ]
    for d, amt, typ in flows:
        await _add_txn(session, test_user.id, account.id, amt, typ, d)

    report = await get_cash_flow_report(session, test_workspace.id, test_user.id, months=1, interval="daily")
    by_date = {p.date: p for p in report.trend}

    # Reconstruct expected running balance day by day.
    expected = 1000.0
    daily_signed = {d: (amt if typ == "credit" else -amt) for d, amt, typ in flows}
    for offset in range(0, 25):
        d = today + timedelta(days=offset)
        if d in daily_signed:
            expected += daily_signed[d]
        pt = by_date.get(d.isoformat())
        if pt is None:
            continue  # day past report window — not all 25 days fit
        assert abs(pt.value - expected) < 0.01, (
            f"Day {d}: got {pt.value}, expected {expected}"
        )


# ---------------------------------------------------------------------------
# Baseline mode (historical-mean forecast) — issue #179
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_cash_flow_baseline_off_uses_recurring(
    session: AsyncSession, test_user, test_workspace: User
):
    """baseline=False is the legacy behavior: only recurring rules project."""
    account = await _make_manual_account(session, test_user.id, "CF Baseline Off")
    await _add_txn(
        session, test_user.id, account.id, 5000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    # 30 days of past actuals that baseline mode would otherwise pick up.
    for offset in range(1, 31):
        await _add_txn(
            session, test_user.id, account.id, 100, "debit",
            today - timedelta(days=offset),
        )
    # One recurring rule.
    await _make_recurring(
        session, test_user.id, account.id,
        amount=1500, txn_type="credit", frequency="monthly",
        next_occurrence=today + timedelta(days=2),
    )

    report = await get_cash_flow_report(
        session, test_workspace.id, test_user.id, months=3, interval="daily", baseline=False,
    )

    assert report.meta.baseline_active is False
    assert report.meta.baseline_lookback_days is None
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    # Recurring credit fires; past debits do NOT project forward.
    assert proj_income.value >= 1500.0
    assert proj_exp.value == 0.0


@pytest.mark.asyncio
async def test_cash_flow_baseline_on_replaces_recurring_with_mean(
    session: AsyncSession, test_user, test_workspace: User
):
    """baseline=True ignores recurring rules and projects from historical mean.

    Setup: 30 days × R$100 debits in the past (no income, no recurring
    debits set up). Baseline should pick those up and project them forward.
    """
    account = await _make_manual_account(session, test_user.id, "CF Baseline On")
    await _add_txn(
        session, test_user.id, account.id, 5000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    # Past actuals: 30 daily R$100 debits → total R$3000 over the lookback.
    for offset in range(1, 31):
        await _add_txn(
            session, test_user.id, account.id, 100, "debit",
            today - timedelta(days=offset),
        )
    # A recurring credit rule that baseline mode should IGNORE.
    await _make_recurring(
        session, test_user.id, account.id,
        amount=9999, txn_type="credit", frequency="monthly",
        next_occurrence=today + timedelta(days=2),
    )

    report = await get_cash_flow_report(
        session, test_workspace.id, test_user.id, months=3, interval="daily", baseline=True,
    )

    assert report.meta.baseline_active is True
    # Lookback window = 30 days of past data (capped by earliest tx, not by
    # the 12-month maximum).
    assert report.meta.baseline_lookback_days == 30
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    # Recurring R$9999 credit should NOT appear (baseline replaces recurring).
    assert proj_income.value < 100.0
    # Outflow should reflect ~R$100/day × 3 months ≈ R$9000.
    assert proj_exp.value > 8000.0
    assert proj_exp.value < 10000.0


@pytest.mark.asyncio
async def test_cash_flow_baseline_lookback_caps_at_twelve_months(
    session: AsyncSession, test_user, test_workspace: User
):
    """User with >12 months of history sees lookback capped at 365 days."""
    account = await _make_manual_account(session, test_user.id, "CF Baseline Cap")
    await _add_txn(
        session, test_user.id, account.id, 5000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    # Single very old transaction (18 months ago) plus one recent one.
    await _add_txn(
        session, test_user.id, account.id, 50, "debit",
        today - timedelta(days=540),
    )
    await _add_txn(
        session, test_user.id, account.id, 50, "debit",
        today - timedelta(days=10),
    )

    report = await get_cash_flow_report(
        session, test_workspace.id, test_user.id, months=3, interval="daily", baseline=True,
    )

    # Window is bounded by `today - 365 days`, not by the 540-days-ago tx.
    # Allow ±2-day tolerance for month-arithmetic edge cases.
    assert report.meta.baseline_lookback_days is not None
    assert 363 <= report.meta.baseline_lookback_days <= 367


@pytest.mark.asyncio
async def test_cash_flow_baseline_lookback_adapts_to_short_history(
    session: AsyncSession, test_user, test_workspace: User
):
    """User with only N days of history sees lookback shrink to N."""
    account = await _make_manual_account(session, test_user.id, "CF Baseline Short")
    await _add_txn(
        session, test_user.id, account.id, 1000, "credit",
        date.today(), source="opening_balance",
    )
    today = date.today()
    # Just 7 days of activity.
    for offset in range(1, 8):
        await _add_txn(
            session, test_user.id, account.id, 20, "debit",
            today - timedelta(days=offset),
        )

    report = await get_cash_flow_report(
        session, test_workspace.id, test_user.id, months=3, interval="daily", baseline=True,
    )

    # Earliest tx is 7 days ago, so lookback is 7 days.
    assert report.meta.baseline_lookback_days == 7
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    # 7 days × R$20 = R$140 over 7 days → R$20/day → ~R$1820 over ~91 days.
    assert proj_exp.value > 1500.0
    assert proj_exp.value < 2200.0


@pytest.mark.asyncio
async def test_cash_flow_baseline_no_history_returns_empty_projection(
    session: AsyncSession, test_user, test_workspace: User
):
    """User with no qualifying transactions → baseline contributes zero.

    Chart should still show starting balance flat across the forecast.
    """
    account = await _make_manual_account(session, test_user.id, "CF Baseline Empty")
    await _add_txn(
        session, test_user.id, account.id, 2000, "credit",
        date.today(), source="opening_balance",
    )

    report = await get_cash_flow_report(
        session, test_workspace.id, test_user.id, months=3, interval="daily", baseline=True,
    )

    assert report.meta.baseline_active is True
    assert report.meta.baseline_lookback_days == 0
    proj_income = next(b for b in report.summary.breakdowns if b.key == "projectedIncome")
    proj_exp = next(b for b in report.summary.breakdowns if b.key == "projectedExpenses")
    assert proj_income.value == 0.0
    assert proj_exp.value == 0.0
    # Ending balance equals starting (flat line).
    assert report.summary.change_amount == 0.0


@pytest.mark.asyncio
async def test_cash_flow_meta_carries_forecast_start_date(
    session: AsyncSession, test_user, test_workspace: User
):
    """forecast_start_date in meta lets the UI split solid vs dashed at today."""
    account = await _make_manual_account(session, test_user.id, "CF Forecast Start")
    await _add_txn(
        session, test_user.id, account.id, 100, "credit",
        date.today(), source="opening_balance",
    )

    report = await get_cash_flow_report(
        session, test_workspace.id, test_user.id, months=3, interval="daily",
    )

    assert report.meta.forecast_start_date == date.today().isoformat()


@pytest.mark.asyncio
async def test_cash_flow_chart_includes_past_history(
    session: AsyncSession, test_user, test_workspace: User
):
    """Trend starts ~1 month before today so the today-marker has context."""
    account = await _make_manual_account(session, test_user.id, "CF Past")
    await _add_txn(
        session, test_user.id, account.id, 500, "credit",
        date.today(), source="opening_balance",
    )

    report = await get_cash_flow_report(
        session, test_workspace.id, test_user.id, months=3, interval="daily",
    )

    # First trend point should be ~30 days before today.
    first_date = date.fromisoformat(report.trend[0].date)
    today = date.today()
    delta_days = (today - first_date).days
    # _PAST_HISTORY_MONTHS = 1 (28–31 days depending on month).
    assert 27 <= delta_days <= 32


def test_add_years_clamps_leap_day():
    """Feb 29 + N years clamps to Feb 28 when the target year isn't leap."""
    assert _add_years(date(2016, 2, 29), 10) == date(2026, 2, 28)
    # Target year is also a leap year, so Feb 29 survives untouched.
    assert _add_years(date(2020, 2, 29), 4) == date(2024, 2, 29)
    assert _add_years(date(2016, 9, 12), 10) == date(2026, 9, 12)


@pytest.mark.parametrize('endpoint,service_name', [
    ('net-worth', 'get_net_worth_report'),
    ('income-expenses', 'get_income_expenses_report'),
])
@pytest.mark.parametrize('start_offset,end_offset,status', [
    # FixedDate.today() is 2026-09-12; -3652 days is exactly 2016-09-12, the
    # 10-calendar-year anniversary. One day further back crosses it, even
    # though the day-count difference (3653 vs. 3652) understates the effect
    # of the two leap days (2020, 2024) inside the window.
    (-1, 1, 422), (0, 0, 200), (-3652, 0, 200), (-3653, 0, 422),
    (None, 0, 422), (-1, None, 422), (0, -1, 422),
])
async def test_historical_api_custom_range_boundaries(
    client, auth_headers, monkeypatch, endpoint, service_name,
    start_offset, end_offset, status,
):
    from app.api import reports as reports_api

    class FixedDate(date):
        @classmethod
        def today(cls):
            return cls(2026, 9, 12)

    monkeypatch.setattr(reports_api, 'date', FixedDate)
    # The guard reads "today" from the app's calendar, not from the module's
    # date class, so both seams get the same fixed day.
    monkeypatch.setattr(reports_api, 'app_today', lambda: FixedDate.today())
    report = ReportResponse(
        summary=ReportSummary(primary_value=0, change_amount=0, change_percent=None,
                              breakdowns=[]),
        trend=[], meta=ReportMeta(type=endpoint, series_keys=[], currency='USD',
                                 interval='monthly'),
        composition=[], category_trend=[],
    )
    service = AsyncMock(return_value=report)
    monkeypatch.setattr(report_service, service_name, service)
    params = {}
    for name, offset in [('start_date', start_offset), ('end_date', end_offset)]:
        if offset is not None:
            params[name] = (FixedDate.today() + timedelta(days=offset)).isoformat()
    response = await client.get(f'/api/reports/{endpoint}', params=params, headers=auth_headers)
    assert response.status_code == status, response.text
    if status == 200:
        service.assert_awaited_once()
        assert service.call_args.kwargs['start_date'].isoformat() == params['start_date']
        assert service.call_args.kwargs['end_date'].isoformat() == params['end_date']
    else:
        service.assert_not_awaited()
        assert isinstance(response.json()['detail'], str)


@pytest.mark.parametrize('endpoint,service_name', [
    ('net-worth', 'get_net_worth_report'),
    ('income-expenses', 'get_income_expenses_report'),
])
async def test_custom_dates_override_indian_yearly_ytd(
    client, auth_headers, monkeypatch, endpoint, service_name,
):
    context = SimpleNamespace(
        workspace=SimpleNamespace(id=uuid.uuid4(), tax_jurisdiction='IN'),
        user_id=uuid.uuid4(), user=SimpleNamespace(primary_currency='INR'),
    )

    async def override_workspace():
        return context

    report = ReportResponse(
        summary=ReportSummary(primary_value=0, change_amount=0, change_percent=None,
                              breakdowns=[]),
        trend=[], meta=ReportMeta(type=endpoint, series_keys=[], currency='INR',
                                 interval='yearly'), composition=[], category_trend=[],
    )
    service = AsyncMock(return_value=report)
    monkeypatch.setattr(report_service, service_name, service)
    app.dependency_overrides[current_workspace] = override_workspace
    try:
        params = {'period': 'ytd', 'interval': 'yearly'}
        rejected = await client.get(f'/api/reports/{endpoint}', params=params,
                                    headers=auth_headers)
        assert rejected.status_code == 422
        service.assert_not_awaited()
        params.update(start_date='2022-03-10', end_date='2022-06-15')
        accepted = await client.get(f'/api/reports/{endpoint}', params=params,
                                    headers=auth_headers)
        assert accepted.status_code == 200, accepted.text
        assert service.call_args.kwargs['period'] is None
        assert service.call_args.kwargs['financial_year_start_month'] == 4
    finally:
        app.dependency_overrides.pop(current_workspace, None)


# ---------------------------------------------------------------------------
# _resolve_custom_range: "today" must come from the app's calendar
# (workspace or application timezone), never the host clock. The service
# below builds its windows from `app_today()`, so a guard that compares
# against a different calendar either rejects a legitimate "today" or
# accepts a future date (issue #840).
# ---------------------------------------------------------------------------


def test_custom_range_accepts_end_date_that_is_app_today():
    host_today = date.today()
    # The workspace runs one day ahead of the host clock: its "today" is
    # what the host only sees as tomorrow, and a report through the app's
    # today is a normal, current request.
    with patch("app.api.reports.app_today", return_value=host_today + timedelta(days=1)):
        start = host_today
        end = host_today + timedelta(days=1)
        assert _resolve_custom_range(start, end) == (start, end)


def test_custom_range_rejects_end_date_after_app_today():
    host_today = date.today()
    # The workspace runs one day behind the host clock: the host's today
    # is still in the future for the workspace, so it is not a valid end.
    with patch("app.api.reports.app_today", return_value=host_today - timedelta(days=1)):
        with pytest.raises(HTTPException) as exc:
            _resolve_custom_range(host_today - timedelta(days=1), host_today)
        assert exc.value.detail == "end_date must be on or before today"


def _host_and_shifted_zone() -> tuple[ZoneInfo, date, date]:
    """A zone whose calendar date differs from the host's right now.

    Kiritimati (UTC+14) and Pago Pago (UTC-11) are 25 hours apart, so they
    always sit on different days, and one of the two always differs from
    the host's day no matter which offset the host runs on.
    """
    host_today = date.today()
    for name in ("Pacific/Kiritimati", "Pacific/Pago_Pago"):
        zone = ZoneInfo(name)
        if today_in(zone) != host_today:
            return zone, today_in(zone), host_today
    pytest.fail("expected a zone on a different calendar day from the host")


def test_custom_range_guard_follows_resolved_timezone():
    zone, zone_today, host_today = _host_and_shifted_zone()
    with use_resolved_timezone(zone):
        if zone_today > host_today:
            # Workspace ahead of the host: asking for the workspace's own
            # today must be accepted.
            start = zone_today - timedelta(days=7)
            assert _resolve_custom_range(start, zone_today) == (start, zone_today)
        else:
            # Workspace behind the host: the host's today is a future date
            # for the workspace and must be rejected.
            with pytest.raises(HTTPException) as exc:
                _resolve_custom_range(zone_today, host_today)
            assert exc.value.detail == "end_date must be on or before today"


@pytest.mark.asyncio
async def test_net_worth_keeps_sub_cent_foreign_assets(
    session: AsyncSession, test_user, test_workspace
):
    """Low-priced foreign-currency holdings must add up in the net worth.

    Three coins at 0.001 USD convert to 0.005 BRL each. A two-decimal
    conversion made each one 0.00 (or 0.01, depending on rounding); converting
    at price precision and summing in Decimal gives 0.015, shown as 0.02. A
    float sum would round 0.015 down to 0.01.
    """
    from app.models.fx_rate import FxRate

    today = date.today()
    session.add(FxRate(
        id=uuid.uuid4(), base_currency="USD", quote_currency="BRL",
        date=today, rate=Decimal("5.0"), source="test",
    ))
    for i in range(3):
        coin = Asset(
            id=uuid.uuid4(), user_id=test_user.id, name=f"Coin {i}",
            type="investment", currency="USD", valuation_method="market_price",
            units=Decimal("1"), last_price=Decimal("0.001"), position=i,
        )
        session.add(coin)
        await session.flush()
        session.add(AssetValue(
            id=uuid.uuid4(), asset_id=coin.id,
            amount=Decimal("0.001"), date=today, source="sync",
        ))
    await session.commit()

    total = await _asset_value_at(session, test_workspace.id, today, "BRL")
    assert total == pytest.approx(0.015)

    point = await _net_worth_at(session, test_workspace.id, today, "BRL")
    assert point.breakdowns["assets"] == pytest.approx(0.02)