"""User model."""
from __future__ import annotations

import enum
from datetime import UTC, datetime

from backend.app.extensions import db


class UserRole(str, enum.Enum):
    ADMIN = "admin"
    REDTEAM = "redteam"
    SOC = "soc"


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

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, nullable=False, index=True)
    password_hash = db.Column(db.String(255), nullable=False)
    role = db.Column(db.Enum(UserRole, name="user_role"), nullable=False)
    created_at = db.Column(
        db.DateTime, nullable=False, default=lambda: datetime.now(UTC)
    )

    def __repr__(self) -> str:
        return f"<User {self.username} ({self.role.value})>"
