import enum
from sqlalchemy import Column, Integer, String, ForeignKey, Enum, DateTime, Boolean, Text, UniqueConstraint
from app.db.base import Base
from datetime import datetime

# Enums (már schema="data" beállítással a biztonságért)
class ModerationStatus(str, enum.Enum):
    pending = "pending"
    approved = "approved"
    rejected = "rejected"

class SourceType(str, enum.Enum):
    manual = "manual"
    ocr = "ocr"
    api_import = "import"

class ServiceProvider(Base):
    __tablename__ = "service_providers"
    __table_args__ = {"schema": "data"}

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, nullable=False)
    address = Column(String, nullable=False)
    category = Column(String)
    
    status = Column(Enum(ModerationStatus, schema="data", name="moderation_status_enum"), default=ModerationStatus.pending, nullable=False)
    source = Column(Enum(SourceType, schema="data", name="source_type_enum"), default=SourceType.manual, nullable=False)
    
    # --- ÚJ MEZŐ ---
    validation_score = Column(Integer, default=0) # A közösségi szavazatok összege
    # ---------------

    evidence_image_path = Column(String, nullable=True)
    added_by_user_id = Column(Integer, ForeignKey("data.users.id"))
    created_at = Column(DateTime, default=datetime.utcnow)

class Vote(Base):
    __tablename__ = "votes"
    __table_args__ = (
        UniqueConstraint('user_id', 'provider_id', name='uq_user_provider_vote'),
        {"schema": "data"}
    )

    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey("data.users.id"), nullable=False)
    provider_id = Column(Integer, ForeignKey("data.service_providers.id"), nullable=False)
    vote_value = Column(Integer, nullable=False) # +1 vagy -1

class Competition(Base):
    __tablename__ = "competitions"
    __table_args__ = {"schema": "data"}

    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False) # Pl: "Januári Feltöltő Verseny"
    description = Column(Text)
    start_date = Column(DateTime, nullable=False)
    end_date = Column(DateTime, nullable=False)
    is_active = Column(Boolean, default=True)

class UserScore(Base):
    __tablename__ = "user_scores"
    __table_args__ = (
        UniqueConstraint('user_id', 'competition_id', name='uq_user_competition_score'),
        {"schema": "data"}
    )

    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey("data.users.id"))
    competition_id = Column(Integer, ForeignKey("data.competitions.id"))
    points = Column(Integer, default=0)
    last_updated = Column(DateTime, default=datetime.utcnow)