# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from typing import TYPE_CHECKING, Optional, Union

if TYPE_CHECKING:
    from .._client import AsyncWorkOSClient, WorkOSClient

from .._types import RequestOptions, enum_value
from workos.common.models.feature_flag import FeatureFlag
from workos.common.models.flag import Flag
from workos.common.models.pagination_order import PaginationOrder
from .._pagination import AsyncPage, SyncPage


class FeatureFlags:
    """Feature Flags API resources."""

    def __init__(self, client: "WorkOSClient") -> None:
        self._client = client

    def list_feature_flags(
        self,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        request_options: Optional[RequestOptions] = None,
    ) -> SyncPage[Flag]:
        """List feature flags

        Get a list of all of your existing feature flags matching the criteria specified.

        Args:
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            order: Order the results by the creation time. Defaults to `desc`.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            SyncPage[Flag]

        Raises:
            BadRequestError: If the request is malformed (400).
            NotFoundError: If the resource is not found (404).
            UnprocessableEntityError: If the request data is unprocessable (422).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
            }.items()
            if v is not None
        }
        return self._client.request_page(
            method="get",
            path=("feature-flags",),
            model=Flag,
            params=params,
            request_options=request_options,
        )

    def get_feature_flag(
        self,
        slug: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> Flag:
        """Get a feature flag

        Get the details of an existing feature flag by its slug.

        Args:
            slug: A unique key to reference the Feature Flag.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            Flag

        Raises:
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return self._client.request(
            method="get",
            path=("feature-flags", str(slug)),
            model=Flag,
            request_options=request_options,
        )

    def disable_feature_flag(
        self,
        slug: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> FeatureFlag:
        """Disable a feature flag

        Disables a feature flag in the current environment.

        Args:
            slug: A unique key to reference the Feature Flag.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            FeatureFlag

        Raises:
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return self._client.request(
            method="put",
            path=("feature-flags", str(slug), "disable"),
            model=FeatureFlag,
            request_options=request_options,
        )

    def enable_feature_flag(
        self,
        slug: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> FeatureFlag:
        """Enable a feature flag

        Enables a feature flag in the current environment.

        Args:
            slug: A unique key to reference the Feature Flag.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            FeatureFlag

        Raises:
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return self._client.request(
            method="put",
            path=("feature-flags", str(slug), "enable"),
            model=FeatureFlag,
            request_options=request_options,
        )

    def add_flag_target(
        self,
        resource_id: str,
        slug: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> None:
        """Add a feature flag target

        Enables a feature flag for a specific target in the current environment. Currently, supported targets include users and organizations.

        Args:
            resource_id: The resource ID in format "user_<id>" or "org_<id>".
            slug: The unique slug identifier of the feature flag.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Raises:
            BadRequestError: If the request is malformed (400).
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        self._client.request(
            method="post",
            path=("feature-flags", str(slug), "targets", str(resource_id)),
            request_options=request_options,
        )

    def remove_flag_target(
        self,
        resource_id: str,
        slug: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> None:
        """Remove a feature flag target

        Removes a target from the feature flag's target list in the current environment. Currently, supported targets include users and organizations.

        Args:
            resource_id: The resource ID in format "user_<id>" or "org_<id>".
            slug: The unique slug identifier of the feature flag.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Raises:
            BadRequestError: If the request is malformed (400).
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        self._client.request(
            method="delete",
            path=("feature-flags", str(slug), "targets", str(resource_id)),
            request_options=request_options,
        )

    def list_organization_feature_flags(
        self,
        organization_id: str,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        request_options: Optional[RequestOptions] = None,
    ) -> SyncPage[Flag]:
        """List enabled feature flags for an organization

        Get a list of all enabled feature flags for an organization.

        Args:
            organization_id: Unique identifier of the Organization.
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            order: Order the results by the creation time. Defaults to `desc`.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            SyncPage[Flag]

        Raises:
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
            }.items()
            if v is not None
        }
        return self._client.request_page(
            method="get",
            path=("organizations", str(organization_id), "feature-flags"),
            model=Flag,
            params=params,
            request_options=request_options,
        )

    def list_user_feature_flags(
        self,
        user_id: str,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        request_options: Optional[RequestOptions] = None,
    ) -> SyncPage[Flag]:
        """List enabled feature flags for a user

        Get a list of all enabled feature flags for the provided user. This includes feature flags enabled specifically for the user as well as any organizations that the user is a member of.

        Args:
            user_id: The ID of the user.
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            order: Order the results by the creation time. Defaults to `desc`.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            SyncPage[Flag]

        Raises:
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
            }.items()
            if v is not None
        }
        return self._client.request_page(
            method="get",
            path=("user_management", "users", str(user_id), "feature-flags"),
            model=Flag,
            params=params,
            request_options=request_options,
        )


