from fastapi import FastAPI, Request, Depends
from fastapi.responses import HTMLResponse
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession
from retailtwin.api.db import get_inventory, get_session, get_stores

app = FastAPI(root_path="/api/v1")


@app.get("/test")
async def test(request: Request):
    return {"message": "Hello World", "root_path": request.scope.get("root_path")}


@app.get("/stock", response_class=HTMLResponse)
async def stock(
    request: Request,
    store: int = 1,
    page: int = 0,
    pagesize: int = 20,
    sortby: str = "upc",
    async_session: async_sessionmaker[AsyncSession] = Depends(get_session),
) -> str:
    body = await get_inventory(
        store=store,
        page=page,
        pagesize=pagesize,
        sortby=sortby,
        root_path=request.scope.get("root_path"),
        async_session=async_session,
    )
    return HTMLResponse(body, status_code=200)


@app.get("/stores", response_class=HTMLResponse)
async def stores(
    request: Request,
    async_session: async_sessionmaker[AsyncSession] = Depends(get_session),
):
    body = await get_stores(
        root_path=request.scope.get("root_path"),
        async_session=async_session,
    )
    return HTMLResponse(body, status_code=200)
