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

class OrgType(str, enum.Enum):
    INDIVIDUAL = "individual"
    SERVICE = "service"
    FLEET_OWNER = "fleet_owner"
    CLUB = "club"

class Organization(Base):
    __tablename__ = "organizations"
    __table_args__ = {"schema": "data"}

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, nullable=False)
    org_type = Column(Enum(OrgType), default=OrgType.INDIVIDUAL)
    
    # A flotta technikai tulajdonosa (User)
    owner_id = Column(Integer, ForeignKey("data.users.id"), nullable=True)
    
    # Üzleti szabályok
    is_active = Column(Boolean, default=True)
    # Privát flotta (INDIVIDUAL) esetén False, cégeknél True
    is_transferable = Column(Boolean, default=True) 
    
    # Verifikáció
    is_verified = Column(Boolean, default=False)
    verification_expires_at = Column(DateTime(timezone=True), nullable=True)

    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # Kapcsolatok
    vehicles = relationship("Vehicle", back_populates="current_org")
    members = relationship("OrganizationMember", back_populates="organization")
    owner = relationship("User", back_populates="owned_organizations")