from pipecat.services.llm_service import FunctionCallParams

from app_resources import AppResources
from use_cases.book_appointments import BookAppointmentsUseCase


class BookAppointmentTool:
    def __init__(self, use_case: BookAppointmentsUseCase):
        self._use_case = use_case

    async def __call__(self, params: FunctionCallParams) -> None:
        resources: AppResources = params.app_resources
        if not resources.client_id:
            await params.result_callback(
                {"error": "Client could not be identified for this call."}
            )
            return

        args = dict(params.arguments)
        service_names = args.get("serviceNames") or []
        requested_start = args.get("requestedStart") or {}
        offer_title = args.get("offer_title")

        if not service_names:
            await params.result_callback({"error": "No services provided."})
            return

        if not requested_start.get("date") or not requested_start.get("time"):
            await params.result_callback(
                {"error": "requestedStart.date and requestedStart.time are required."}
            )
            return

        result = await self._use_case.execute(
            salon_id=resources.salon_id,
            client_id=resources.client_id,
            timezone=resources.timezone,
            service_names=service_names,
            requested_start=requested_start,
            offer_title=offer_title,
        )
        await params.result_callback(result)
