# @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 datetime import datetime
from typing import Any, Final, List, Optional

from typing_extensions import Unpack

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 (
    InfluxRepository,
    InfluxRepositoryCreate,
    InfluxRepositoryCreateModel,
    InfluxRepositoryFilter,
    InfluxRepositoryFilterModel,
    InfluxRepositoryUpdate,
    InfluxRepositoryUpdateModel,
)
from ncae_sdk.types._models import InfluxDataPoint, InfluxTagFilter


class InfluxRepositoryEndpoint(Endpoint):
    BASE_PATH: Final[str] = "observability/v1/influx-repository"

    def list(self, **filters: Unpack[InfluxRepositoryFilter]) -> Iterator[InfluxRepository]:
        results = self._list(self.BASE_PATH, InfluxRepositoryFilterModel, filters)
        return map_sync(InfluxRepository.parse_api, results)

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

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

    def create(self, **payload: Unpack[InfluxRepositoryCreate]) -> InfluxRepository:
        result = self._create(self.BASE_PATH, InfluxRepositoryCreateModel, payload)
        return InfluxRepository.parse_api(result)

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

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

    def ingest_metrics(self, rid: ResourceId, points: List[InfluxDataPoint]) -> None:
        points_data = [point.model_dump(exclude_defaults=True, mode="json") for point in points]
        self._session.post(
            f"{self.BASE_PATH}/{rid}/metrics/ingest",
            json={"points": points_data},
        )

    def query_metrics(
        self,
        rid: ResourceId,
        date_from: Optional[datetime] = None,
        date_to: Optional[datetime] = None,
        tag_filters: Optional[List[InfluxTagFilter]] = None,
    ) -> Any:
        date_from_data = date_from.isoformat() if date_from else None
        date_to_data = date_to.isoformat() if date_to else None
        tag_filter_data = (
            [tag_filter.model_dump(exclude_defaults=True, mode="json") for tag_filter in tag_filters]
            if tag_filters
            else []
        )

        response = self._session.post(
            f"{self.BASE_PATH}/{rid}/metrics/query",
            json={
                "date_from": date_from_data,
                "date_to": date_to_data,
                "tag_filters": tag_filter_data,
            },
        )
        return response.json()
