"""C2Config model — per-engagement Mythic connection settings."""
from __future__ import annotations

from datetime import UTC, datetime

from backend.app.extensions import db


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

    id = db.Column(db.Integer, primary_key=True)
    engagement_id = db.Column(
        db.Integer,
        db.ForeignKey("engagements.id", ondelete="CASCADE"),
        nullable=False,
        unique=True,
        index=True,
    )
    url = db.Column(db.Text, nullable=False)
    api_token_encrypted = db.Column(db.Text, nullable=False)
    verify_tls = db.Column(db.Boolean, nullable=False, default=False)
    created_at = db.Column(
        db.DateTime, nullable=False, default=lambda: datetime.now(UTC)
    )
    updated_at = db.Column(db.DateTime, nullable=True)

    engagement = db.relationship(
        "Engagement",
        backref=db.backref("c2_config", uselist=False, cascade="all, delete-orphan"),
    )

    def __repr__(self) -> str:
        return f"<C2Config engagement_id={self.engagement_id}>"
