from fastapi import FastAPI

from utils.spider import Walmart
from utils.proxy import start_session

app = FastAPI()
gateway, session = start_session('https://www.walmart.com/')


@app.get("/")
async def root():
    return {"message": "Nothing much here, really...!"}


@app.get("/productinfo/{sku}")
async def get_product_info(sku: int):
    p = Walmart(sku, session)
    results = p.get_product_info()
    return results


@app.get("/productreview/{sku}/{page}")
async def get_product_review(sku: int, page: int):
    p = Walmart(sku, session)
    reviews, stats = p.get_product_reviews(sku, page)
    reviews = [r['review'] for r in reviews]
    data = {
        'stats': stats,
        'reviews': reviews
    }
    return data
