from uuid import UUID

from sqlalchemy import event
from sqlalchemy.schema import Column, ForeignKey, UniqueConstraint
from sqlmodel import Field, Relationship
from sqlmodel.sql.sqltypes import GUID

from joj.horse.models.base import DomainURLORMModel, url_pre_save
from joj.horse.models.domain import Domain
from joj.horse.schemas.domain_invitation import DomainInvitationDetail


class DomainInvitation(DomainURLORMModel, DomainInvitationDetail, table=True):  # type: ignore[call-arg]
    __tablename__ = "domain_invitations"
    __table_args__ = (
        UniqueConstraint("domain_id", "url"),
        UniqueConstraint("domain_id", "code"),
    )

    domain_id: UUID = Field(
        sa_column=Column(
            GUID, ForeignKey("domains.id", ondelete="CASCADE"), nullable=False
        )
    )
    domain: "Domain" = Relationship(back_populates="invitations")


event.listen(DomainInvitation, "before_insert", url_pre_save)
event.listen(DomainInvitation, "before_update", url_pre_save)

sqlmodel.Relationship sqlalchemy.schema.ForeignKey sqlalchemy.schema.UniqueConstraint sqlalchemy.event.listen
[((869, 930), 'sqlalchemy.event.listen', 'event.listen', (['DomainInvitation', '"""before_insert"""', 'url_pre_save'], {}), "(DomainInvitation, 'before_insert', url_pre_save)\n", (881, 930), False, 'from sqlalchemy import event\n'), ((931, 992), 'sqlalchemy.event.listen', 'event.listen', (['DomainInvitation', '"""before_update"""', 'url_pre_save'], {}), "(DomainInvitation, 'before_update', url_pre_save)\n", (943, 992), False, 'from sqlalchemy import event\n'), ((824, 866), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""invitations"""'}), "(back_populates='invitations')\n", (836, 866), False, 'from sqlmodel import Field, Relationship\n'), ((559, 595), 'sqlalchemy.schema.UniqueConstraint', 'UniqueConstraint', (['"""domain_id"""', '"""url"""'], {}), "('domain_id', 'url')\n", (575, 595), False, 'from sqlalchemy.schema import Column, ForeignKey, UniqueConstraint\n'), ((605, 642), 'sqlalchemy.schema.UniqueConstraint', 'UniqueConstraint', (['"""domain_id"""', '"""code"""'], {}), "('domain_id', 'code')\n", (621, 642), False, 'from sqlalchemy.schema import Column, ForeignKey, UniqueConstraint\n'), ((724, 768), 'sqlalchemy.schema.ForeignKey', 'ForeignKey', (['"""domains.id"""'], {'ondelete': '"""CASCADE"""'}), "('domains.id', ondelete='CASCADE')\n", (734, 768), False, 'from sqlalchemy.schema import Column, ForeignKey, UniqueConstraint\n')]