#!/usr/bin/env python3
from pathlib import Path
import sys

APP_ROOT = Path(__file__).resolve().parents[2]
if str(APP_ROOT) not in sys.path:
    sys.path.insert(0, str(APP_ROOT))

from tool_runtime_helpers import create_plan, generate_task_id
from tool_server_lite.tools.file_tools import BaseTool

class CheapClawSchedulePlanTool(BaseTool):
    name = "cheapclaw_schedule_plan"
    def execute(self, task_id, parameters):
        name = str(parameters.get("name") or "").strip()
        scope = str(parameters.get("scope") or "").strip()
        if not name or not scope:
            return {"status": "error", "output": "", "error": "name and scope are required"}
        resolved_task_id = str(parameters.get("task_id") or "").strip()
        requested_agent_system = str(parameters.get("agent_system") or "").strip() or "CheapClawWorkerScheduled"
        requested_agent_name = str(parameters.get("agent_name") or "").strip() or "scheduled_worker"
        channel = str(parameters.get("channel") or "").strip()
        conversation_id = str(parameters.get("conversation_id") or "").strip()
        task_name = str(parameters.get("task_name") or "").strip()
        bootstrap_user_input = str(parameters.get("bootstrap_user_input") or parameters.get("user_input") or "").strip()

        if scope == "task" and not resolved_task_id:
            if not channel or not conversation_id or not task_name:
                return {
                    "status": "error",
                    "output": "",
                    "error": "for scope=task without task_id, channel, conversation_id and task_name are required",
                }
            resolved_task_id = generate_task_id(channel, conversation_id, task_name)
        elif scope == "task" and resolved_task_id:
            if (channel and not conversation_id) or (conversation_id and not channel):
                return {
                    "status": "error",
                    "output": "",
                    "error": "for scope=task, channel and conversation_id must be provided together",
                }
        plan = create_plan(
            name=name,
            scope=scope,
            task_id=resolved_task_id,
            channel=str(parameters.get("channel") or "").strip(),
            conversation_id=str(parameters.get("conversation_id") or "").strip(),
            interval_sec=int(parameters.get("interval_sec") or 0),
            once_at=str(parameters.get("once_at") or "").strip(),
            schedule_type=str(parameters.get("schedule_type") or "").strip(),
            time_of_day=str(parameters.get("time_of_day") or "").strip(),
            days_of_week=parameters.get("days_of_week") or [],
            message=str(parameters.get("message") or "").strip(),
            agent_system=requested_agent_system if scope == "task" else "",
            agent_name=requested_agent_name if scope == "task" else "",
            task_name=task_name if scope == "task" else "",
            display_name=str(parameters.get("display_name") or "").strip() if scope == "task" else "",
            bootstrap_user_input=bootstrap_user_input if scope == "task" else "",
            enabled=bool(parameters.get("enabled", True)),
        )
        return {"status": "success", "output": f"created plan {plan['plan_id']}", "plan": plan, "task_id": resolved_task_id}
