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

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

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


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


@router.get("/{network_id}", response_model=schemas.NetworkOut)
def get_network(network_id: int, db: Session = Depends(get_db)):
    network = db.get(models.Network, network_id)
    if network is None:
        raise HTTPException(status_code=404, detail="Tarmoq turi topilmadi")
    return network
