from sqlalchemy import Column, Integer, String, Boolean, DateTime, Enum
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base
import enum


class UserRole(str, enum.Enum):
    DIRECTOR = "director"
    COORDINATOR = "coordinator"
    ARTIST = "artist"
    DEVELOPER = "developer"


class DepartmentRole(str, enum.Enum):
    LAYOUT = "layout"
    ANIMATION = "animation"
    LIGHTING = "lighting"
    COMPOSITE = "composite"
    MODELING = "modeling"
    RIGGING = "rigging"
    SURFACING = "surfacing"


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String, unique=True, index=True, nullable=False)
    password_hash = Column(String, nullable=False)
    first_name = Column(String, nullable=False)
    last_name = Column(String, nullable=False)
    role = Column(Enum(UserRole), nullable=False, default=UserRole.ARTIST)
    is_admin = Column(Boolean, default=False, nullable=False)
    is_approved = Column(Boolean, default=False, nullable=False)
    avatar_url = Column(String, nullable=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    # Relationships
    project_memberships = relationship("ProjectMember", back_populates="user", cascade="all, delete-orphan")
    assigned_tasks = relationship("Task", foreign_keys="Task.assigned_user_id", back_populates="assigned_user")
    submissions = relationship("Submission", foreign_keys="Submission.user_id", back_populates="user")
    reviews = relationship("Review", foreign_keys="Review.reviewer_id", back_populates="reviewer")
    production_notes = relationship("ProductionNote", foreign_keys="ProductionNote.user_id", back_populates="user")
    task_attachments = relationship("TaskAttachment", foreign_keys="TaskAttachment.user_id", back_populates="user")
    api_keys = relationship("APIKey", back_populates="user", cascade="all, delete-orphan")
    notifications = relationship("Notification", back_populates="user", cascade="all, delete-orphan")
    notification_preferences = relationship("UserNotificationPreference", back_populates="user", uselist=False, cascade="all, delete-orphan")

    def __repr__(self):
        return f"<User(id={self.id}, email='{self.email}', role='{self.role}')>"