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

# --- Enums for Logic Control ---
class ModerationStatus(str, enum.Enum):
    PENDING = "pending"
    APPROVED = "approved"
    REJECTED = "rejected"

class SourceType(str, enum.Enum):
    MANUAL = "manual"     # User uploaded
    OCR = "ocr"           # Extracted from a receipt
    IMPORT = "import"     # System import

# --- Models ---
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)
    
    # Moderation Logic
    status = Column(Enum(ModerationStatus), default=ModerationStatus.PENDING, nullable=False)
    source = Column(Enum(SourceType), default=SourceType.MANUAL, nullable=False)
    
    # Verification Data
    evidence_image_path = Column(String, nullable=True) # Path to MinIO object
    
    # Audit
    added_by_user_id = Column(Integer, ForeignKey("data.users.id"))
    created_at = Column(DateTime, default=datetime.utcnow)

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

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, nullable=False)
    description = Column(Text, nullable=True)
    
    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__ = {"schema": "data"}

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