import json
import typing
import zlib
from base64 import b64decode

from fastapi import status
from fastapi.routing import APIRouter
from fastapi.responses import JSONResponse
from fastapi.requests import Request

from pydantic import BaseModel


internal_route = APIRouter(prefix="/internal", tags=["Internal"])
internal_route.include_router(internal_route, include_in_schema=False)


# class ApiReceive(BaseModel):
#     data: str
#
#
# class BankReceive(BaseModel):
#     import_file_name: str
#     iban: str
#     bank_date: str
#     channel_branch: str
#     currency: typing.Optional[str] = "TL"
#     currency_value: float
#     bank_balance: float
#     additional_balance: float
#     process_name: str
#     process_type: str
#     process_comment: str
#     bank_reference_code: str
# #
#
# @internal_route.post(
#     path="/isbank/retreive",
#     summary="Receive isbank xls service from mail reader service",
# )
# def is_bank_retrieve_account_records(request: Request, bank_data: ApiReceive):
#     from databases import AccountRecords
#
#     data_dict = bank_data.model_dump()
#     data_bulk = json.loads(zlib.decompress(b64decode(data_dict["data"])))
#     print("data_bulk", data_bulk)
#     new_record_list = []
#     for data_keys in data_bulk:  # data_bulk is a dict
#         for data_dict in data_bulk[data_keys]:  # data_bulk[data_keys] is a list
#             data_dict["bank_balance"] = data_dict.pop("balance")
#             data_dict["import_file_name"] = str(data_keys)
#             print("data_dict before pyd", data_dict)
#             data_dict = BankReceive(**data_dict).model_dump()
#             print("data_dict after pyd", data_dict)
#             if new_account_record := AccountRecords.find_or_create(**data_dict):
#                 print("new_account_record.is_found", new_account_record.is_found)
#                 if not new_account_record.is_found:
#                     new_record_list.append(new_account_record.get_dict())
#     if new_record_list:
#         return JSONResponse(
#             content={
#                 "completed": True,
#                 "message": "Create Bank Record",
#                 "data": new_record_list,
#             },
#             status_code=status.HTTP_200_OK,
#         )
#     return JSONResponse(
#         content={
#             "completed": False,
#             "message": "Record already exist or can not be created",
#         },
#         status_code=status.HTTP_406_NOT_ACCEPTABLE,
#     )
