# robot_monitoring_tools/tools/battery_tool.py
from langchain.tools import BaseTool
from typing import List, Type
import requests
from .schemas import LastTransferDateRequest, LastTransferDateResponse
from .api_schemas import RobotStatusRequest, RobotStatusResponse
from langchain.tools import BaseTool
from pydantic import BaseModel
from datetime import datetime

class GetRobotLastTransferDateTool(BaseTool):
    name : str = "get_robot_last_transfer_date"
    description : str = "collect_date 까지 데이터가 전송된 로봇 중 가장 최근의 것을 확인합니다. 특별한 요청 사항이 없으면 today_date를 기준으로 확인합니다."
    args_schema : Type[BaseModel] = LastTransferDateRequest
    def _run(self, collect_date: datetime) -> List[LastTransferDateResponse]:
        result = []
        url = "http://localhost:8000/robots/status"
        if collect_date is None:
            collect_date_str = datetime.now()
        collect_date = datetime.now()
        collect_date_str = collect_date.strftime("%Y-%m-%d %H:%M:%S")
        payload = RobotStatusRequest(
            columnName = "collect_date",
            collect_date = collect_date_str,
        ).model_dump()
        response = requests.post(url, json=payload)
        if response.status_code == 200:
            for item in response.json():
                response_item = RobotStatusResponse(**item)
                result_item = LastTransferDateResponse(
                    robot_name = response_item.robot_name,
                    serial_number = response_item.serial_number,
                    collect_date = response_item.collect_date
                )
                result.append(result_item)
            return result
        return []

    async def _arun(self):
        raise NotImplementedError("비동기 실행은 지원하지 않습니다")
