"""Engagement ORM model."""

from __future__ import annotations

from datetime import datetime, timezone
from typing import TYPE_CHECKING, Any

from sqlalchemy import DateTime, JSON, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.models.base import Base

if TYPE_CHECKING:
    from app.models.coverage import Coverage
    from app.models.event import Event


class Engagement(Base):
    """Represents a pentest engagement/workspace."""

    __tablename__ = "engagements"

    id: Mapped[int] = mapped_column(primary_key=True, index=True)
    name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
    metadata_json: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True, default=None)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        default=lambda: datetime.now(timezone.utc),
    )

    events: Mapped[list["Event"]] = relationship(
        back_populates="engagement",
        cascade="all, delete-orphan",
    )
    coverage: Mapped["Coverage"] = relationship(
        back_populates="engagement",
        cascade="all, delete-orphan",
        uselist=False,
    )
