from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from .. import models, schemas
from ..database import get_db

router = APIRouter(prefix="/api/threats", tags=["threats"])


@router.get("", response_model=list[schemas.ThreatOut])
def list_threats(db: Session = Depends(get_db)):
    return db.query(models.Threat).order_by(models.Threat.id).all()


@router.get("/{threat_id}", response_model=schemas.ThreatOut)
def get_threat(threat_id: int, db: Session = Depends(get_db)):
    threat = db.get(models.Threat, threat_id)
    if threat is None:
        raise HTTPException(status_code=404, detail="Tahdid topilmadi")
    return threat
