import uuid
from sqlalchemy import Column, String, Boolean, DateTime, Text, ForeignKey, Index
from sqlalchemy.orm import relationship
from sqlalchemy import Enum as SqlEnum
from utils.utils import Base, utc_now, UserRole


class User(Base):
    __tablename__ = "users"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    email = Column(String(255), nullable=False, unique=True, index=True)
    password_hash = Column(String(255), nullable=False)
    first_name = Column(String(100), nullable=False)
    last_name = Column(String(100), nullable=False)
    role = Column(SqlEnum(UserRole, name="userrole_enum"), nullable=False)
    department_id = Column(String(36), ForeignKey("departments.id", ondelete="SET NULL"), nullable=True, index=True)
    phone = Column(String(50), nullable=True)
    is_active = Column(Boolean, nullable=False, default=True)
    created_at = Column(DateTime(timezone=True), default=utc_now, nullable=False)
    updated_at = Column(DateTime(timezone=True), default=utc_now, onupdate=utc_now, nullable=False)

    # Relationships
    department = relationship("Department", back_populates="users")
    user_teams = relationship("Userteam", back_populates="user", cascade="all, delete-orphan", passive_deletes=True)
    created_jobs = relationship("Job", foreign_keys="[Job.created_by_user_id]", back_populates="created_by_user")
    assigned_jobs = relationship("Job", foreign_keys="[Job.assigned_to_user_id]", back_populates="assigned_to_user")
    comments = relationship("Comment", back_populates="user", cascade="all, delete-orphan", passive_deletes=True)
    attachments = relationship("Attachment", back_populates="uploaded_by_user", cascade="all, delete-orphan", passive_deletes=True)
    time_logs = relationship("Timelog", back_populates="user", cascade="all, delete-orphan", passive_deletes=True)
    job_history_entries = relationship("Jobhistory", back_populates="user", cascade="all, delete-orphan", passive_deletes=True)
    assigned_checklist_items = relationship("ChecklistItem", back_populates="assigned_to_user")
    notifications = relationship("Notification", back_populates="user", cascade="all, delete-orphan", passive_deletes=True)
    reports = relationship("Report", back_populates="generated_by_user", cascade="all, delete-orphan", passive_deletes=True)


class Notification(Base):
    __tablename__ = "notifications"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    user_id = Column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
    type = Column(String(100), nullable=False)
    title = Column(String(500), nullable=False)
    message = Column(Text, nullable=False)
    related_entity_type = Column(String(100), nullable=True)
    related_entity_id = Column(String(36), nullable=True, index=True)
    is_read = Column(Boolean, nullable=False, default=False)
    read_at = Column(DateTime(timezone=True), nullable=True)
    created_at = Column(DateTime(timezone=True), default=utc_now, nullable=False)
    updated_at = Column(DateTime(timezone=True), default=utc_now, onupdate=utc_now, nullable=False)

    # Relationships
    user = relationship("User", back_populates="notifications")

    __table_args__ = (
        Index("ix_notifications_user_is_read", "user_id", "is_read"),
        Index("ix_notifications_related_entity", "related_entity_type", "related_entity_id"),
    )