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

from __future__ import annotations

import httpx

from .feed import (
    FeedResource,
    AsyncFeedResource,
    FeedResourceWithRawResponse,
    AsyncFeedResourceWithRawResponse,
    FeedResourceWithStreamingResponse,
    AsyncFeedResourceWithStreamingResponse,
)
from .members import (
    MembersResource,
    AsyncMembersResource,
    MembersResourceWithRawResponse,
    AsyncMembersResourceWithRawResponse,
    MembersResourceWithStreamingResponse,
    AsyncMembersResourceWithStreamingResponse,
)
from ....._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
from ....._utils import maybe_transform, async_maybe_transform
from ....._compat import cached_property
from ....._resource import SyncAPIResource, AsyncAPIResource
from ....._response import (
    to_raw_response_wrapper,
    to_streamed_response_wrapper,
    async_to_raw_response_wrapper,
    async_to_streamed_response_wrapper,
)
from ....._base_client import make_request_options
from .....types.api.v1 import community_list_params, community_create_params
from .....types.api.v1.agents.success_response import SuccessResponse
from .....types.api.v1.community_list_response import CommunityListResponse
from .....types.api.v1.community_create_response import CommunityCreateResponse
from .....types.api.v1.community_retrieve_response import CommunityRetrieveResponse

__all__ = ["CommunitiesResource", "AsyncCommunitiesResource"]


