"""SimulationTemplate model."""
from __future__ import annotations

from datetime import UTC, datetime

from backend.app.extensions import db


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

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False, unique=True)
    description = db.Column(db.Text, nullable=True)
    commands = db.Column(db.Text, nullable=True)
    prerequisites = db.Column(db.Text, nullable=True)
    techniques = db.Column(db.JSON, nullable=False, default=list)
    tactic_ids = db.Column(db.JSON, nullable=False, default=list)
    created_at = db.Column(
        db.DateTime, nullable=False, default=lambda: datetime.now(UTC)
    )
    updated_at = db.Column(db.DateTime, nullable=True)
    created_by_id = db.Column(
        db.Integer,
        db.ForeignKey("users.id", ondelete="RESTRICT"),
        nullable=False,
    )

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

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