# User Database Models
from typing import Optional
from sqlalchemy import String, Boolean, JSON, Integer
from sqlalchemy.orm import Mapped, mapped_column

from app.database.base import Base, TimestampMixin


class User(Base, TimestampMixin):
    """User model for authentication"""
    __tablename__ = "users"

    id: Mapped[int] = mapped_column(primary_key=True)
    username: Mapped[str] = mapped_column(String, unique=True, nullable=False, index=True)
    email: Mapped[str] = mapped_column(String, unique=True, nullable=False, index=True)
    hashed_password: Mapped[str] = mapped_column(String, nullable=False)

    is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    is_superuser: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    failed_login_attempts: Mapped[int] = mapped_column(default=0, nullable=False, server_default="0")

    # Recent market deployments (max 5, most recent first)
    recent_events: Mapped[Optional[list]] = mapped_column(JSON, nullable=True, default=list)

    # Subscription
    subscription_tier: Mapped[Optional[str]] = mapped_column(String(20), nullable=True)  # 'terminal' or 'api'
    stripe_customer_id: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    stripe_subscription_id: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)

    # Referral
    referral_code: Mapped[Optional[str]] = mapped_column(String(50), nullable=True, index=True)
    referred_by: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)

    # Session settings
    session_ttl_hours: Mapped[int] = mapped_column(Integer, default=72, nullable=False, server_default="72")