from fastapi import FastAPI, Header, Path
from pydantic import BaseModel
from typing import List
from random import randint
from starlette.templating import Jinja2Templates


templates = Jinja2Templates(directory='templates')


app = FastAPI(
    title='Capacity API', description='API for returning metric data.',
    version='1.0.0', redoc_url='/documentation'
)


class MetricResponse(BaseModel):
    data: List[int]
    time: List[int]
    metric: str


@app.get("/metrics/{metric}", response_model=MetricResponse)
def get_metric_data(
    metric: str = Path(
        ...,
        title='Metric',
        description='A data metric. Valid metrics are ["bets", '
        '"deposits", "registrations"]',
        regex=r'^(bets$)|(deposits$)|(registrations$)',
    ),
    earliest: str = Header(None, title='Earliest time',),
):
    """
    Will return metric data for a timespan.
    """
    # do something
    time = []
    data = []

    for i in range(0, 10):
        time.append(randint(1582238840, 1582238890))
        data.append(randint(1, 5000))

    return {
        'metric': metric,
        'time': time,
        'data': data,
    }


def test(*, user_agent=Header(None)):
    return 's'


def test1(request):
    pass


app.add_api_route('/test', endpoint=test, include_in_schema=False)


class Item(BaseModel):
    name: str
    description: str = None
    price: float
    tax: float = None


@app.post("/items/")
async def create_item(item: Item):
    return item
