from utils.db_connect import BaseModelForTable
from sqlalchemy import (
    Column,
    String,
    Enum as SQLEnum,
    UUID,
    ForeignKey,
    Boolean
)
from sqlalchemy.orm import relationship
from enum import Enum


class PlatformRole(str, Enum):
    SUPER_ADMIN = "SUPER_ADMIN"
    ADMIN= "ADMIN"
    USER = "USER"


class OrgRole(str, Enum):
    ORG_ADMIN = "ORG_ADMIN"
    MANAGER = "MANAGER"
    MEMBER = "MEMBER"


class User(BaseModelForTable):
    __tablename__ = "users"

    name = Column(String, nullable=False)

    email = Column(
        String,
        nullable=False,
        unique=True,
        index=True
    )

    password = Column(String(256), nullable=False)

    org_id = Column(
        UUID(as_uuid=True),
        ForeignKey("organizations.id", ondelete="CASCADE"),
        index=True,
        nullable=True
    )

    platform_role = Column(
        SQLEnum(PlatformRole),
        default=PlatformRole.USER,
        nullable=False
    )

    org_role = Column(
        SQLEnum(OrgRole),
        default=OrgRole.MEMBER,
        nullable=False
    )

    is_active = Column(
        Boolean,
        nullable=False,
        default=True
    )

    is_verified = Column(
        Boolean,
        nullable=False,
        default=False
    )

    # Relationships

    owned_organizations = relationship(
        "Organization",
        back_populates="owner",
        foreign_keys="Organization.owner_id"
    )

    organization = relationship(
        "Organization",
        foreign_keys=[org_id]
    )

    reviewed_requests = relationship(
        "OrganizationRegistrationRequest",
        back_populates="request_reviewer",
        foreign_keys="OrganizationRegistrationRequest.reviewed_by"
    )

    sent_invites = relationship(
        "OrganizationUserInvite",
        back_populates="inviter"
    )


    created_projects = relationship(
        "Project",
        back_populates="creator",
        foreign_keys="Project.created_by"
    )

    created_labels = relationship(
        "Label",
        back_populates="creator",
        foreign_keys="Label.created_by"
    )

    reported_tickets = relationship("Ticket", back_populates="reporter", foreign_keys="Ticket.reporter_id")

    comments = relationship(
        "Comment",
        back_populates="author",
        cascade="all, delete-orphan"
    )

    activity_logs = relationship(
        "ActivityLog",
        back_populates="actor",
        cascade="all, delete-orphan"
    )

    notifications = relationship(
        "Notification",
        back_populates="user",
        cascade="all, delete-orphan"
    )

    job_creator=relationship(
        "JobService",
        back_populates="reporter",
        cascade="all, delete-orphan"
    )

    def to_dict(self):
        return {
            "id": str(self.id),
            "name": self.name,
            "email": self.email,
            "platform_role": self.platform_role.value,
            "org_role": self.org_role.value,

            "org": {
                "id": str(self.organization.id) if self.organization else None,
                "name": self.organization.name if self.organization else None,
                "slug": self.organization.slug if hasattr(self.organization, "slug") else None,
                "status": self.organization.status if self.organization else None,
            } if self.organization else None
        }