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

from database import Base


class Branche(Base):
    __tablename__ = "branche"

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100), unique=True, nullable=False)
    sortierung = Column(Integer, default=0)

    kontakte = relationship("KontaktBranche", back_populates="branche", cascade="all, delete-orphan")


class KontaktBranche(Base):
    __tablename__ = "kontakt_branche"

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

    kontakt = relationship("Kontakt", back_populates="branchen")
    branche = relationship("Branche", back_populates="kontakte")
