from typing import Any

from api_initializer.event_clusters import Event
from api_validations.response import (
    PaginateOnly, 
    Pagination, 
    PaginationResult, 
    PostgresResponseSingle, 
    PostgresResponse, 
    EndpointResponse
)
from schemas import (
    BuildArea,
    Build,
    BuildParts,
    AccountRecords,
)
from api_validations.defaults.validations import CommonHeaders


# List all area Super User
SuperAreaListEvent = Event(
    name="super_area_list",
    key="0d5ba7be-028c-43ce-9a99-f495f812a835",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super Area List all endpoint",
)

# Create area Super User
SuperAreaCreateEvent = Event(
    name="super_area_create",
    key="0ba2a06d-f4fa-47b9-a305-2225414ffc4a",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super Area Create endpoint",
)

# Update area Super User
SuperAreaUpdateEvent = Event(
    name="super_area_update",
    key="ecec956a-eadf-4556-b4e1-4ee81a6b8fb2",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super Area Update endpoint",
)

# Delete area Super User
SuperAreaDeleteEvent = Event(
    name="super_area_delete",
    key="4b2ca548-4113-4942-8a76-1f4337fba98a",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super Area Delete endpoint",
)


def super_area_list_callable(list_options: PaginateOnly, headers: CommonHeaders):
    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 AccountRecords.new_session() as db_session:
        AccountRecords.set_session(db_session)
        list_of_fields = [
            AccountRecords.iban,
            AccountRecords.bank_date,
            AccountRecords.currency,
            AccountRecords.currency_value,
            AccountRecords.process_comment,
            AccountRecords.add_comment_note,
            AccountRecords.receive_debit,
            AccountRecords.is_email_send,
            AccountRecords.is_notification_send,
        ]
        account_records_query = db_session.query(*list_of_fields
        ).join(BuildParts, BuildParts.id == AccountRecords.build_parts_id
        ).filter(BuildParts.id == token.selected_occupant.build_part_id)
        if list_options.query:
            account_records_query = account_records_query.filter(*AccountRecords.convert(list_options.query))

        pagination = Pagination(data=account_records_query)
        pagination.change(**list_options.model_dump())
        pagination_result = PaginationResult(data=account_records_query, pagination=pagination)
        return EndpointResponse(message="MSG0003-LIST", pagination_result=pagination_result).response


SuperAreaListEvent.event_callable = super_area_list_callable


def super_area_create_callable(data, headers: CommonHeaders):
    return {
        "message": "MSG0001-INSERT",
        "data": data,
        "completed": True,
    }


SuperAreaCreateEvent.event_callable = super_area_create_callable


def super_area_update_callable(data, headers: CommonHeaders):
    return {
        "message": "MSG0002-UPDATE",
        "data": data,
        "completed": True,
    }


SuperAreaUpdateEvent.event_callable = super_area_update_callable


def super_area_delete_callable(uu_id: str, headers: CommonHeaders):
    return {
        "message": "MSG0003-DELETE",
        "data": data,
        "completed": True,
    }


SuperAreaDeleteEvent.event_callable = super_area_delete_callable
