"""
Badge models for SQLAlchemy ORM
"""
from datetime import datetime
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Text
from sqlalchemy.orm import relationship
from app.database import Base


class Badge(Base):
    """Badge definition model"""
    __tablename__ = "badges"

    id = Column(Integer, primary_key=True, autoincrement=True)
    badge_id = Column(String(50), unique=True, nullable=False, index=True)
    name = Column(String(100), nullable=False)
    description = Column(Text, nullable=False)
    icon = Column(String(10), nullable=False)  # emoji or icon string
    category = Column(String(50), nullable=False)  # predictions, accuracy, engagement, social
    criteria_type = Column(String(50), nullable=False)  # predictions_count, correct_predictions, etc.
    criteria_value = Column(Integer, nullable=False)
    criteria_description = Column(String(200), nullable=False)
    rarity = Column(String(20), nullable=False)  # common, rare, epic, legendary
    points = Column(Integer, nullable=False)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)

    # Relationships
    user_badges = relationship("UserBadge", back_populates="badge")


class UserBadge(Base):
    """User's unlocked badges"""
    __tablename__ = "user_badges"

    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    badge_id = Column(Integer, ForeignKey("badges.id"), nullable=False, index=True)
    unlocked_at = Column(DateTime, default=datetime.utcnow, nullable=False)

    # Relationships
    user = relationship("User", back_populates="badges")
    badge = relationship("Badge", back_populates="user_badges")

    def __repr__(self):
        return f"<UserBadge user_id={self.user_id} badge_id={self.badge_id}>"
