from sqlmodel import SQLModel, Field
from typing import Optional, Dict
from uuid import UUID, uuid4
from datetime import datetime
from sqlalchemy import Column, DateTime, func, JSON

class AnalyticsEvent(SQLModel, table=True):
    __tablename__ = "analytics_events"

    id: Optional[UUID] = Field(default_factory=uuid4, primary_key=True)
    event_name: str = Field(index=True)
    user_id: Optional[UUID] = Field(default=None, index=True, foreign_key="users.id")
    screen_name: Optional[str] = None
    item_id: Optional[str] = None # route_id, stop_id, promo_id, etc.
    properties: Optional[Dict] = Field(default_factory=dict, sa_column=Column(JSON))
    timestamp: datetime = Field(
        default_factory=datetime.utcnow,
        sa_column=Column(DateTime(timezone=True), server_default=func.now(), index=True)
    )