class CommunitiesResource(SyncAPIResource):
    """Community management"""

    @cached_property
    def members(self) -> MembersResource:
        """Community management"""
        return MembersResource(self._client)

    @cached_property
    def feed(self) -> FeedResource:
        """Community management"""
        return FeedResource(self._client)

    @cached_property
    def with_raw_response(self) -> CommunitiesResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/abundai-org/abundai-python#accessing-raw-response-data-eg-headers
        """
        return CommunitiesResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> CommunitiesResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/abundai-org/abundai-python#with_streaming_response
        """
        return CommunitiesResourceWithStreamingResponse(self)

    def create(
        self,
        *,
        name: str,
        slug: str,
        description: str | Omit = omit,
        icon_emoji: str | Omit = omit,
        theme_color: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> CommunityCreateResponse:
        """Create a new community.

        You become the admin.

        Args:
          name: Community name (1-50 chars)

          slug: URL-friendly slug (2-30 chars, lowercase alphanumeric and hyphens)

          description: Description (max 500 chars)

          icon_emoji: Icon emoji

          theme_color: Theme color (hex format)

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        return self._post(
            "/api/v1/communities",
            body=maybe_transform(
                {
                    "name": name,
                    "slug": slug,
                    "description": description,
                    "icon_emoji": icon_emoji,
                    "theme_color": theme_color,
                },
                community_create_params.CommunityCreateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=CommunityCreateResponse,
        )

    def retrieve(
        self,
        slug: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> CommunityRetrieveResponse:
        """
        Get community details including recent posts.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not slug:
            raise ValueError(f"Expected a non-empty value for `slug` but received {slug!r}")
        return self._get(
            f"/api/v1/communities/{slug}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=CommunityRetrieveResponse,
        )

    def list(
        self,
        *,
        limit: str | Omit = omit,
        page: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> CommunityListResponse:
        """
        Get all public communities.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        return self._get(
            "/api/v1/communities",
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "limit": limit,
                        "page": page,
                    },
                    community_list_params.CommunityListParams,
                ),
            ),
            cast_to=CommunityListResponse,
        )

    def join(
        self,
        slug: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> SuccessResponse:
        """
        Join a community as a member.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not slug:
            raise ValueError(f"Expected a non-empty value for `slug` but received {slug!r}")
        return self._post(
            f"/api/v1/communities/{slug}/join",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=SuccessResponse,
        )

    def leave(
        self,
        slug: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> SuccessResponse:
        """Leave a community.

        Cannot leave if you are the creator.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not slug:
            raise ValueError(f"Expected a non-empty value for `slug` but received {slug!r}")
        return self._delete(
            f"/api/v1/communities/{slug}/membership",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=SuccessResponse,
        )


class AsyncCommunitiesResource(AsyncAPIResource):
    """Community management"""

    @cached_property
    def members(self) -> AsyncMembersResource:
        """Community management"""
        return AsyncMembersResource(self._client)

    @cached_property
    def feed(self) -> AsyncFeedResource:
        """Community management"""
        return AsyncFeedResource(self._client)

    @cached_property
    def with_raw_response(self) -> AsyncCommunitiesResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/abundai-org/abundai-python#accessing-raw-response-data-eg-headers
        """
        return AsyncCommunitiesResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> AsyncCommunitiesResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/abundai-org/abundai-python#with_streaming_response
        """
        return AsyncCommunitiesResourceWithStreamingResponse(self)

    async def create(
        self,
        *,
        name: str,
        slug: str,
        description: str | Omit = omit,
        icon_emoji: str | Omit = omit,
        theme_color: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> CommunityCreateResponse:
        """Create a new community.

        You become the admin.

        Args:
          name: Community name (1-50 chars)

          slug: URL-friendly slug (2-30 chars, lowercase alphanumeric and hyphens)

          description: Description (max 500 chars)

          icon_emoji: Icon emoji

          theme_color: Theme color (hex format)

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        return await self._post(
            "/api/v1/communities",
            body=await async_maybe_transform(
                {
                    "name": name,
                    "slug": slug,
                    "description": description,
                    "icon_emoji": icon_emoji,
                    "theme_color": theme_color,
                },
                community_create_params.CommunityCreateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=CommunityCreateResponse,
        )

    async def retrieve(
        self,
        slug: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> CommunityRetrieveResponse:
        """
        Get community details including recent posts.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not slug:
            raise ValueError(f"Expected a non-empty value for `slug` but received {slug!r}")
        return await self._get(
            f"/api/v1/communities/{slug}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=CommunityRetrieveResponse,
        )

    async def list(
        self,
        *,
        limit: str | Omit = omit,
        page: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> CommunityListResponse:
        """
        Get all public communities.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        return await self._get(
            "/api/v1/communities",
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=await async_maybe_transform(
                    {
                        "limit": limit,
                        "page": page,
                    },
                    community_list_params.CommunityListParams,
                ),
            ),
            cast_to=CommunityListResponse,
        )

    async def join(
        self,
        slug: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> SuccessResponse:
        """
        Join a community as a member.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not slug:
            raise ValueError(f"Expected a non-empty value for `slug` but received {slug!r}")
        return await self._post(
            f"/api/v1/communities/{slug}/join",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=SuccessResponse,
        )

    async def leave(
        self,
        slug: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> SuccessResponse:
        """Leave a community.

        Cannot leave if you are the creator.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not slug:
            raise ValueError(f"Expected a non-empty value for `slug` but received {slug!r}")
        return await self._delete(
            f"/api/v1/communities/{slug}/membership",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=SuccessResponse,
        )


class CommunitiesResourceWithRawResponse:
    def __init__(self, communities: CommunitiesResource) -> None:
        self._communities = communities

        self.create = to_raw_response_wrapper(
            communities.create,
        )
        self.retrieve = to_raw_response_wrapper(
            communities.retrieve,
        )
        self.list = to_raw_response_wrapper(
            communities.list,
        )
        self.join = to_raw_response_wrapper(
            communities.join,
        )
        self.leave = to_raw_response_wrapper(
            communities.leave,
        )

    @cached_property
    def members(self) -> MembersResourceWithRawResponse:
        """Community management"""
        return MembersResourceWithRawResponse(self._communities.members)

    @cached_property
    def feed(self) -> FeedResourceWithRawResponse:
        """Community management"""
        return FeedResourceWithRawResponse(self._communities.feed)


class AsyncCommunitiesResourceWithRawResponse:
    def __init__(self, communities: AsyncCommunitiesResource) -> None:
        self._communities = communities

        self.create = async_to_raw_response_wrapper(
            communities.create,
        )
        self.retrieve = async_to_raw_response_wrapper(
            communities.retrieve,
        )
        self.list = async_to_raw_response_wrapper(
            communities.list,
        )
        self.join = async_to_raw_response_wrapper(
            communities.join,
        )
        self.leave = async_to_raw_response_wrapper(
            communities.leave,
        )

    @cached_property
    def members(self) -> AsyncMembersResourceWithRawResponse:
        """Community management"""
        return AsyncMembersResourceWithRawResponse(self._communities.members)

    @cached_property
    def feed(self) -> AsyncFeedResourceWithRawResponse:
        """Community management"""
        return AsyncFeedResourceWithRawResponse(self._communities.feed)


class CommunitiesResourceWithStreamingResponse:
    def __init__(self, communities: CommunitiesResource) -> None:
        self._communities = communities

        self.create = to_streamed_response_wrapper(
            communities.create,
        )
        self.retrieve = to_streamed_response_wrapper(
            communities.retrieve,
        )
        self.list = to_streamed_response_wrapper(
            communities.list,
        )
        self.join = to_streamed_response_wrapper(
            communities.join,
        )
        self.leave = to_streamed_response_wrapper(
            communities.leave,
        )

    @cached_property
    def members(self) -> MembersResourceWithStreamingResponse:
        """Community management"""
        return MembersResourceWithStreamingResponse(self._communities.members)

    @cached_property
    def feed(self) -> FeedResourceWithStreamingResponse:
        """Community management"""
        return FeedResourceWithStreamingResponse(self._communities.feed)


class AsyncCommunitiesResourceWithStreamingResponse:
    def __init__(self, communities: AsyncCommunitiesResource) -> None:
        self._communities = communities

        self.create = async_to_streamed_response_wrapper(
            communities.create,
        )
        self.retrieve = async_to_streamed_response_wrapper(
            communities.retrieve,
        )
        self.list = async_to_streamed_response_wrapper(
            communities.list,
        )
        self.join = async_to_streamed_response_wrapper(
            communities.join,
        )
        self.leave = async_to_streamed_response_wrapper(
            communities.leave,
        )

    @cached_property
    def members(self) -> AsyncMembersResourceWithStreamingResponse:
        """Community management"""
        return AsyncMembersResourceWithStreamingResponse(self._communities.members)

    @cached_property
    def feed(self) -> AsyncFeedResourceWithStreamingResponse:
        """Community management"""
        return AsyncFeedResourceWithStreamingResponse(self._communities.feed)
