import httpx
import respx

from whatsrb_cloud import WhatsrbCloud
from whatsrb_cloud.objects.business_account import BusinessAccount
from whatsrb_cloud.objects.connect_request import ConnectRequest
from whatsrb_cloud.objects.list_response import ListResponse

BASE_URL = "https://whatsrb.com/api/v1"

ACCOUNT_DATA = {
    "id": "ba_1",
    "waba_id": "waba_123",
    "business_name": "Test Corp",
    "display_name": "Test",
    "phone_number": "+33612345678",
    "phone_number_id": "pn_123",
    "status": "connected",
    "quality_rating": "GREEN",
    "connected": True,
    "meta_token_expires_at": None,
}


class TestBusinessAccountsResource:
    @respx.mock
    def test_list(self):
        respx.get(f"{BASE_URL}/business_accounts").mock(
            return_value=httpx.Response(200, json={"data": [ACCOUNT_DATA], "meta": {"total": 1}})
        )
        client = WhatsrbCloud("wrb_test")
        result = client.business_accounts.list()
        assert isinstance(result, ListResponse)
        assert len(result) == 1
        assert result[0].business_name == "Test Corp"

    @respx.mock
    def test_retrieve(self):
        respx.get(f"{BASE_URL}/business_accounts/ba_1").mock(
            return_value=httpx.Response(200, json={"data": ACCOUNT_DATA})
        )
        client = WhatsrbCloud("wrb_test")
        account = client.business_accounts.retrieve("ba_1")
        assert isinstance(account, BusinessAccount)
        assert account.is_connected is True

    @respx.mock
    def test_connect(self):
        connect_data = {
            "id": "cr_1",
            "url": "https://connect.example.com",
            "status": "pending",
            "expires_at": "2025-01-01T01:00:00Z",
        }
        respx.post(f"{BASE_URL}/business_accounts/connect").mock(
            return_value=httpx.Response(201, json={"data": connect_data})
        )
        client = WhatsrbCloud("wrb_test")
        cr = client.business_accounts.connect()
        assert isinstance(cr, ConnectRequest)
        assert cr.is_pending is True

    @respx.mock
    def test_connect_manual(self):
        respx.post(f"{BASE_URL}/business_accounts").mock(
            return_value=httpx.Response(201, json={"data": ACCOUNT_DATA})
        )
        client = WhatsrbCloud("wrb_test")
        account = client.business_accounts.connect_manual(
            waba_id="waba_123", phone_number_id="pn_123", meta_token="token_xxx"
        )
        assert isinstance(account, BusinessAccount)

    @respx.mock
    def test_window(self):
        respx.get(f"{BASE_URL}/business_accounts/ba_1/window?phone_number=%2B33612345678").mock(
            return_value=httpx.Response(200, json={"data": {"open": True, "expires_at": "2025-01-01T12:00:00Z"}})
        )
        client = WhatsrbCloud("wrb_test")
        result = client.business_accounts.window("ba_1", phone_number="+33612345678")
        assert result["open"] is True

    @respx.mock
    def test_delete(self):
        respx.delete(f"{BASE_URL}/business_accounts/ba_1").mock(
            return_value=httpx.Response(204, text="")
        )
        client = WhatsrbCloud("wrb_test")
        assert client.business_accounts.delete("ba_1") is True
