from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.tysc import Vendor
from src.db.session import SessionDep
from src.schema.tysc.vendor import VendorResponse, to_response


router = APIRouter()

@router.get("/vendors")
def get_all_vendors(db: SessionDep) -> List[VendorResponse]:
    """
    retrieve all vendors as json response.
    """
    results: list[VendorResponse] = []
    vendors = db.query(Vendor).all()
    for vendor in vendors:
        response = to_response(vendor)
        results.append(response)
    return results

@router.get("/vendors/{vendor_id}", response_model=VendorResponse)
def get_vendor(vendor_id: str, db: SessionDep) -> VendorResponse:
    """
    retrieve vendor by id as json response.
    """
    vendor = db.get(Vendor, vendor_id)
    if vendor is None:
        raise HTTPException(status_code=404, detail="Vendor could not be found")
    response = to_response(vendor)
    return response
