from typing import Any

from api_initializer.event_clusters import Event
from api_validations.response import (
    PaginateOnly, 
    Pagination, 
    PaginationResult, 
    PostgresResponseSingle, 
    PostgresResponse, 
    EndpointResponse
)
from api_validations.token.validations import TokenDictType
from schemas import (
    Build,
    BuildLivingSpace,
    BuildParts,
    People,
)


# List all endpoint Super User
SuperPeopleListEvent = Event(
    name="super_people_list",
    key="0f8a8b7f-0615-4507-916b-030d48cb5c1d",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super People List all flat representative users endpoint",
)

# Create endpoint Super User
SuperPeopleCreateEvent = Event(
    name="super_people_create",
    key="e18657b7-7a5a-43b8-b43a-422cbc783326",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super People Create flat representative users endpoint",
)

# Update endpoint Super User
SuperPeopleUpdateEvent = Event(
    name="super_people_update",
    key="02a774aa-1f7d-472b-98f1-7b4a58d43e31",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super People Update flat representative users endpoint",
)

# Delete endpoint Super User
SuperPeopleDeleteEvent = Event(
    name="super_people_delete",
    key="b56fd146-b11a-466a-84c9-4c72fb0b9ffa",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super People Delete flat representative users endpoint",
)


def super_people_list_callable(list_options: PaginateOnly, token: TokenDictType):
    list_options = PaginateOnly(**list_options.model_dump())
    if token.is_employee:
        raise Exception("Forbidden for employees")

    # TODO: Pydantic Model must be implemnted for list_options.query
    with People.new_session() as db_session:
        People.set_session(db_session)
        db_session.query(
        ).join(BuildParts, BuildParts.id == BuildLivingSpace.build_parts_id
        ).join(People, People.person_id == BuildLivingSpace.people_id
        ).filter()
        if list_options.query:
            people_list = People.query.filter(*People.convert(list_options.query))
        else:
            people_list = People.query.filter()
        
        pagination = Pagination(data=people_list)
        pagination.change(**list_options.model_dump())
        pagination_result = PaginationResult(data=people_list, pagination=pagination)
        return EndpointResponse(message="MSG0003-LIST", pagination_result=pagination_result).response


SuperPeopleListEvent.event_callable = super_people_list_callable


def super_people_create_callable(data: People, token: TokenDictType):
    return {
        "message": "MSG0001-INSERT",
        "data": None,
        "completed": True,
    }


SuperPeopleCreateEvent.event_callable = super_people_create_callable


def super_people_update_callable(data: People, token: TokenDictType):
    return {
        "message": "MSG0002-UPDATE",
        "data": None,
        "completed": True,
    }


SuperPeopleUpdateEvent.event_callable = super_people_update_callable


def super_people_delete_callable(data: People, token: TokenDictType):
    return {
        "message": "MSG0004-DELETE",
        "data": None,
        "completed": True,
    }


SuperPeopleDeleteEvent.event_callable = super_people_delete_callable
