"""Engagement model."""
from __future__ import annotations

import enum
from datetime import UTC, datetime

from backend.app.extensions import db


class EngagementStatus(str, enum.Enum):
    PLANNED = "planned"
    ACTIVE = "active"
    CLOSED = "closed"


class Engagement(db.Model):  # type: ignore[name-defined]
    __tablename__ = "engagements"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    description = db.Column(db.Text, nullable=True)
    start_date = db.Column(db.Date, nullable=False)
    end_date = db.Column(db.Date, nullable=True)
    status = db.Column(
        db.Enum(EngagementStatus, name="engagement_status"),
        nullable=False,
        default=EngagementStatus.PLANNED,
    )
    created_at = db.Column(
        db.DateTime, nullable=False, default=lambda: datetime.now(UTC)
    )
    created_by_id = db.Column(
        db.Integer, db.ForeignKey("users.id", ondelete="RESTRICT"), nullable=False
    )

    created_by = db.relationship("User", backref="engagements", lazy="joined")

    def __repr__(self) -> str:
        return f"<Engagement {self.id} {self.name!r}>"
