from datetime import datetime
from typing import Optional
from sqlalchemy import ForeignKey, String, Integer, DateTime, func, Boolean
from sqlalchemy.orm import Mapped, mapped_column
from app.db.base import Base 

# Közös beállítás az összes táblához ebben a fájlban
SCHEMA_ARGS = {"schema": "data"}

class PointRule(Base):
    __tablename__ = "point_rules"
    __table_args__ = SCHEMA_ARGS
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    action_key: Mapped[str] = mapped_column(String, unique=True, index=True)
    points: Mapped[int] = mapped_column(Integer, default=0)
    description: Mapped[Optional[str]] = mapped_column(String)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)

class LevelConfig(Base):
    __tablename__ = "level_configs"
    __table_args__ = SCHEMA_ARGS
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    level_number: Mapped[int] = mapped_column(Integer, unique=True)
    min_points: Mapped[int] = mapped_column(Integer)
    rank_name: Mapped[str] = mapped_column(String)

class RegionalSetting(Base):
    __tablename__ = "regional_settings"
    __table_args__ = SCHEMA_ARGS
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    country_code: Mapped[str] = mapped_column(String, unique=True)
    currency: Mapped[str] = mapped_column(String, default="HUF")
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)

class PointsLedger(Base):
    __tablename__ = "points_ledger"
    __table_args__ = SCHEMA_ARGS
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    # JAVÍTVA: data.users.id hivatkozás
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("data.users.id"))
    points: Mapped[int] = mapped_column(Integer)
    reason: Mapped[str] = mapped_column(String)
    created_at: Mapped[datetime] = mapped_column(DateTime, default=func.now())

class UserStats(Base):
    __tablename__ = "user_stats"
    __table_args__ = SCHEMA_ARGS
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    # JAVÍTVA: data.users.id hivatkozás
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("data.users.id"), unique=True)
    total_points: Mapped[int] = mapped_column(Integer, default=0)
    current_level: Mapped[int] = mapped_column(Integer, default=1)
    last_activity: Mapped[datetime] = mapped_column(DateTime, default=func.now())

class Badge(Base):
    __tablename__ = "badges"
    __table_args__ = SCHEMA_ARGS
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    name: Mapped[str] = mapped_column(String, unique=True)
    description: Mapped[str] = mapped_column(String)
    icon_url: Mapped[Optional[str]] = mapped_column(String)

class UserBadge(Base):
    __tablename__ = "user_badges"
    __table_args__ = SCHEMA_ARGS
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    # JAVÍTVA: data.users.id hivatkozás
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("data.users.id"))
    badge_id: Mapped[int] = mapped_column(Integer, ForeignKey("data.badges.id"))
    earned_at: Mapped[datetime] = mapped_column(DateTime, default=func.now())