# This file was auto-generated by Fern from our API Definition.

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.apply_mode import ApplyMode
from ..types.attachment_create_inline import AttachmentCreateInline
from ..types.entity_type import EntityType
from ..types.rule_category import RuleCategory
from ..types.rule_detail_response import RuleDetailResponse
from ..types.rule_list_paginated_response import RuleListPaginatedResponse
from ..types.rule_scope import RuleScope
from ..types.rule_scope_api import RuleScopeApi
from ..types.rule_type import RuleType
from .raw_client import AsyncRawRulesClient, RawRulesClient

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class RulesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawRulesClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawRulesClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawRulesClient
        """
        return self._raw_client

    def list_rules(
        self,
        org_id: str,
        *,
        skip: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        search: typing.Optional[str] = None,
        category: typing.Optional[RuleCategory] = None,
        rule_type: typing.Optional[RuleType] = None,
        scope: typing.Optional[RuleScope] = None,
        is_active: typing.Optional[bool] = None,
        is_public: typing.Optional[bool] = None,
        entity_type: typing.Optional[EntityType] = None,
        entity_id: typing.Optional[str] = None,
        attached_only: typing.Optional[bool] = None,
        include_attachments: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RuleListPaginatedResponse:
        """
        List all rules in an organization with pagination, filtering, and search.

        Parameters
        ----------
        org_id : str

        skip : typing.Optional[int]
            Number of records to skip for pagination

        limit : typing.Optional[int]
            Maximum number of records to return (1-100)

        search : typing.Optional[str]
            Search query to match against name, description, and content

        category : typing.Optional[RuleCategory]
            Filter by category (safety, professional, creative, technical, custom)

        rule_type : typing.Optional[RuleType]
            Filter by rule type (behavior, guardrails, formatting, context, content_policy, constraint)

        scope : typing.Optional[RuleScope]
            Filter by scope (global, organization, model, assistant, user, vector_store)

        is_active : typing.Optional[bool]
            Filter by active status (true/false)

        is_public : typing.Optional[bool]
            Filter by public status (true/false)

        entity_type : typing.Optional[EntityType]
            Filter by entity type (assistant, vector_store, user, model, organization)

        entity_id : typing.Optional[str]
            Filter by specific entity ID (e.g., asst_123, vs_456). Requires entity_type if provided.

        attached_only : typing.Optional[bool]
            Filter by attachment status - true: only attached rules, false: only unattached rules

        include_attachments : typing.Optional[bool]
            Include list of attached entities with priority in response

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        RuleListPaginatedResponse
            Rules retrieved successfully

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.rules.list_rules(
            org_id="org_id",
        )
        """
        _response = self._raw_client.list_rules(
            org_id,
            skip=skip,
            limit=limit,
            search=search,
            category=category,
            rule_type=rule_type,
            scope=scope,
            is_active=is_active,
            is_public=is_public,
            entity_type=entity_type,
            entity_id=entity_id,
            attached_only=attached_only,
            include_attachments=include_attachments,
            request_options=request_options,
        )
        return _response.data

    def create_rule(
        self,
        org_id: str,
        *,
        name: str,
        content: str,
        description: typing.Optional[str] = OMIT,
        category: typing.Optional[RuleCategory] = OMIT,
        rule_type: typing.Optional[RuleType] = OMIT,
        scope: typing.Optional[RuleScopeApi] = OMIT,
        apply_mode: typing.Optional[ApplyMode] = OMIT,
        is_public: typing.Optional[bool] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        attachments: typing.Optional[typing.Sequence[AttachmentCreateInline]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RuleDetailResponse:
        """
        Create a new rule within an organization. The creator automatically receives owner access.

        Parameters
        ----------
        org_id : str

        name : str
            Rule name

        content : str
            Rule content (max 50KB)

        description : typing.Optional[str]
            Rule description

        category : typing.Optional[RuleCategory]
            Rule category

        rule_type : typing.Optional[RuleType]
            Rule type

        scope : typing.Optional[RuleScopeApi]
            Rule scope (global scope restricted - admin only)

        apply_mode : typing.Optional[ApplyMode]
            Rule apply mode

        is_public : typing.Optional[bool]
            Whether rule is public

        is_active : typing.Optional[bool]
            Whether rule is active

        attachments : typing.Optional[typing.Sequence[AttachmentCreateInline]]
            Optional list of entities to attach to this rule upon creation

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        RuleDetailResponse
            Rule created successfully

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.rules.create_rule(
            org_id="org_id",
            name="Professional Communication Rule",
            content="Always maintain a professional and courteous tone in all communications. Use formal language and avoid slang.",
        )
        """
        _response = self._raw_client.create_rule(
            org_id,
            name=name,
            content=content,
            description=description,
            category=category,
            rule_type=rule_type,
            scope=scope,
            apply_mode=apply_mode,
            is_public=is_public,
            is_active=is_active,
            attachments=attachments,
            request_options=request_options,
        )
        return _response.data

    def get_rule(
        self,
        org_id: str,
        rule_id: str,
        *,
        include_attachments: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RuleDetailResponse:
        """
        Get complete details of a specific rule including full content.

        Parameters
        ----------
        org_id : str

        rule_id : str

        include_attachments : typing.Optional[bool]
            Include list of attached entities in response

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        RuleDetailResponse
            Rule details retrieved successfully

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.rules.get_rule(
            org_id="org_id",
            rule_id="rule_id",
        )
        """
        _response = self._raw_client.get_rule(
            org_id, rule_id, include_attachments=include_attachments, request_options=request_options
        )
        return _response.data

    def get_rule_global(
        self,
        rule_id: str,
        *,
        include_attachments: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RuleDetailResponse:
        """
        Get rule details by ID without requiring organization ID. Rule IDs are globally unique.

        Parameters
        ----------
        rule_id : str

        include_attachments : typing.Optional[bool]
            Include list of attached entities in response

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        RuleDetailResponse
            Rule details retrieved successfully

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.rules.get_rule_global(
            rule_id="rule_id",
        )
        """
        _response = self._raw_client.get_rule_global(
            rule_id, include_attachments=include_attachments, request_options=request_options
        )
        return _response.data


class AsyncRulesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawRulesClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawRulesClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawRulesClient
        """
        return self._raw_client

    async def list_rules(
        self,
        org_id: str,
        *,
        skip: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        search: typing.Optional[str] = None,
        category: typing.Optional[RuleCategory] = None,
        rule_type: typing.Optional[RuleType] = None,
        scope: typing.Optional[RuleScope] = None,
        is_active: typing.Optional[bool] = None,
        is_public: typing.Optional[bool] = None,
        entity_type: typing.Optional[EntityType] = None,
        entity_id: typing.Optional[str] = None,
        attached_only: typing.Optional[bool] = None,
        include_attachments: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RuleListPaginatedResponse:
        """
        List all rules in an organization with pagination, filtering, and search.

        Parameters
        ----------
        org_id : str

        skip : typing.Optional[int]
            Number of records to skip for pagination

        limit : typing.Optional[int]
            Maximum number of records to return (1-100)

        search : typing.Optional[str]
            Search query to match against name, description, and content

        category : typing.Optional[RuleCategory]
            Filter by category (safety, professional, creative, technical, custom)

        rule_type : typing.Optional[RuleType]
            Filter by rule type (behavior, guardrails, formatting, context, content_policy, constraint)

        scope : typing.Optional[RuleScope]
            Filter by scope (global, organization, model, assistant, user, vector_store)

        is_active : typing.Optional[bool]
            Filter by active status (true/false)

        is_public : typing.Optional[bool]
            Filter by public status (true/false)

        entity_type : typing.Optional[EntityType]
            Filter by entity type (assistant, vector_store, user, model, organization)

        entity_id : typing.Optional[str]
            Filter by specific entity ID (e.g., asst_123, vs_456). Requires entity_type if provided.

        attached_only : typing.Optional[bool]
            Filter by attachment status - true: only attached rules, false: only unattached rules

        include_attachments : typing.Optional[bool]
            Include list of attached entities with priority in response

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        RuleListPaginatedResponse
            Rules retrieved successfully

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.rules.list_rules(
                org_id="org_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list_rules(
            org_id,
            skip=skip,
            limit=limit,
            search=search,
            category=category,
            rule_type=rule_type,
            scope=scope,
            is_active=is_active,
            is_public=is_public,
            entity_type=entity_type,
            entity_id=entity_id,
            attached_only=attached_only,
            include_attachments=include_attachments,
            request_options=request_options,
        )
        return _response.data

    async def create_rule(
        self,
        org_id: str,
        *,
        name: str,
        content: str,
        description: typing.Optional[str] = OMIT,
        category: typing.Optional[RuleCategory] = OMIT,
        rule_type: typing.Optional[RuleType] = OMIT,
        scope: typing.Optional[RuleScopeApi] = OMIT,
        apply_mode: typing.Optional[ApplyMode] = OMIT,
        is_public: typing.Optional[bool] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        attachments: typing.Optional[typing.Sequence[AttachmentCreateInline]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RuleDetailResponse:
        """
        Create a new rule within an organization. The creator automatically receives owner access.

        Parameters
        ----------
        org_id : str

        name : str
            Rule name

        content : str
            Rule content (max 50KB)

        description : typing.Optional[str]
            Rule description

        category : typing.Optional[RuleCategory]
            Rule category

        rule_type : typing.Optional[RuleType]
            Rule type

        scope : typing.Optional[RuleScopeApi]
            Rule scope (global scope restricted - admin only)

        apply_mode : typing.Optional[ApplyMode]
            Rule apply mode

        is_public : typing.Optional[bool]
            Whether rule is public

        is_active : typing.Optional[bool]
            Whether rule is active

        attachments : typing.Optional[typing.Sequence[AttachmentCreateInline]]
            Optional list of entities to attach to this rule upon creation

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        RuleDetailResponse
            Rule created successfully

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.rules.create_rule(
                org_id="org_id",
                name="Professional Communication Rule",
                content="Always maintain a professional and courteous tone in all communications. Use formal language and avoid slang.",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create_rule(
            org_id,
            name=name,
            content=content,
            description=description,
            category=category,
            rule_type=rule_type,
            scope=scope,
            apply_mode=apply_mode,
            is_public=is_public,
            is_active=is_active,
            attachments=attachments,
            request_options=request_options,
        )
        return _response.data

    async def get_rule(
        self,
        org_id: str,
        rule_id: str,
        *,
        include_attachments: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RuleDetailResponse:
        """
        Get complete details of a specific rule including full content.

        Parameters
        ----------
        org_id : str

        rule_id : str

        include_attachments : typing.Optional[bool]
            Include list of attached entities in response

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        RuleDetailResponse
            Rule details retrieved successfully

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.rules.get_rule(
                org_id="org_id",
                rule_id="rule_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_rule(
            org_id, rule_id, include_attachments=include_attachments, request_options=request_options
        )
        return _response.data

    async def get_rule_global(
        self,
        rule_id: str,
        *,
        include_attachments: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RuleDetailResponse:
        """
        Get rule details by ID without requiring organization ID. Rule IDs are globally unique.

        Parameters
        ----------
        rule_id : str

        include_attachments : typing.Optional[bool]
            Include list of attached entities in response

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        RuleDetailResponse
            Rule details retrieved successfully

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.rules.get_rule_global(
                rule_id="rule_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_rule_global(
            rule_id, include_attachments=include_attachments, request_options=request_options
        )
        return _response.data
