"""Organization invitation model.

⚠️ AUTO-GENERATED BY PRISM - DO NOT EDIT
"""

from __future__ import annotations

import secrets
from datetime import UTC, datetime, timedelta

from sqlalchemy import DateTime, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from {{ project_name }}.models.base import Base


class OrganizationInvitation(Base):
    """Pending invitation to join an organization."""

    __tablename__ = "organization_invitations"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    organization_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("organizations.id", ondelete="CASCADE"), nullable=False, index=True
    )
    email: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
    role: Mapped[str] = mapped_column(
        String(50), nullable=False, default="{{ default_org_role }}"
    )
    token: Mapped[str] = mapped_column(
        String(255), nullable=False, unique=True, default=lambda: secrets.token_urlsafe(32)
    )
    invited_by_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=lambda: datetime.now(UTC), nullable=False
    )
    expires_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        default=lambda: datetime.now(UTC) + timedelta(hours={{ invitation_expiry_hours }}),
        nullable=False,
    )

    # Relationships
    organization: Mapped[Organization] = relationship(
        "Organization", back_populates="invitations"
    )

    @property
    def is_expired(self) -> bool:
        """Check if the invitation has expired."""
        return datetime.now(UTC) > self.expires_at

    def __repr__(self) -> str:
        return f"<OrgInvitation org={self.organization_id} email={self.email}>"


# Import here to avoid circular imports
from {{ project_name }}.organization.org_model import Organization  # noqa: E402
