import uuid
from datetime import datetime, timezone
from sqlalchemy import Column, String, Text, DateTime, ForeignKey, UniqueConstraint
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from database.postgres import Base


def _now():
    return datetime.now(timezone.utc)


class Entry(Base):
    __tablename__ = "crm_entries"

    id          = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    type        = Column(String(10), nullable=False)          # 'note' | 'issue'
    title       = Column(String(500), nullable=False)
    body        = Column(Text, nullable=True)
    status      = Column(String(20), nullable=True)           # null for notes; open/researching/resolved for issues
    severity    = Column(String(10), nullable=True)           # null | low | medium | high | critical
    category    = Column(String(30), nullable=True)           # null for notes; technical | install_support | general
    author_id   = Column(String(128), nullable=False)         # staff user ID from JWT
    author_name = Column(String(255), nullable=True)          # denormalized for display
    created_at  = Column(DateTime(timezone=True), nullable=False, default=_now)
    updated_at  = Column(DateTime(timezone=True), nullable=False, default=_now, onupdate=_now)

    links = relationship("EntryLink", back_populates="entry", cascade="all, delete-orphan", lazy="noload")


class EntryLink(Base):
    __tablename__ = "crm_entry_links"
    __table_args__ = (
        UniqueConstraint("entry_id", "entity_type", "entity_id"),
    )

    id          = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    entry_id    = Column(UUID(as_uuid=True), ForeignKey("crm_entries.id", ondelete="CASCADE"), nullable=False)
    entity_type = Column(String(20), nullable=False)   # 'device' | 'app_user' | 'customer'
    entity_id   = Column(String(128), nullable=False)  # Firestore ID or Postgres UUID as string

    entry = relationship("Entry", back_populates="links")
