# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

import os
from typing import Any, cast

import pytest

from notiflows import Notiflows, AsyncNotiflows
from tests.utils import assert_matches_type
from notiflows.types import User
from notiflows.pagination import SyncCursorPage, AsyncCursorPage

base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")


class TestUsers:
    parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_method_retrieve(self, client: Notiflows) -> None:
        user = client.users.retrieve(
            "user_12345",
        )
        assert_matches_type(User, user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_raw_response_retrieve(self, client: Notiflows) -> None:
        response = client.users.with_raw_response.retrieve(
            "user_12345",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        user = response.parse()
        assert_matches_type(User, user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_streaming_response_retrieve(self, client: Notiflows) -> None:
        with client.users.with_streaming_response.retrieve(
            "user_12345",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            user = response.parse()
            assert_matches_type(User, user, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_path_params_retrieve(self, client: Notiflows) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `user_external_id` but received ''"):
            client.users.with_raw_response.retrieve(
                "",
            )

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_method_list(self, client: Notiflows) -> None:
        user = client.users.list()
        assert_matches_type(SyncCursorPage[User], user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_method_list_with_all_params(self, client: Notiflows) -> None:
        user = client.users.list(
            after="after",
            before="before",
            created_after="created_after",
            created_before="created_before",
            limit=0,
        )
        assert_matches_type(SyncCursorPage[User], user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_raw_response_list(self, client: Notiflows) -> None:
        response = client.users.with_raw_response.list()

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        user = response.parse()
        assert_matches_type(SyncCursorPage[User], user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_streaming_response_list(self, client: Notiflows) -> None:
        with client.users.with_streaming_response.list() as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            user = response.parse()
            assert_matches_type(SyncCursorPage[User], user, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_method_delete(self, client: Notiflows) -> None:
        user = client.users.delete(
            "user_12345",
        )
        assert user is None

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_raw_response_delete(self, client: Notiflows) -> None:
        response = client.users.with_raw_response.delete(
            "user_12345",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        user = response.parse()
        assert user is None

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_streaming_response_delete(self, client: Notiflows) -> None:
        with client.users.with_streaming_response.delete(
            "user_12345",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            user = response.parse()
            assert user is None

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_path_params_delete(self, client: Notiflows) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `user_external_id` but received ''"):
            client.users.with_raw_response.delete(
                "",
            )

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_method_upsert(self, client: Notiflows) -> None:
        user = client.users.upsert(
            user_external_id="user_12345",
        )
        assert_matches_type(User, user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_method_upsert_with_all_params(self, client: Notiflows) -> None:
        user = client.users.upsert(
            user_external_id="user_12345",
            avatar="https://example.com/avatars/jane.jpg",
            channel_settings=[
                {
                    "channel_id": "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
                    "settings": {},
                }
            ],
            custom_fields={
                "company": "Acme Inc",
                "plan": "premium",
            },
            email="jane.doe@example.com",
            first_name="Jane",
            last_name="Doe",
            locale="en-US",
            phone="+15551234567",
            preferences={
                "channel_types": {"sms": False},
                "notiflows": {
                    "order-updates": {
                        "channel_types": {"email": False},
                        "enabled": True,
                    }
                },
            },
            timezone="America/New_York",
        )
        assert_matches_type(User, user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_raw_response_upsert(self, client: Notiflows) -> None:
        response = client.users.with_raw_response.upsert(
            user_external_id="user_12345",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        user = response.parse()
        assert_matches_type(User, user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_streaming_response_upsert(self, client: Notiflows) -> None:
        with client.users.with_streaming_response.upsert(
            user_external_id="user_12345",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            user = response.parse()
            assert_matches_type(User, user, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    def test_path_params_upsert(self, client: Notiflows) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `user_external_id` but received ''"):
            client.users.with_raw_response.upsert(
                user_external_id="",
            )


class TestAsyncUsers:
    parametrize = pytest.mark.parametrize(
        "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
    )

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_method_retrieve(self, async_client: AsyncNotiflows) -> None:
        user = await async_client.users.retrieve(
            "user_12345",
        )
        assert_matches_type(User, user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_raw_response_retrieve(self, async_client: AsyncNotiflows) -> None:
        response = await async_client.users.with_raw_response.retrieve(
            "user_12345",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        user = await response.parse()
        assert_matches_type(User, user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_streaming_response_retrieve(self, async_client: AsyncNotiflows) -> None:
        async with async_client.users.with_streaming_response.retrieve(
            "user_12345",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            user = await response.parse()
            assert_matches_type(User, user, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_path_params_retrieve(self, async_client: AsyncNotiflows) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `user_external_id` but received ''"):
            await async_client.users.with_raw_response.retrieve(
                "",
            )

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_method_list(self, async_client: AsyncNotiflows) -> None:
        user = await async_client.users.list()
        assert_matches_type(AsyncCursorPage[User], user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_method_list_with_all_params(self, async_client: AsyncNotiflows) -> None:
        user = await async_client.users.list(
            after="after",
            before="before",
            created_after="created_after",
            created_before="created_before",
            limit=0,
        )
        assert_matches_type(AsyncCursorPage[User], user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_raw_response_list(self, async_client: AsyncNotiflows) -> None:
        response = await async_client.users.with_raw_response.list()

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        user = await response.parse()
        assert_matches_type(AsyncCursorPage[User], user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_streaming_response_list(self, async_client: AsyncNotiflows) -> None:
        async with async_client.users.with_streaming_response.list() as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            user = await response.parse()
            assert_matches_type(AsyncCursorPage[User], user, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_method_delete(self, async_client: AsyncNotiflows) -> None:
        user = await async_client.users.delete(
            "user_12345",
        )
        assert user is None

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_raw_response_delete(self, async_client: AsyncNotiflows) -> None:
        response = await async_client.users.with_raw_response.delete(
            "user_12345",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        user = await response.parse()
        assert user is None

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_streaming_response_delete(self, async_client: AsyncNotiflows) -> None:
        async with async_client.users.with_streaming_response.delete(
            "user_12345",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            user = await response.parse()
            assert user is None

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_path_params_delete(self, async_client: AsyncNotiflows) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `user_external_id` but received ''"):
            await async_client.users.with_raw_response.delete(
                "",
            )

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_method_upsert(self, async_client: AsyncNotiflows) -> None:
        user = await async_client.users.upsert(
            user_external_id="user_12345",
        )
        assert_matches_type(User, user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_method_upsert_with_all_params(self, async_client: AsyncNotiflows) -> None:
        user = await async_client.users.upsert(
            user_external_id="user_12345",
            avatar="https://example.com/avatars/jane.jpg",
            channel_settings=[
                {
                    "channel_id": "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
                    "settings": {},
                }
            ],
            custom_fields={
                "company": "Acme Inc",
                "plan": "premium",
            },
            email="jane.doe@example.com",
            first_name="Jane",
            last_name="Doe",
            locale="en-US",
            phone="+15551234567",
            preferences={
                "channel_types": {"sms": False},
                "notiflows": {
                    "order-updates": {
                        "channel_types": {"email": False},
                        "enabled": True,
                    }
                },
            },
            timezone="America/New_York",
        )
        assert_matches_type(User, user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_raw_response_upsert(self, async_client: AsyncNotiflows) -> None:
        response = await async_client.users.with_raw_response.upsert(
            user_external_id="user_12345",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        user = await response.parse()
        assert_matches_type(User, user, path=["response"])

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_streaming_response_upsert(self, async_client: AsyncNotiflows) -> None:
        async with async_client.users.with_streaming_response.upsert(
            user_external_id="user_12345",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            user = await response.parse()
            assert_matches_type(User, user, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Prism doesn't support callbacks yet")
    @parametrize
    async def test_path_params_upsert(self, async_client: AsyncNotiflows) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `user_external_id` but received ''"):
            await async_client.users.with_raw_response.upsert(
                user_external_id="",
            )
