import uuid
from datetime import datetime

from sqlalchemy import String, Float, Boolean, BigInteger, DateTime, func
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import Mapped, mapped_column, relationship

from backend.app.core.database import Base


class Rider(Base):
    __tablename__ = "riders"

    id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    telegram_id: Mapped[int | None] = mapped_column(BigInteger, unique=True, index=True, nullable=True)
    telegram_username: Mapped[str | None] = mapped_column(String(100), nullable=True)
    avatar_url: Mapped[str | None] = mapped_column(String(500), nullable=True)
    name: Mapped[str] = mapped_column(String(100))
    ftp: Mapped[float | None] = mapped_column(Float, nullable=True)
    lthr: Mapped[int | None] = mapped_column(nullable=True)
    weight: Mapped[float | None] = mapped_column(Float, nullable=True)
    zones_config: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
    goals: Mapped[str | None] = mapped_column(String(500), nullable=True)
    experience_level: Mapped[str | None] = mapped_column(String(50), nullable=True)
    coaching_profile: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
    onboarding_completed: Mapped[bool] = mapped_column(Boolean, default=False)

    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    activities = relationship("Activity", back_populates="rider", lazy="selectin")
