from __future__ import annotations

import datetime as dt
from typing import Any, Dict, Optional

from sqlalchemy import Column, JSON
from sqlmodel import Field, SQLModel


class Game(SQLModel, table=True):
    id: str = Field(primary_key=True)
    name: str
    status: str
    max_players: int
    created_by: str
    created_at: dt.datetime
    updated_at: dt.datetime
    seed: int
    slots: Dict[str, Any] = Field(sa_column=Column(JSON))
    winner: Optional[str] = None


class GameEvent(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    game_id: str = Field(index=True)
    idx: int
    ts: dt.datetime
    actor: str
    action_type: str
    payload: Dict[str, Any] = Field(sa_column=Column(JSON))
    applied: bool = True
    debug_payload: Optional[Dict[str, Any]] = Field(default=None, sa_column=Column(JSON))


class TradeOffer(SQLModel, table=True):
    id: str = Field(primary_key=True)
    game_id: str = Field(index=True)
    from_player: str
    to_player: Optional[str] = None
    offer: Dict[str, int] = Field(sa_column=Column(JSON))
    request: Dict[str, int] = Field(sa_column=Column(JSON))
    status: str
    created_at: dt.datetime
    updated_at: dt.datetime
