import json

from aws_lambda_powertools.event_handler.api_gateway import Router
from elasticsearch import Elasticsearch

import elastic
from settings import ELASTIC_CONN, ORDER_TABLE

router = Router()
elastic_client = Elasticsearch(**ELASTIC_CONN)


@router.get('/', compress=True, tags=['Order'])
def get_orders():
    event = router.current_event
    query = event.get_query_string_value('query', '{}')
    page_size = event.get_query_string_value('page_size', '25')

    return elastic.search(  # type: ignore
        index=ORDER_TABLE,
        page_size=int(page_size),
        query=json.loads(query),
        elastic_client=elastic_client,
    )
