from enum import Enum
from typing import Annotated, Any

from pydantic import BaseModel, Field


class Granularity(str, Enum):
    """
    Time Series Granuality
    """

    seconds = "seconds"
    minutes = "minutes"
    hours = "hours"


class TimeSeriesConfig(BaseModel):
    """
    Time Series Collection config
    """

    time_field: str
    meta_field: str | None = None
    granularity: Granularity | None = None
    bucket_max_span_seconds: int | None = None
    bucket_rounding_second: Annotated[
        int | None,
        Field(
            deprecated="This field is deprecated in favor of 'bucket_rounding_seconds'.",
        ),
    ] = None
    bucket_rounding_seconds: int | None = None
    expire_after_seconds: int | None = None

    def build_query(self, collection_name: str) -> dict[str, Any]:
        res: dict[str, Any] = {"name": collection_name}
        timeseries: dict[str, Any] = {"timeField": self.time_field}

        if self.meta_field is not None:
            timeseries["metaField"] = self.meta_field
        if self.granularity is not None:
            timeseries["granularity"] = self.granularity
        if self.bucket_max_span_seconds is not None:
            timeseries["bucketMaxSpanSeconds"] = self.bucket_max_span_seconds

        # Use new field if present, otherwise fallback to deprecated one without triggering warning
        rounding = self.bucket_rounding_seconds
        if rounding is None:
            # Avoid deprecation warning by checking __dict__ directly
            rounding = self.__dict__.get("bucket_rounding_second")

        if rounding is not None:
            timeseries["bucketRoundingSeconds"] = rounding

        res["timeseries"] = timeseries

        if self.expire_after_seconds is not None:
            res["expireAfterSeconds"] = self.expire_after_seconds

        return res
