from __future__ import annotations

from sqlalchemy import ForeignKey, Index, Integer, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.db.base import Base


class UserOrg(Base):
    __tablename__ = "user_org"
    __table_args__ = (
        UniqueConstraint("user_id", "org_unit_id"),
        Index("ix_v4_user_org_org", "org_unit_id"),
        Index("ix_v4_user_org_user", "user_id"),
        {"schema": "v3"},
    )

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("v3.app_user.id", ondelete="CASCADE"))
    org_unit_id: Mapped[int] = mapped_column(Integer, ForeignKey("v3.org_unit.id", ondelete="CASCADE"))

    user: Mapped[AppUser] = relationship("AppUser", back_populates="user_orgs", lazy="selectin")
    org_unit: Mapped[OrgUnit] = relationship("OrgUnit", back_populates="user_orgs", lazy="selectin")
