from __future__ import annotations

from pathlib import Path
from unittest import TestCase

_REPO_ROOT = Path(__file__).resolve().parents[2]


class TestPaymentStatusEndpointContracts(TestCase):
    """Source-code contracts for the billing payment status endpoint."""

    def _read_billing_source(self) -> str:
        path = _REPO_ROOT / "backend/app/routers/billing.py"
        return path.read_text(encoding="utf-8")

    def test_payment_status_endpoint_exists(self) -> None:
        source = self._read_billing_source()
        self.assertIn('@router.get("/payment/{payment_id}/status"', source)

    def test_payment_status_endpoint_returns_response_model(self) -> None:
        source = self._read_billing_source()
        self.assertIn("response_model=PaymentStatusResponse", source)

    def test_payment_status_requires_auth(self) -> None:
        source = self._read_billing_source()
        idx = source.index("async def get_payment_status")
        block = source[idx : idx + 400]
        self.assertIn("get_current_restaurant", block)

    def test_payment_status_calls_get_payment(self) -> None:
        source = self._read_billing_source()
        idx = source.index("async def get_payment_status")
        block = source[idx : idx + 1200]
        self.assertIn("get_payment(payment_id)", block)

    def test_payment_status_handles_not_found(self) -> None:
        source = self._read_billing_source()
        idx = source.index("async def get_payment_status")
        block = source[idx : idx + 1000]
        self.assertIn("HTTP_404_NOT_FOUND", block)

    def test_payment_status_handles_provider_not_configured(self) -> None:
        source = self._read_billing_source()
        idx = source.index("async def get_payment_status")
        block = source[idx : idx + 1000]
        self.assertIn("HTTP_503_SERVICE_UNAVAILABLE", block)

    def test_payment_status_handles_provider_error(self) -> None:
        source = self._read_billing_source()
        idx = source.index("async def get_payment_status")
        block = source[idx : idx + 1800]
        self.assertIn("HTTP_502_BAD_GATEWAY", block)


class TestPaymentStatusSchemaContracts(TestCase):
    """Source-code contracts for the payment status response model."""

    def _read_billing_source(self) -> str:
        path = _REPO_ROOT / "backend/app/routers/billing.py"
        return path.read_text(encoding="utf-8")

    def test_payment_status_response_model_exists(self) -> None:
        source = self._read_billing_source()
        self.assertIn("class PaymentStatusResponse", source)

    def test_payment_status_response_has_payment_id(self) -> None:
        source = self._read_billing_source()
        idx = source.index("class PaymentStatusResponse")
        block = source[idx : idx + 400]
        self.assertIn("payment_id: str", block)

    def test_payment_status_response_has_status(self) -> None:
        source = self._read_billing_source()
        idx = source.index("class PaymentStatusResponse")
        block = source[idx : idx + 400]
        self.assertIn("status: str", block)

    def test_payment_status_response_has_amount(self) -> None:
        source = self._read_billing_source()
        idx = source.index("class PaymentStatusResponse")
        block = source[idx : idx + 400]
        self.assertIn("amount:", block)

    def test_payment_status_response_has_currency(self) -> None:
        source = self._read_billing_source()
        idx = source.index("class PaymentStatusResponse")
        block = source[idx : idx + 400]
        self.assertIn("currency:", block)

    def test_payment_status_response_has_created_at(self) -> None:
        source = self._read_billing_source()
        idx = source.index("class PaymentStatusResponse")
        block = source[idx : idx + 400]
        self.assertIn("created_at:", block)
