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 (
    Build,
    BuildParts,
    AccountRecords,
)
from api_validations.defaults.validations import CommonHeaders


# List all endpoint FL-REP
SuperBuildListEvent = Event(
    name="build_list",
    key="e8586858-db39-4520-bb1a-338ab9c5f043",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super Build List all endpoint",
)

SuperBuildCreateEvent = Event(
    name="build_create",
    key="79519e0f-c4a6-4afc-a494-d0e547ba39bc",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super Build Create endpoint",
)

SuperBuildUpdateEvent = Event(
    name="build_update",
    key="ca51080e-11f2-46f7-a1ba-caa1c40b3fd6",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super Build Update endpoint",
)

SuperBuildDeleteEvent = Event(
    name="build_delete",
    key="a30d32cc-c931-41d6-8a66-d6c04479098f",
    request_validator=None,  # TODO: Add request validator
    response_validator=None,  # TODO: Add response validator
    description="Super Build Delete endpoint",
)


def super_build_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


SuperBuildListEvent.event_callable = super_build_list_callable


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


SuperBuildCreateEvent.event_callable = super_build_create_callable


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


SuperBuildUpdateEvent.event_callable = super_build_update_callable


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


SuperBuildDeleteEvent.event_callable = super_build_delete_callable
