from datetime import datetime

from sqlalchemy import DateTime, ForeignKey, Integer, JSON, String, Text, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.db.database import Base


class KnowledgeManufacturer(Base):
    __tablename__ = "knowledge_manufacturers"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(160), unique=True, index=True)
    slug: Mapped[str] = mapped_column(String(180), unique=True, index=True)
    website: Mapped[str] = mapped_column(String(255), default="", server_default="")
    notes: Mapped[str] = mapped_column(Text, default="", server_default="")
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    devices: Mapped[list["KnowledgeDevice"]] = relationship(back_populates="manufacturer", cascade="all, delete-orphan")
    documents: Mapped[list["KnowledgeDocument"]] = relationship(back_populates="manufacturer")
    notes_rel: Mapped[list["KnowledgeNote"]] = relationship(back_populates="manufacturer")


class KnowledgeDevice(Base):
    __tablename__ = "knowledge_devices"
    __table_args__ = (
        UniqueConstraint("manufacturer_id", "slug", name="uq_knowledge_devices_manufacturer_slug"),
    )

    id: Mapped[int] = mapped_column(primary_key=True)
    manufacturer_id: Mapped[int] = mapped_column(ForeignKey("knowledge_manufacturers.id", ondelete="CASCADE"), index=True)
    name: Mapped[str] = mapped_column(String(180), index=True)
    slug: Mapped[str] = mapped_column(String(200), index=True)
    model_number: Mapped[str] = mapped_column(String(120), default="", server_default="", index=True)
    device_type: Mapped[str] = mapped_column(String(80), default="", server_default="", index=True)
    frequency_range: Mapped[str] = mapped_column(String(120), default="", server_default="")
    production_year_from: Mapped[int | None] = mapped_column(Integer, nullable=True)
    production_year_to: Mapped[int | None] = mapped_column(Integer, nullable=True)
    notes: Mapped[str] = mapped_column(Text, default="", server_default="")
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    manufacturer: Mapped[KnowledgeManufacturer] = relationship(back_populates="devices")
    documents: Mapped[list["KnowledgeDocument"]] = relationship(back_populates="device")
    notes_rel: Mapped[list["KnowledgeNote"]] = relationship(back_populates="device")


class KnowledgeDocument(Base):
    __tablename__ = "knowledge_documents"

    id: Mapped[int] = mapped_column(primary_key=True)
    manufacturer_id: Mapped[int] = mapped_column(ForeignKey("knowledge_manufacturers.id", ondelete="RESTRICT"), index=True)
    device_id: Mapped[int | None] = mapped_column(ForeignKey("knowledge_devices.id", ondelete="SET NULL"), nullable=True, index=True)
    title: Mapped[str] = mapped_column(String(255), index=True)
    slug: Mapped[str] = mapped_column(String(280), unique=True, index=True)
    document_type: Mapped[str] = mapped_column(String(80), index=True)
    language: Mapped[str] = mapped_column(String(20), default="de", server_default="", index=True)
    file_name: Mapped[str] = mapped_column(String(255), default="", server_default="")
    file_path: Mapped[str] = mapped_column(String(500), default="", server_default="")
    mime_type: Mapped[str] = mapped_column(String(120), default="", server_default="")
    file_size: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
    checksum_sha256: Mapped[str] = mapped_column(String(64), default="", server_default="", index=True)
    external_url: Mapped[str] = mapped_column(String(500), default="", server_default="")
    paperless_document_id: Mapped[str] = mapped_column(String(120), default="", server_default="", index=True)
    description: Mapped[str] = mapped_column(Text, default="", server_default="")
    tags: Mapped[list[str]] = mapped_column(JSON, default=list, server_default="[]")
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    manufacturer: Mapped[KnowledgeManufacturer] = relationship(back_populates="documents")
    device: Mapped[KnowledgeDevice | None] = relationship(back_populates="documents")


class KnowledgeNote(Base):
    __tablename__ = "knowledge_notes"

    id: Mapped[int] = mapped_column(primary_key=True)
    manufacturer_id: Mapped[int | None] = mapped_column(ForeignKey("knowledge_manufacturers.id", ondelete="SET NULL"), nullable=True, index=True)
    device_id: Mapped[int | None] = mapped_column(ForeignKey("knowledge_devices.id", ondelete="SET NULL"), nullable=True, index=True)
    title: Mapped[str] = mapped_column(String(255), index=True)
    content: Mapped[str] = mapped_column(Text)
    note_type: Mapped[str] = mapped_column(String(80), index=True)
    severity: Mapped[str] = mapped_column(String(40), default="", server_default="", index=True)
    tags: Mapped[list[str]] = mapped_column(JSON, default=list, server_default="[]")
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    manufacturer: Mapped[KnowledgeManufacturer | None] = relationship(back_populates="notes_rel")
    device: Mapped[KnowledgeDevice | None] = relationship(back_populates="notes_rel")


class KnowledgeCategory(Base):
    __tablename__ = "knowledge_categories"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(120), unique=True, index=True)
    slug: Mapped[str] = mapped_column(String(140), unique=True, index=True)
    description: Mapped[str] = mapped_column(Text, default="", server_default="")
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
