from typing import Any
from fastapi import APIRouter, Depends
from sqlalchemy import func
from schemas import AccountRecords

from endpoints.index import endpoints_index
from events.event_endpoints.cluster import EventsEndpointRouterCluster

from api_validations.defaults.validations import CommonHeaders
from api_validations.response.pagination import PaginateOnly
from api_middlewares.token_provider import TokenProvider


test_endpoint_route = APIRouter(prefix="/tests", tags=["Endpoint Tests"])


account_records_all = "AccountRecordsAll"
@test_endpoint_route.get(
    path="/account/records/all",
    description="Account records endpoint",
    operation_id=endpoints_index[account_records_all],
)
def account_records_all_route(headers: CommonHeaders = Depends(CommonHeaders.as_dependency)):
    with AccountRecords.new_session() as db_session:
        AccountRecords.set_session(db_session)
        account_records_positive_list = db_session.query(func.sum(AccountRecords.currency_value)
        ).filter(AccountRecords.currency_value > 0
        ).order_by(AccountRecords.bank_date.desc()).first()
        account_records_negative_list = db_session.query(func.sum(AccountRecords.currency_value)
        ).filter(AccountRecords.currency_value < 0
        ).order_by(AccountRecords.bank_date.desc()).first()
        return {
            "message": "MSG0003-LIST",
            "data": {
                "debt": float(account_records_negative_list),
                "budget": float(account_records_positive_list),
                "total": float(account_records_positive_list) + float(account_records_negative_list),
                "lastPayment": account_records_data.bank_date,
            },
            "completed": True,
        }


account_records_monthly = "AccountRecordsMonthly"
@test_endpoint_route.get(
    path="/account/records/monthly",
    description="Account records endpoint",
    operation_id=endpoints_index[account_records_monthly],
)
def account_records_monthly_route(headers: CommonHeaders = Depends(CommonHeaders.as_dependency)):
    with AccountRecords.new_session() as db_session:
        account_records_positive_list = db_session.query(func.sum(AccountRecords.currency_value)).filter(
            AccountRecords.bank_date_y == datetime.date.today().year,
            AccountRecords.bank_date_m == datetime.date.today().month,
        ).order_by(AccountRecords.bank_date.desc()).first()
        account_records_negative_list = db_session.query(func.sum(AccountRecords.currency_value)).filter(
            AccountRecords.bank_date_y == datetime.date.today().year,
            AccountRecords.bank_date_m == datetime.date.today().month,
        ).order_by(AccountRecords.bank_date.desc()).first()
        return {
            "message": "MSG0003-LIST",
            "data": {
                "debt": float(account_records_negative_list),
                "budget": float(account_records_positive_list),
                "total": float(account_records_positive_list) + float(account_records_negative_list),
                "lastPayment": account_records_data.bank_date,
            },
            "completed": True,
        }
