import enum
import warnings
from dataclasses import dataclass, field
from typing import NotRequired, TypeAlias, TypedDict

import pandas as pd


# ** API
class HttpRequestTypes(enum.StrEnum):
    GET = enum.auto()
    POST = enum.auto()
    PUT = enum.auto()
    DELETE = enum.auto()


HttpContentHeaders = TypedDict(
    "HttpContentHeaders",
    {
        "Content-type": str,
        "Accept": str,
        "DelecoToken": NotRequired[str],
    },
)


class CurrentConnection:
    def __init__(
        self,
        base_headers: HttpContentHeaders,
    ) -> None:
        self._headers = base_headers
        self._session_token: str | None = None
        self._logged_in: bool = False

    @property
    def headers(self) -> HttpContentHeaders:
        return self._headers

    @property
    def session_token(self) -> str | None:
        return self._session_token

    @property
    def logged_in(self) -> bool:
        return self._logged_in

    def add_session_token(
        self,
        token: str,
    ) -> None:
        if self.session_token is not None:
            warnings.warn(
                "Setting new API session token despite it was already set. "
                "Overwriting existing token."
            )
        self._session_token = token
        self._headers.update(DelecoToken=token)
        self._logged_in = True

    def remove_session_token(self) -> None:
        if "DelecoToken" in self.headers:
            del self._headers["DelecoToken"]
            self._session_token = None
            self._logged_in = False


# ** forecasts
@dataclass(slots=True)
class CustomerDataSalesForecast:
    order: list[int] = field(default_factory=list)
    date: list[pd.Timestamp] = field(default_factory=list)
    sales: list[float] = field(default_factory=list)


class FcErrorCodes(enum.IntEnum):
    SUCCESS = 0
    DATA_TOO_FEW_POINTS = 1
    DATA_BAD_QUALITY = 2


FcResult: TypeAlias = tuple[FcErrorCodes, pd.DataFrame | None]
