"""Analytics database models.

Defines SQLAlchemy models for analytics event and session tracking.

⚠️ AUTO-GENERATED BY PRISM — DO NOT EDIT
"""

from __future__ import annotations

from datetime import datetime

from sqlalchemy import Boolean, DateTime, Float, Index, Integer, String, Text
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column

from {{ project_name }}.models.base import Base


class AnalyticsEvent(Base):
    """Tracks individual analytics events."""

    __tablename__ = "analytics_events"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    event_name: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
    category: Mapped[str] = mapped_column(String(100), nullable=False, default="custom", index=True)
    properties: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
    user_id: Mapped[str | None] = mapped_column(String(255), nullable=True, index=True)
    session_id: Mapped[str | None] = mapped_column(String(255), nullable=True, index=True)
    page_url: Mapped[str | None] = mapped_column(String(2048), nullable=True)
    timestamp: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        default=datetime.utcnow,
        index=True,
    )
{% if track_device_info %}
    user_agent: Mapped[str | None] = mapped_column(String(512), nullable=True)
{% endif %}
{% if track_referrer %}
    referrer: Mapped[str | None] = mapped_column(String(2048), nullable=True)
{% endif %}
{% if track_geo %}
    country: Mapped[str | None] = mapped_column(String(2), nullable=True)
    city: Mapped[str | None] = mapped_column(String(255), nullable=True)
{% endif %}

    __table_args__ = (
        Index("ix_analytics_events_name_ts", "event_name", "timestamp"),
        Index("ix_analytics_events_category_ts", "category", "timestamp"),
    )

    def __repr__(self) -> str:
        return f"<AnalyticsEvent(id={self.id}, event={self.event_name}, ts={self.timestamp})>"

{% if track_user_sessions %}


class AnalyticsSession(Base):
    """Tracks user sessions for analytics."""

    __tablename__ = "analytics_sessions"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    session_id: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
    user_id: Mapped[str | None] = mapped_column(String(255), nullable=True, index=True)
    started_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        default=datetime.utcnow,
    )
    ended_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True),
        nullable=True,
    )
    is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    page_views: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    events_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    duration_seconds: Mapped[float | None] = mapped_column(Float, nullable=True)
{% if track_device_info %}
    user_agent: Mapped[str | None] = mapped_column(String(512), nullable=True)
{% endif %}
{% if track_referrer %}
    referrer: Mapped[str | None] = mapped_column(String(2048), nullable=True)
    landing_page: Mapped[str | None] = mapped_column(String(2048), nullable=True)
{% endif %}
{% if track_geo %}
    country: Mapped[str | None] = mapped_column(String(2), nullable=True)
    city: Mapped[str | None] = mapped_column(String(255), nullable=True)
{% endif %}

    __table_args__ = (
        Index("ix_analytics_sessions_user_ts", "user_id", "started_at"),
    )

    def __repr__(self) -> str:
        return f"<AnalyticsSession(id={self.id}, session={self.session_id}, active={self.is_active})>"
{% endif %}