class AsyncFeatureFlags:
    """Feature Flags API resources (async)."""

    def __init__(self, client: "AsyncWorkOSClient") -> None:
        self._client = client

    async def list_feature_flags(
        self,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        request_options: Optional[RequestOptions] = None,
    ) -> AsyncPage[Flag]:
        """List feature flags

        Get a list of all of your existing feature flags matching the criteria specified.

        Args:
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            order: Order the results by the creation time. Defaults to `desc`.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            AsyncPage[Flag]

        Raises:
            BadRequestError: If the request is malformed (400).
            NotFoundError: If the resource is not found (404).
            UnprocessableEntityError: If the request data is unprocessable (422).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
            }.items()
            if v is not None
        }
        return await self._client.request_page(
            method="get",
            path=("feature-flags",),
            model=Flag,
            params=params,
            request_options=request_options,
        )

    async def get_feature_flag(
        self,
        slug: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> Flag:
        """Get a feature flag

        Get the details of an existing feature flag by its slug.

        Args:
            slug: A unique key to reference the Feature Flag.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            Flag

        Raises:
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return await self._client.request(
            method="get",
            path=("feature-flags", str(slug)),
            model=Flag,
            request_options=request_options,
        )

    async def disable_feature_flag(
        self,
        slug: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> FeatureFlag:
        """Disable a feature flag

        Disables a feature flag in the current environment.

        Args:
            slug: A unique key to reference the Feature Flag.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            FeatureFlag

        Raises:
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return await self._client.request(
            method="put",
            path=("feature-flags", str(slug), "disable"),
            model=FeatureFlag,
            request_options=request_options,
        )

    async def enable_feature_flag(
        self,
        slug: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> FeatureFlag:
        """Enable a feature flag

        Enables a feature flag in the current environment.

        Args:
            slug: A unique key to reference the Feature Flag.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            FeatureFlag

        Raises:
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return await self._client.request(
            method="put",
            path=("feature-flags", str(slug), "enable"),
            model=FeatureFlag,
            request_options=request_options,
        )

    async def add_flag_target(
        self,
        resource_id: str,
        slug: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> None:
        """Add a feature flag target

        Enables a feature flag for a specific target in the current environment. Currently, supported targets include users and organizations.

        Args:
            resource_id: The resource ID in format "user_<id>" or "org_<id>".
            slug: The unique slug identifier of the feature flag.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Raises:
            BadRequestError: If the request is malformed (400).
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        await self._client.request(
            method="post",
            path=("feature-flags", str(slug), "targets", str(resource_id)),
            request_options=request_options,
        )

    async def remove_flag_target(
        self,
        resource_id: str,
        slug: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> None:
        """Remove a feature flag target

        Removes a target from the feature flag's target list in the current environment. Currently, supported targets include users and organizations.

        Args:
            resource_id: The resource ID in format "user_<id>" or "org_<id>".
            slug: The unique slug identifier of the feature flag.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Raises:
            BadRequestError: If the request is malformed (400).
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        await self._client.request(
            method="delete",
            path=("feature-flags", str(slug), "targets", str(resource_id)),
            request_options=request_options,
        )

    async def list_organization_feature_flags(
        self,
        organization_id: str,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        request_options: Optional[RequestOptions] = None,
    ) -> AsyncPage[Flag]:
        """List enabled feature flags for an organization

        Get a list of all enabled feature flags for an organization.

        Args:
            organization_id: Unique identifier of the Organization.
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            order: Order the results by the creation time. Defaults to `desc`.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            AsyncPage[Flag]

        Raises:
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
            }.items()
            if v is not None
        }
        return await self._client.request_page(
            method="get",
            path=("organizations", str(organization_id), "feature-flags"),
            model=Flag,
            params=params,
            request_options=request_options,
        )

    async def list_user_feature_flags(
        self,
        user_id: str,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        request_options: Optional[RequestOptions] = None,
    ) -> AsyncPage[Flag]:
        """List enabled feature flags for a user

        Get a list of all enabled feature flags for the provided user. This includes feature flags enabled specifically for the user as well as any organizations that the user is a member of.

        Args:
            user_id: The ID of the user.
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            order: Order the results by the creation time. Defaults to `desc`.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            AsyncPage[Flag]

        Raises:
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
            }.items()
            if v is not None
        }
        return await self._client.request_page(
            method="get",
            path=("user_management", "users", str(user_id), "feature-flags"),
            model=Flag,
            params=params,
            request_options=request_options,
        )
