# !!!
# WARNING: This file is autogenerated
# Only modify code within MANUAL() sections
# or your changes may be overwritten later!
# !!!

from __future__ import annotations

from typing import Any, Dict, Optional, Union

from stytch.b2b.models.rbac import OrgPolicy
from stytch.b2b.models.rbac_organizations import (
    GetOrgPolicyResponse,
    SetOrgPolicyResponse,
)
from stytch.core.api_base import ApiBase
from stytch.core.http.client import AsyncClient, SyncClient


class Organizations:
    def __init__(
        self, api_base: ApiBase, sync_client: SyncClient, async_client: AsyncClient
    ) -> None:
        self.api_base = api_base
        self.sync_client = sync_client
        self.async_client = async_client

    def get_org_policy(
        self,
        organization_id: str,
    ) -> GetOrgPolicyResponse:
        """Get the active RBAC Policy for a specific Organization within your Stytch Project. An Organization RBAC Policy contains the roles that have been defined specifically for that organization, allowing for organization-specific permissioning models.

        This endpoint returns the organization-scoped roles that supplement the project-level RBAC policy. Organization policies allow you to define custom roles that are specific to individual organizations within your project.

        When using the backend SDKs, the RBAC Policy will be cached to allow for local evaluations, eliminating the need for an extra request to Stytch. The policy will be refreshed if an authorization check is requested and the RBAC policy was last updated more than 5 minutes ago.

        Organization-specific roles can be created and managed through this API endpoint, providing fine-grained control over permissions at the organization level.

        Check out the [RBAC overview](https://stytch.com/docs/b2b/guides/rbac/overview) to learn more about Stytch's RBAC permissioning model and organization-scoped policies.

        Fields:
          - organization_id: Globally unique UUID that identifies a specific Organization. The `organization_id` is critical to perform operations on an Organization, so be sure to preserve this value. You may also use the organization_slug or organization_external_id here as a convenience.
        """  # noqa
        headers: Dict[str, str] = {}
        data: Dict[str, Any] = {
            "organization_id": organization_id,
        }

        url = self.api_base.url_for(
            "/v1/b2b/rbac/organizations/{organization_id}", data
        )
        res = self.sync_client.get(url, data, headers)
        return GetOrgPolicyResponse.from_json(res.response.status_code, res.json)

    async def get_org_policy_async(
        self,
        organization_id: str,
    ) -> GetOrgPolicyResponse:
        """Get the active RBAC Policy for a specific Organization within your Stytch Project. An Organization RBAC Policy contains the roles that have been defined specifically for that organization, allowing for organization-specific permissioning models.

        This endpoint returns the organization-scoped roles that supplement the project-level RBAC policy. Organization policies allow you to define custom roles that are specific to individual organizations within your project.

        When using the backend SDKs, the RBAC Policy will be cached to allow for local evaluations, eliminating the need for an extra request to Stytch. The policy will be refreshed if an authorization check is requested and the RBAC policy was last updated more than 5 minutes ago.

        Organization-specific roles can be created and managed through this API endpoint, providing fine-grained control over permissions at the organization level.

        Check out the [RBAC overview](https://stytch.com/docs/b2b/guides/rbac/overview) to learn more about Stytch's RBAC permissioning model and organization-scoped policies.

        Fields:
          - organization_id: Globally unique UUID that identifies a specific Organization. The `organization_id` is critical to perform operations on an Organization, so be sure to preserve this value. You may also use the organization_slug or organization_external_id here as a convenience.
        """  # noqa
        headers: Dict[str, str] = {}
        data: Dict[str, Any] = {
            "organization_id": organization_id,
        }

        url = self.api_base.url_for(
            "/v1/b2b/rbac/organizations/{organization_id}", data
        )
        res = await self.async_client.get(url, data, headers)
        return GetOrgPolicyResponse.from_json(res.response.status, res.json)

    def set_org_policy(
        self,
        organization_id: str,
        org_policy: Optional[Union[OrgPolicy, Dict[str, Any]]] = None,
    ) -> SetOrgPolicyResponse:
        headers: Dict[str, str] = {}
        data: Dict[str, Any] = {
            "organization_id": organization_id,
        }
        if org_policy is not None:
            data["org_policy"] = (
                org_policy if isinstance(org_policy, dict) else org_policy.dict()
            )

        url = self.api_base.url_for(
            "/v1/b2b/rbac/organizations/{organization_id}", data
        )
        res = self.sync_client.put(url, data, headers)
        return SetOrgPolicyResponse.from_json(res.response.status_code, res.json)

    async def set_org_policy_async(
        self,
        organization_id: str,
        org_policy: Optional[OrgPolicy] = None,
    ) -> SetOrgPolicyResponse:
        headers: Dict[str, str] = {}
        data: Dict[str, Any] = {
            "organization_id": organization_id,
        }
        if org_policy is not None:
            data["org_policy"] = (
                org_policy if isinstance(org_policy, dict) else org_policy.dict()
            )

        url = self.api_base.url_for(
            "/v1/b2b/rbac/organizations/{organization_id}", data
        )
        res = await self.async_client.put(url, data, headers)
        return SetOrgPolicyResponse.from_json(res.response.status, res.json)
