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

import typing

from .. import core
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.file import File
from ..types.max_page_size import MaxPageSize
from ..types.next_page_token import NextPageToken
from ..types.sort_dir import SortDir
from .raw_client import AsyncRawFilesClient, RawFilesClient
from .types.files_delete_response import FilesDeleteResponse
from .types.files_list_response import FilesListResponse

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


class FilesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawFilesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawFilesClient
        """
        return self._raw_client

    def list(
        self,
        *,
        name_contains: typing.Optional[str] = None,
        sort_dir: typing.Optional[SortDir] = None,
        next_page_token: typing.Optional[NextPageToken] = None,
        max_page_size: typing.Optional[MaxPageSize] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> FilesListResponse:
        """
        List files.

        Parameters
        ----------
        name_contains : typing.Optional[str]
            Filters files to only include those that contain the given string in the name.

            Example: `"invoice"`

        sort_dir : typing.Optional[SortDir]

        next_page_token : typing.Optional[NextPageToken]

        max_page_size : typing.Optional[MaxPageSize]

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

        Returns
        -------
        FilesListResponse
            List of files

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.files.list(
            next_page_token="xK9mLPqRtN3vS8wF5hB2cQ==:zWvUxYjM4nKpL7aDgE9HbTcR2mAyX3/Q+CNkfBSw1dZ=",
        )
        """
        _response = self._raw_client.list(
            name_contains=name_contains,
            sort_dir=sort_dir,
            next_page_token=next_page_token,
            max_page_size=max_page_size,
            request_options=request_options,
        )
        return _response.data

    def retrieve(
        self,
        id: str,
        *,
        raw_text: typing.Optional[bool] = None,
        markdown: typing.Optional[bool] = None,
        html: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> File:
        """
        Fetch a file by its ID.

        Parameters
        ----------
        id : str
            ID for the file. It will always start with `"file_"`.

            Example: `"file_Xj8mK2pL9nR4vT7qY5wZ"`

        raw_text : typing.Optional[bool]
            **Deprecated:** Use `POST /parse_runs` instead to parse file contents.

            If set to true, the raw text content of the file will be included in the response.

        markdown : typing.Optional[bool]
            **Deprecated:** Use `POST /parse_runs` instead to parse file contents.

            If set to true, the markdown content of the file will be included in the response.

            Only available for files with a type of PDF, IMG, or DOCX files that were auto-converted to PDFs.

        html : typing.Optional[bool]
            **Deprecated:** Use `POST /parse_runs` instead to parse file contents.

            If set to true, the html content of the file will be included in the response.

            Only available for files with a type of DOCX.

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

        Returns
        -------
        File
            Retrieved file

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.files.retrieve(
            id="file_id_here",
        )
        """
        _response = self._raw_client.retrieve(
            id, raw_text=raw_text, markdown=markdown, html=html, request_options=request_options
        )
        return _response.data

    def delete(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> FilesDeleteResponse:
        """
        Delete a file and all associated data from Extend. This operation is permanent and cannot be undone.

        This endpoint can be used if you'd like to manage data retention on your own rather than automated data retention policies. Or make one-off deletions for your downstream customers.

        Parameters
        ----------
        id : str
            The ID of the file to delete.

            Example: `"file_xK9mLPqRtN3vS8wF5hB2cQ"`

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

        Returns
        -------
        FilesDeleteResponse
            Successfully deleted file

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.files.delete(
            id="file_id_here",
        )
        """
        _response = self._raw_client.delete(id, request_options=request_options)
        return _response.data

    def upload(self, *, file: core.File, request_options: typing.Optional[RequestOptions] = None) -> File:
        """
        Upload and create a new file in Extend.

        This endpoint accepts file contents and registers them as a File in Extend, which can be used for [running workflows](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/workflow/run-workflow), [creating evaluation set items](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/evaluation/bulk-create-evaluation-set-items), [parsing](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/parse/parse-file), etc.

        If an uploaded file is detected as a Word or PowerPoint document, it will be automatically converted to a PDF.

        Supported file types can be found [here](https://docs.extend.ai/2026-02-09/product/general/supported-file-types).

        This endpoint requires multipart form encoding. Most HTTP clients will handle this encoding automatically (see the examples).

        Parameters
        ----------
        file : core.File
            See core.File for more documentation

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

        Returns
        -------
        File
            Successfully uploaded file

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.files.upload()
        """
        _response = self._raw_client.upload(file=file, request_options=request_options)
        return _response.data


class AsyncFilesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawFilesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawFilesClient
        """
        return self._raw_client

    async def list(
        self,
        *,
        name_contains: typing.Optional[str] = None,
        sort_dir: typing.Optional[SortDir] = None,
        next_page_token: typing.Optional[NextPageToken] = None,
        max_page_size: typing.Optional[MaxPageSize] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> FilesListResponse:
        """
        List files.

        Parameters
        ----------
        name_contains : typing.Optional[str]
            Filters files to only include those that contain the given string in the name.

            Example: `"invoice"`

        sort_dir : typing.Optional[SortDir]

        next_page_token : typing.Optional[NextPageToken]

        max_page_size : typing.Optional[MaxPageSize]

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

        Returns
        -------
        FilesListResponse
            List of files

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.files.list(
                next_page_token="xK9mLPqRtN3vS8wF5hB2cQ==:zWvUxYjM4nKpL7aDgE9HbTcR2mAyX3/Q+CNkfBSw1dZ=",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list(
            name_contains=name_contains,
            sort_dir=sort_dir,
            next_page_token=next_page_token,
            max_page_size=max_page_size,
            request_options=request_options,
        )
        return _response.data

    async def retrieve(
        self,
        id: str,
        *,
        raw_text: typing.Optional[bool] = None,
        markdown: typing.Optional[bool] = None,
        html: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> File:
        """
        Fetch a file by its ID.

        Parameters
        ----------
        id : str
            ID for the file. It will always start with `"file_"`.

            Example: `"file_Xj8mK2pL9nR4vT7qY5wZ"`

        raw_text : typing.Optional[bool]
            **Deprecated:** Use `POST /parse_runs` instead to parse file contents.

            If set to true, the raw text content of the file will be included in the response.

        markdown : typing.Optional[bool]
            **Deprecated:** Use `POST /parse_runs` instead to parse file contents.

            If set to true, the markdown content of the file will be included in the response.

            Only available for files with a type of PDF, IMG, or DOCX files that were auto-converted to PDFs.

        html : typing.Optional[bool]
            **Deprecated:** Use `POST /parse_runs` instead to parse file contents.

            If set to true, the html content of the file will be included in the response.

            Only available for files with a type of DOCX.

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

        Returns
        -------
        File
            Retrieved file

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.files.retrieve(
                id="file_id_here",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.retrieve(
            id, raw_text=raw_text, markdown=markdown, html=html, request_options=request_options
        )
        return _response.data

    async def delete(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> FilesDeleteResponse:
        """
        Delete a file and all associated data from Extend. This operation is permanent and cannot be undone.

        This endpoint can be used if you'd like to manage data retention on your own rather than automated data retention policies. Or make one-off deletions for your downstream customers.

        Parameters
        ----------
        id : str
            The ID of the file to delete.

            Example: `"file_xK9mLPqRtN3vS8wF5hB2cQ"`

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

        Returns
        -------
        FilesDeleteResponse
            Successfully deleted file

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.files.delete(
                id="file_id_here",
            )


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

    async def upload(self, *, file: core.File, request_options: typing.Optional[RequestOptions] = None) -> File:
        """
        Upload and create a new file in Extend.

        This endpoint accepts file contents and registers them as a File in Extend, which can be used for [running workflows](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/workflow/run-workflow), [creating evaluation set items](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/evaluation/bulk-create-evaluation-set-items), [parsing](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/parse/parse-file), etc.

        If an uploaded file is detected as a Word or PowerPoint document, it will be automatically converted to a PDF.

        Supported file types can be found [here](https://docs.extend.ai/2026-02-09/product/general/supported-file-types).

        This endpoint requires multipart form encoding. Most HTTP clients will handle this encoding automatically (see the examples).

        Parameters
        ----------
        file : core.File
            See core.File for more documentation

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

        Returns
        -------
        File
            Successfully uploaded file

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.files.upload()


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