from datetime import date
from decimal import Decimal

from finops_batches.providers.gcp.usage_gold import GcpUsageGoldTransformer


def _record(**overrides: object) -> dict[str, object]:
    record: dict[str, object] = {
        "business_date": date(2026, 11, 1),
        "ServiceProviderName": "Google Cloud",
        "BillingAccountId": "01FBF0-7A5A05-1309B5",
        "BillingAccountName": "gentleIT",
        "InvoiceIssuerName": "Google Cloud France SARL",
        "SubAccountId": "prj-a1001-dev-01",
        "SubAccountName": "AI Data and Ingest DEV",
        "ServiceCategory": "Compute",
        "ServiceName": "Compute Engine",
        "SkuId": "sku-1",
        "ChargeDescription": "CPU",
        "RegionId": "europe-west1",
        "RegionName": "europe-west1",
        "BillingCurrency": "EUR",
        "ConsumedQuantity": Decimal("2.5"),
        "ConsumedUnit": "hour",
        "ListCost": Decimal("4.0"),
        "ContractedCost": Decimal("3.0"),
        "BilledCost": Decimal("2.0"),
        "EffectiveCost": Decimal("2.0"),
        "ChargeCategory": "Usage",
        "PricingCategory": "Standard",
        "x_FocusConformanceStatus": "READY_FOR_CONFORMANCE_REVIEW",
    }
    record.update(overrides)
    return record


def test_transform_aggregates_same_grain() -> None:
    transformer = GcpUsageGoldTransformer()

    result = transformer.transform(
        [
            _record(),
            _record(
                ConsumedQuantity=Decimal("1.5"),
                ListCost=Decimal("2.0"),
                ContractedCost=Decimal("1.5"),
                BilledCost=Decimal("1.0"),
                EffectiveCost=Decimal("1.0"),
            ),
        ]
    )

    assert len(result) == 1
    row = result[0]
    assert row["consumed_quantity"] == Decimal("4.0")
    assert row["list_cost"] == Decimal("6.0")
    assert row["contracted_cost"] == Decimal("4.5")
    assert row["billed_cost"] == Decimal("3.0")
    assert row["effective_cost"] == Decimal("3.0")
    assert row["credit_amount"] == Decimal("-1.5")
    assert row["source_row_count"] == 2
    assert row["valued_row_count"] == 2
    assert row["unpriced_row_count"] == 0
    assert row["conformance_ready_row_count"] == 2


def test_transform_separates_pricing_categories() -> None:
    transformer = GcpUsageGoldTransformer()

    result = transformer.transform(
        [
            _record(PricingCategory="Standard"),
            _record(PricingCategory="Committed"),
        ]
    )

    assert len(result) == 2
    categories = {row["pricing_category"] for row in result}
    assert categories == {"Standard", "Committed"}


def test_transform_tracks_tax_and_adjustments() -> None:
    transformer = GcpUsageGoldTransformer()

    result = transformer.transform(
        [
            _record(
                ServiceCategory="Other",
                ServiceName="Invoice",
                SkuId=None,
                ChargeDescription="Tax",
                SubAccountId=None,
                SubAccountName=None,
                RegionId=None,
                RegionName=None,
                ConsumedQuantity=Decimal(0),
                ConsumedUnit="N/A",
                ChargeCategory="Tax",
                BilledCost=Decimal("5.17"),
                EffectiveCost=Decimal("5.17"),
                ContractedCost=Decimal("5.17"),
                ListCost=Decimal("5.17"),
            )
        ]
    )

    assert len(result) == 1
    row = result[0]
    assert row["tax_row_count"] == 1
    assert row["usage_row_count"] == 0
    assert row["billed_cost"] == Decimal("5.17")


def test_transform_tracks_partial_conformance() -> None:
    transformer = GcpUsageGoldTransformer()

    result = transformer.transform([_record(x_FocusConformanceStatus="PARTIAL")])

    assert len(result) == 1
    row = result[0]
    assert row["conformance_ready_row_count"] == 0
    assert row["conformance_partial_row_count"] == 1


def test_transform_grain_key_is_stable() -> None:
    transformer = GcpUsageGoldTransformer()

    first = transformer.transform([_record()])
    second = transformer.transform([_record()])

    assert first[0]["grain_key"] == second[0]["grain_key"]
    assert len(str(first[0]["grain_key"])) == 64


def test_transform_separates_projects() -> None:
    transformer = GcpUsageGoldTransformer()

    result = transformer.transform(
        [
            _record(SubAccountId="project-a", SubAccountName="Project A"),
            _record(SubAccountId="project-b", SubAccountName="Project B"),
        ]
    )

    assert len(result) == 2
    assert {row["project_id"] for row in result} == {"project-a", "project-b"}
