from enum import Enum
from typing import Literal, TypeAlias

from pydantic import BaseModel


class ChatHistoryColumn(str, Enum):
    """Canonical logical column names used by chat history providers."""

    ID = "id"
    USER_ID = "user_id"
    SESSION_ID = "session_id"
    ROLE = "role"
    CONTENT = "content"
    METADATA = "metadata"
    CREATED_AT = "created_at"


CHAT_HISTORY_COLUMNS: tuple[ChatHistoryColumn, ...] = tuple(ChatHistoryColumn)
CHAT_HISTORY_FILTERABLE_COLUMNS: frozenset[ChatHistoryColumn] = frozenset(
    {
        ChatHistoryColumn.ID,
        ChatHistoryColumn.USER_ID,
        ChatHistoryColumn.SESSION_ID,
        ChatHistoryColumn.ROLE,
        ChatHistoryColumn.CONTENT,
    }
)
CHAT_HISTORY_ORDERABLE_COLUMNS: tuple[ChatHistoryColumn, ...] = (
    ChatHistoryColumn.CREATED_AT,
    ChatHistoryColumn.ID,
)
CHAT_HISTORY_INDEXED_COLUMNS: tuple[ChatHistoryColumn, ...] = (
    ChatHistoryColumn.USER_ID,
    ChatHistoryColumn.SESSION_ID,
    ChatHistoryColumn.CREATED_AT,
)


ProviderName: TypeAlias = Literal["postgres", "sqlite", "supabase"]
ChatHistoryOrder: TypeAlias = Literal["asc", "desc"]
ChatHistoryOrderBy: TypeAlias = Literal["created_at", "id"]
ChatHistoryFilterValue: TypeAlias = str | int | float | bool | None
ChatHistoryRole: TypeAlias = Literal["system", "user", "assistant", "tool", "developer"]


class ChatHistoryFilters(BaseModel):
    """Shared message-level filters used by fetch and delete templates."""

    id: str | None = None
    role: ChatHistoryRole | None = None
    content: str | None = None
