# @generated
# This file was automatically generated by the ncae-sdk code generator.
# DO NOT EDIT MANUALLY, ANY CHANGES WILL BE OVERWRITTEN.

from collections.abc import Iterator
from contextlib import contextmanager
from typing import IO, Final, Optional

from typing_extensions import Unpack, deprecated

from ncae_sdk._resource import ResourceId
from ncae_sdk._sync.endpoint import Endpoint
from ncae_sdk._util import map_sync
from ncae_sdk.resources._schema import (
    UploadFile,
    UploadFileCreate,
    UploadFileCreateModel,
    UploadFileFilter,
    UploadFileFilterModel,
    UploadFileUpdate,
    UploadFileUpdateModel,
)


class UploadFileEndpoint(Endpoint):
    BASE_PATH: Final[str] = "automation/v1/upload-file"

    def list(self, **filters: Unpack[UploadFileFilter]) -> Iterator[UploadFile]:
        results = self._list(self.BASE_PATH, UploadFileFilterModel, filters)
        return map_sync(UploadFile.parse_api, results)

    def create(self, file: IO[bytes], **payload: Unpack[UploadFileCreate]) -> UploadFile:
        result = self._create_multipart(self.BASE_PATH, UploadFileCreateModel, payload, {"file": file})
        return UploadFile.parse_api(result)

    def find(self, **filters: Unpack[UploadFileFilter]) -> Optional[UploadFile]:
        result = self._get_by_filters(self.BASE_PATH, UploadFileFilterModel, filters)
        return UploadFile.parse_api(result) if result else None

    def get(self, rid: ResourceId) -> Optional[UploadFile]:
        result = self._get_by_id(self.BASE_PATH, rid)
        return UploadFile.parse_api(result) if result else None

    def update(self, rid: ResourceId, /, **payload: Unpack[UploadFileUpdate]) -> UploadFile:
        result = self._update(self.BASE_PATH, rid, UploadFileUpdateModel, payload)
        return UploadFile.parse_api(result)

    def delete(self, rid: ResourceId) -> bool:
        return self._delete(self.BASE_PATH, rid)

    @deprecated("Use 'create' method instead, which is more appropriately named and has the same signature")
    def upload(self, file: IO[bytes], **payload: Unpack[UploadFileCreate]) -> UploadFile:
        return self.create(file, **payload)

    @contextmanager
    def download(self, rid: ResourceId, *, chunk_size: Optional[int] = None) -> Iterator[Iterator[bytes]]:
        with self._session.stream("GET", f"{self.BASE_PATH}/{rid}/download") as response:
            yield response.iter_bytes(chunk_size=chunk_size)
