from __future__ import annotations

from typing import Any

from sqlalchemy import Boolean, DateTime, Float, Integer, String, Text
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.sql import text


def map_type(type_name: str, *, item_type: str | None = None):
    match type_name:
        case "list":
            if not item_type:
                raise ValueError("list type requires item_type")
            return ARRAY(map_type(item_type))
        case "str":
            return String()
        case "text":
            return Text()
        case "int":
            return Integer()
        case "float":
            return Float()
        case "bool":
            return Boolean()
        case "datetime":
            return DateTime(timezone=True)
        case "uuid":
            return UUID(as_uuid=True)
        case _:
            raise ValueError(f"unsupported type: {type_name}")


def map_server_default(default: Any | None):
    # MVP: only support DB-side now(). uuid4 is generated by SDK.
    if default is None:
        return None
    if default == "now":
        return text("now()")
    return None
