import uuid
import enum
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, JSON, Numeric, text, Enum, BigInteger
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.sql import func
from app.db.base import Base # <--- JAVÍTVA: base_class helyett base

class UserRole(str, enum.Enum):
    admin = "admin"
    user = "user"
    service = "service"
    fleet_manager = "fleet_manager"
    driver = "driver"

class Person(Base):
    __tablename__ = "persons"
    __table_args__ = {"schema": "data"}

    id = Column(BigInteger, primary_key=True, index=True)
    id_uuid = Column(UUID(as_uuid=True), default=uuid.uuid4, unique=True, nullable=False)
    
    last_name = Column(String, nullable=False)
    first_name = Column(String, nullable=False)
    mothers_name = Column(String, nullable=True)
    birth_place = Column(String, nullable=True)
    birth_date = Column(DateTime, nullable=True)
    
    identity_docs = Column(JSON, server_default=text("'{}'::jsonb"))
    medical_emergency = Column(JSON, server_default=text("'{}'::jsonb"))
    ice_contact = Column(JSON, server_default=text("'{}'::jsonb"))
    
    # Ez a mező kell a 2-lépcsős regisztrációhoz
    is_active = Column(Boolean, default=False, nullable=False)
    
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

    users = relationship("User", back_populates="person")

class User(Base):
    __tablename__ = "users"
    __table_args__ = {"schema": "data"}

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String, unique=True, index=True, nullable=False)
    hashed_password = Column(String, nullable=True)
    
    role = Column(Enum(UserRole), default=UserRole.user)
    is_active = Column(Boolean, default=False)
    region_code = Column(String, default="HU")
    
    is_deleted = Column(Boolean, default=False)
    person_id = Column(BigInteger, ForeignKey("data.persons.id"), nullable=True)
    
    person = relationship("Person", back_populates="users")
    wallet = relationship("Wallet", back_populates="user", uselist=False)
    # Kapcsolat lusta betöltéssel a mapper hiba ellen
    owned_organizations = relationship("Organization", back_populates="owner", lazy="select")
    
    created_at = Column(DateTime(timezone=True), server_default=func.now())

class Wallet(Base):
    __tablename__ = "wallets"
    __table_args__ = {"schema": "data"}

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("data.users.id"), unique=True)
    coin_balance = Column(Numeric(18, 2), default=0.00)
    xp_balance = Column(Integer, default=0)
    
    user = relationship("User", back_populates="wallet")

class VerificationToken(Base):
    __tablename__ = "verification_tokens"
    __table_args__ = {"schema": "data"}

    id = Column(Integer, primary_key=True, index=True)
    token = Column(UUID(as_uuid=True), default=uuid.uuid4, unique=True, nullable=False)
    user_id = Column(Integer, ForeignKey("data.users.id", ondelete="CASCADE"), nullable=False)
    token_type = Column(String(20), nullable=False) # 'registration' or 'password_reset'
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    expires_at = Column(DateTime(timezone=True), nullable=False)
    is_used = Column(Boolean, default=False)