import json
from typing import Literal

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

import elastic
from settings import ELASTIC_CONN, ENROLLMENT_TABLE

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


@router.get('/', compress=True, tags=['Enrollment'])
def get_enrollments():
    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=ENROLLMENT_TABLE,
        page_size=int(page_size),
        query=json.loads(query),
        elastic_client=elastic_client,
    )


@router.get('/<id>', compress=True, tags=['Enrollment'])
def get_enrollment(id: str):
    return {}


class CancelPayload(BaseModel):
    status: Literal['CANCELED'] = 'CANCELED'


@router.patch('/<id>', compress=True, tags=['Enrollment'])
def cancel(id: str, payload: CancelPayload):
    return {}


@router.post('/', compress=True, tags=['Enrollment'])
def enroll():
    return {}
