from __future__ import annotations

import pytest
from pydantic import ValidationError

from finops_batches.providers.gcp.config import GcpBillingSettings


def test_environment_configuration(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setenv("GCP_BILLING_BQ_SOURCE", "p.d.v_billing")
    monkeypatch.setenv("GCP_BILLING_PAGE_SIZE", "2500")
    monkeypatch.setenv("GCP_BILLING_MAXIMUM_BYTES_BILLED", "123")
    monkeypatch.setenv("GCP_BILLING_ALLOW_CURRENT_DAY", "true")
    monkeypatch.setenv("GCP_BILLING_SCOPE_ID", "BILLING-1")
    settings = GcpBillingSettings(_env_file=None)
    assert settings.gcp_billing_bq_source == "p.d.v_billing"
    assert settings.gcp_billing_page_size == 2500
    assert settings.maximum_bytes_billed == 123
    assert settings.gcp_billing_allow_current_day is True
    assert settings.scope_id == "BILLING-1"


def test_zero_max_bytes_means_no_guard(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setenv("GCP_BILLING_BQ_SOURCE", "p.d.t")
    monkeypatch.setenv("GCP_BILLING_MAXIMUM_BYTES_BILLED", "0")
    settings = GcpBillingSettings(_env_file=None)
    assert settings.maximum_bytes_billed is None


def test_blank_query_project_means_adc_project(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setenv("GCP_BILLING_BQ_SOURCE", "p.d.t")
    monkeypatch.setenv("GCP_BILLING_BQ_PROJECT_ID", " ")
    settings = GcpBillingSettings(_env_file=None)
    assert settings.query_project_id is None


@pytest.mark.parametrize("table", ["bad-name", "1table", "schema.table", "table name"])
def test_mariadb_table_names_are_validated(
    monkeypatch: pytest.MonkeyPatch,
    table: str,
) -> None:
    monkeypatch.setenv("GCP_BILLING_BQ_SOURCE", "p.d.t")
    monkeypatch.setenv("GCP_BILLING_BRONZE_TABLE", table)
    with pytest.raises(ValidationError):
        GcpBillingSettings(_env_file=None)


def test_non_empty_provider_setting_is_validated(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    monkeypatch.setenv("GCP_BILLING_BQ_SOURCE", "p.d.t")
    monkeypatch.setenv("GCP_BILLING_BQ_LOCATION", " ")
    with pytest.raises(ValidationError):
        GcpBillingSettings(_env_file=None)
