from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from database import Base


class TagKategorie(Base):
    __tablename__ = "tag_kategorie"

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100), unique=True, nullable=False)
    sortierung = Column(Integer, default=0)
    farbe = Column(String(7), default="#607D8B")

    tags = relationship("Tag", back_populates="kategorie", cascade="all, delete-orphan")


class Tag(Base):
    __tablename__ = "tag"

    id = Column(Integer, primary_key=True, autoincrement=True)
    kategorie_id = Column(Integer, ForeignKey("tag_kategorie.id", ondelete="CASCADE"), nullable=False)
    name = Column(String(100), nullable=False)
    cobra_stichwort = Column(String(200))

    kategorie = relationship("TagKategorie", back_populates="tags")
    kontakte = relationship("KontaktTag", back_populates="tag", cascade="all, delete-orphan")


class KontaktTag(Base):
    __tablename__ = "kontakt_tag"

    kontakt_id = Column(Integer, ForeignKey("kontakt.id", ondelete="CASCADE"), primary_key=True)
    tag_id = Column(Integer, ForeignKey("tag.id", ondelete="CASCADE"), primary_key=True)

    kontakt = relationship("Kontakt", back_populates="tags")
    tag = relationship("Tag", back_populates="kontakte")
