import pytest

import whatsrb_cloud
from whatsrb_cloud import WhatsrbCloud
from whatsrb_cloud.errors import AuthenticationError
from whatsrb_cloud.resources.sessions import Sessions
from whatsrb_cloud.resources.webhooks import Webhooks
from whatsrb_cloud.resources.business_accounts import BusinessAccounts
from whatsrb_cloud.resources.agents import Agents
from whatsrb_cloud.resources.messages import Messages
from whatsrb_cloud.resources.business_messages import BusinessMessages
from whatsrb_cloud.resources.templates import Templates
from whatsrb_cloud.resources.agent_runs import AgentRuns
from whatsrb_cloud.resources.connects import Connects


class TestWhatsrbCloud:
    def setup_method(self):
        whatsrb_cloud.reset_configuration()

    def test_requires_api_key(self):
        with pytest.raises(AuthenticationError, match="API key is required"):
            WhatsrbCloud()

    def test_direct_api_key(self):
        client = WhatsrbCloud("wrb_test_xxx")
        assert repr(client) == "<WhatsrbCloud base_url='https://whatsrb.com' api_key=[FILTERED]>"

    def test_global_configure(self):
        whatsrb_cloud.configure(api_key="wrb_test_global")
        client = WhatsrbCloud()
        assert "[FILTERED]" in repr(client)

    def test_per_client_override(self):
        whatsrb_cloud.configure(api_key="wrb_test_global", base_url="https://global.example.com")
        client = WhatsrbCloud("wrb_override", base_url="https://override.example.com")
        assert "override.example.com" in repr(client)

    def test_sessions_resource(self):
        client = WhatsrbCloud("wrb_test")
        assert isinstance(client.sessions, Sessions)

    def test_webhooks_resource(self):
        client = WhatsrbCloud("wrb_test")
        assert isinstance(client.webhooks, Webhooks)

    def test_business_accounts_resource(self):
        client = WhatsrbCloud("wrb_test")
        assert isinstance(client.business_accounts, BusinessAccounts)

    def test_agents_resource(self):
        client = WhatsrbCloud("wrb_test")
        assert isinstance(client.agents, Agents)

    def test_connects_resource(self):
        client = WhatsrbCloud("wrb_test")
        assert isinstance(client.connects, Connects)

    def test_messages_resource(self):
        client = WhatsrbCloud("wrb_test")
        assert isinstance(client.messages("sess_1"), Messages)

    def test_business_messages_resource(self):
        client = WhatsrbCloud("wrb_test")
        assert isinstance(client.business_messages("ba_1"), BusinessMessages)

    def test_templates_resource(self):
        client = WhatsrbCloud("wrb_test")
        assert isinstance(client.templates("ba_1"), Templates)

    def test_agent_runs_resource(self):
        client = WhatsrbCloud("wrb_test")
        assert isinstance(client.agent_runs("agt_1"), AgentRuns)

    def test_reset_configuration(self):
        whatsrb_cloud.configure(api_key="wrb_test")
        whatsrb_cloud.reset_configuration()
        with pytest.raises(AuthenticationError):
            WhatsrbCloud()
