# 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 .raw_client import AsyncRawSummaryClient, RawSummaryClient
from .types.create_summary_request_fhir_resources import CreateSummaryRequestFhirResources
from .types.create_summary_request_mode import CreateSummaryRequestMode
from .types.create_summary_response import CreateSummaryResponse
from .types.create_summary_template_response import CreateSummaryTemplateResponse
from .types.summary_delete_template_response import SummaryDeleteTemplateResponse
from .types.summary_get_template_response import SummaryGetTemplateResponse
from .types.summary_list_templates_response import SummaryListTemplatesResponse
from .types.summary_update_template_response import SummaryUpdateTemplateResponse

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


class SummaryClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawSummaryClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawSummaryClient
        """
        return self._raw_client

    def list_templates(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> SummaryListTemplatesResponse:
        """
        Retrieves all summary templates for the authenticated user

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

        Returns
        -------
        SummaryListTemplatesResponse
            Templates retrieved successfully

        Examples
        --------
        from phenoml import phenoml

        client = phenoml(
            token="YOUR_TOKEN",
        )
        client.summary.list_templates()
        """
        _response = self._raw_client.list_templates(request_options=request_options)
        return _response.data

    def create_template(
        self,
        *,
        name: str,
        example_summary: str,
        target_resources: typing.Sequence[str],
        mode: str,
        description: typing.Optional[str] = OMIT,
        example_fhir_data: typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateSummaryTemplateResponse:
        """
        Creates a summary template from an example using LLM function calling

        Parameters
        ----------
        name : str
            Name of the template

        example_summary : str
            Example summary note to generate template from

        target_resources : typing.Sequence[str]
            List of target FHIR resources

        mode : str
            Template mode (stored with the template)

        description : typing.Optional[str]
            Description of the template

        example_fhir_data : typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]]
            Optional example FHIR data that corresponds to the example summary

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

        Returns
        -------
        CreateSummaryTemplateResponse
            Template created successfully

        Examples
        --------
        from phenoml import phenoml

        client = phenoml(
            token="YOUR_TOKEN",
        )
        client.summary.create_template(
            name="name",
            example_summary="Patient John Doe, age 45, presents with hypertension diagnosed on 2024-01-15.",
            target_resources=["Patient", "Condition", "Observation"],
            mode="mode",
        )
        """
        _response = self._raw_client.create_template(
            name=name,
            example_summary=example_summary,
            target_resources=target_resources,
            mode=mode,
            description=description,
            example_fhir_data=example_fhir_data,
            request_options=request_options,
        )
        return _response.data

    def get_template(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> SummaryGetTemplateResponse:
        """
        Retrieves a specific summary template

        Parameters
        ----------
        id : str
            Template ID

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

        Returns
        -------
        SummaryGetTemplateResponse
            Template retrieved successfully

        Examples
        --------
        from phenoml import phenoml

        client = phenoml(
            token="YOUR_TOKEN",
        )
        client.summary.get_template(
            id="id",
        )
        """
        _response = self._raw_client.get_template(id, request_options=request_options)
        return _response.data

    def update_template(
        self,
        id: str,
        *,
        name: str,
        template: str,
        target_resources: typing.Sequence[str],
        mode: str,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SummaryUpdateTemplateResponse:
        """
        Updates an existing summary template

        Parameters
        ----------
        id : str
            Template ID

        name : str

        template : str
            Updated template with placeholders

        target_resources : typing.Sequence[str]

        mode : str
            Template mode

        description : typing.Optional[str]

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

        Returns
        -------
        SummaryUpdateTemplateResponse
            Template updated successfully

        Examples
        --------
        from phenoml import phenoml

        client = phenoml(
            token="YOUR_TOKEN",
        )
        client.summary.update_template(
            id="id",
            name="name",
            template="template",
            target_resources=["target_resources"],
            mode="mode",
        )
        """
        _response = self._raw_client.update_template(
            id,
            name=name,
            template=template,
            target_resources=target_resources,
            mode=mode,
            description=description,
            request_options=request_options,
        )
        return _response.data

    def delete_template(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> SummaryDeleteTemplateResponse:
        """
        Deletes a summary template

        Parameters
        ----------
        id : str
            Template ID

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

        Returns
        -------
        SummaryDeleteTemplateResponse
            Template deleted successfully

        Examples
        --------
        from phenoml import phenoml

        client = phenoml(
            token="YOUR_TOKEN",
        )
        client.summary.delete_template(
            id="id",
        )
        """
        _response = self._raw_client.delete_template(id, request_options=request_options)
        return _response.data

    def create(
        self,
        *,
        fhir_resources: CreateSummaryRequestFhirResources,
        mode: typing.Optional[CreateSummaryRequestMode] = OMIT,
        template_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateSummaryResponse:
        """
        Creates a summary from FHIR resources using one of three modes:
        - **narrative**: Uses a template to substitute FHIR data into placeholders (requires template_id)
        - **flatten**: Flattens FHIR resources into a searchable format for RAG/search (no template needed)
        - **ips**: Generates an International Patient Summary (IPS) narrative per ISO 27269/HL7 FHIR IPS IG. Requires a Bundle with exactly one Patient resource (returns 400 error if no Patient or multiple Patients are present). Automatically filters resources to those referencing the patient and generates sections for allergies, medications, problems, immunizations, procedures, and vital signs.

        Parameters
        ----------
        fhir_resources : CreateSummaryRequestFhirResources
            FHIR resources (single resource or Bundle).
            For IPS mode, must be a Bundle containing exactly one Patient resource with at least one
            identifier (id, fullUrl, or identifier field). Returns an error if no Patient is found,
            if multiple Patients are present, or if the Patient has no identifiers. Resources are
            automatically filtered to only include those referencing the patient.

        mode : typing.Optional[CreateSummaryRequestMode]
            Summary generation mode:
            - narrative: Substitute FHIR data into a template (requires template_id)
            - flatten: Flatten FHIR resources for RAG/search (no template needed)
            - ips: Generate International Patient Summary (IPS) narrative per ISO 27269/HL7 FHIR IPS IG

        template_id : typing.Optional[str]
            ID of the template to use (required for narrative mode)

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

        Returns
        -------
        CreateSummaryResponse
            Summary generated successfully

        Examples
        --------
        from phenoml import phenoml
        from phenoml.summary import FhirResource

        client = phenoml(
            token="YOUR_TOKEN",
        )
        client.summary.create(
            fhir_resources=FhirResource(
                resource_type="resourceType",
            ),
        )
        """
        _response = self._raw_client.create(
            fhir_resources=fhir_resources, mode=mode, template_id=template_id, request_options=request_options
        )
        return _response.data


class AsyncSummaryClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawSummaryClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawSummaryClient
        """
        return self._raw_client

    async def list_templates(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> SummaryListTemplatesResponse:
        """
        Retrieves all summary templates for the authenticated user

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

        Returns
        -------
        SummaryListTemplatesResponse
            Templates retrieved successfully

        Examples
        --------
        import asyncio

        from phenoml import Asyncphenoml

        client = Asyncphenoml(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.summary.list_templates()


        asyncio.run(main())
        """
        _response = await self._raw_client.list_templates(request_options=request_options)
        return _response.data

    async def create_template(
        self,
        *,
        name: str,
        example_summary: str,
        target_resources: typing.Sequence[str],
        mode: str,
        description: typing.Optional[str] = OMIT,
        example_fhir_data: typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateSummaryTemplateResponse:
        """
        Creates a summary template from an example using LLM function calling

        Parameters
        ----------
        name : str
            Name of the template

        example_summary : str
            Example summary note to generate template from

        target_resources : typing.Sequence[str]
            List of target FHIR resources

        mode : str
            Template mode (stored with the template)

        description : typing.Optional[str]
            Description of the template

        example_fhir_data : typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]]
            Optional example FHIR data that corresponds to the example summary

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

        Returns
        -------
        CreateSummaryTemplateResponse
            Template created successfully

        Examples
        --------
        import asyncio

        from phenoml import Asyncphenoml

        client = Asyncphenoml(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.summary.create_template(
                name="name",
                example_summary="Patient John Doe, age 45, presents with hypertension diagnosed on 2024-01-15.",
                target_resources=["Patient", "Condition", "Observation"],
                mode="mode",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create_template(
            name=name,
            example_summary=example_summary,
            target_resources=target_resources,
            mode=mode,
            description=description,
            example_fhir_data=example_fhir_data,
            request_options=request_options,
        )
        return _response.data

    async def get_template(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> SummaryGetTemplateResponse:
        """
        Retrieves a specific summary template

        Parameters
        ----------
        id : str
            Template ID

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

        Returns
        -------
        SummaryGetTemplateResponse
            Template retrieved successfully

        Examples
        --------
        import asyncio

        from phenoml import Asyncphenoml

        client = Asyncphenoml(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.summary.get_template(
                id="id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_template(id, request_options=request_options)
        return _response.data

    async def update_template(
        self,
        id: str,
        *,
        name: str,
        template: str,
        target_resources: typing.Sequence[str],
        mode: str,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SummaryUpdateTemplateResponse:
        """
        Updates an existing summary template

        Parameters
        ----------
        id : str
            Template ID

        name : str

        template : str
            Updated template with placeholders

        target_resources : typing.Sequence[str]

        mode : str
            Template mode

        description : typing.Optional[str]

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

        Returns
        -------
        SummaryUpdateTemplateResponse
            Template updated successfully

        Examples
        --------
        import asyncio

        from phenoml import Asyncphenoml

        client = Asyncphenoml(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.summary.update_template(
                id="id",
                name="name",
                template="template",
                target_resources=["target_resources"],
                mode="mode",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update_template(
            id,
            name=name,
            template=template,
            target_resources=target_resources,
            mode=mode,
            description=description,
            request_options=request_options,
        )
        return _response.data

    async def delete_template(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> SummaryDeleteTemplateResponse:
        """
        Deletes a summary template

        Parameters
        ----------
        id : str
            Template ID

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

        Returns
        -------
        SummaryDeleteTemplateResponse
            Template deleted successfully

        Examples
        --------
        import asyncio

        from phenoml import Asyncphenoml

        client = Asyncphenoml(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.summary.delete_template(
                id="id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.delete_template(id, request_options=request_options)
        return _response.data

    async def create(
        self,
        *,
        fhir_resources: CreateSummaryRequestFhirResources,
        mode: typing.Optional[CreateSummaryRequestMode] = OMIT,
        template_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateSummaryResponse:
        """
        Creates a summary from FHIR resources using one of three modes:
        - **narrative**: Uses a template to substitute FHIR data into placeholders (requires template_id)
        - **flatten**: Flattens FHIR resources into a searchable format for RAG/search (no template needed)
        - **ips**: Generates an International Patient Summary (IPS) narrative per ISO 27269/HL7 FHIR IPS IG. Requires a Bundle with exactly one Patient resource (returns 400 error if no Patient or multiple Patients are present). Automatically filters resources to those referencing the patient and generates sections for allergies, medications, problems, immunizations, procedures, and vital signs.

        Parameters
        ----------
        fhir_resources : CreateSummaryRequestFhirResources
            FHIR resources (single resource or Bundle).
            For IPS mode, must be a Bundle containing exactly one Patient resource with at least one
            identifier (id, fullUrl, or identifier field). Returns an error if no Patient is found,
            if multiple Patients are present, or if the Patient has no identifiers. Resources are
            automatically filtered to only include those referencing the patient.

        mode : typing.Optional[CreateSummaryRequestMode]
            Summary generation mode:
            - narrative: Substitute FHIR data into a template (requires template_id)
            - flatten: Flatten FHIR resources for RAG/search (no template needed)
            - ips: Generate International Patient Summary (IPS) narrative per ISO 27269/HL7 FHIR IPS IG

        template_id : typing.Optional[str]
            ID of the template to use (required for narrative mode)

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

        Returns
        -------
        CreateSummaryResponse
            Summary generated successfully

        Examples
        --------
        import asyncio

        from phenoml import Asyncphenoml
        from phenoml.summary import FhirResource

        client = Asyncphenoml(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.summary.create(
                fhir_resources=FhirResource(
                    resource_type="resourceType",
                ),
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create(
            fhir_resources=fhir_resources, mode=mode, template_id=template_id, request_options=request_options
        )
        return _response.data
