use crate::chrome_mcp_handler::ChromeMcpHandler;
use rust_mcp_sdk::{
    macros,
    schema::{CallToolError, CallToolRequestParams, CallToolResult},
};
use serde_json::json;

#[macros::mcp_tool(
    name = "set_breakpoint",
    description = "Set a debugger breakpoint at a specific script, line and column"
)]
#[derive(Debug, ::serde::Deserialize, ::serde::Serialize, macros::JsonSchema)]
pub struct SetBreakpointTool {
    pub script_hash: Option<String>,
    pub script_id: Option<String>,
    pub url: Option<String>,
    pub line_number: u32,
    pub column_number: Option<u32>,
}

impl SetBreakpointTool {
    pub async fn handle(
        params: CallToolRequestParams,
        handler: &ChromeMcpHandler,
    ) -> Result<CallToolResult, CallToolError> {
        let args_value = serde_json::Value::Object(params.arguments.unwrap_or_default());
        let args: SetBreakpointTool = serde_json::from_value(args_value)
            .map_err(|e| CallToolError::from_message(e.to_string()))?;

        let mut client_lock = handler.get_or_connect().await?;
        let cdp_client = client_lock.as_mut().unwrap();

        let response = if let Some(script_id) = args.script_id {
            let mut location = json!({
                "scriptId": script_id,
                "lineNumber": args.line_number,
            });
            if let Some(col) = args.column_number {
                location
                    .as_object_mut()
                    .unwrap()
                    .insert("columnNumber".to_string(), json!(col));
            }
            cdp_client
                .send_raw_command("Debugger.setBreakpoint", json!({ "location": location }))
                .await
        } else if let Some(url) = args.url {
            let mut params = json!({
                "url": url,
                "lineNumber": args.line_number,
            });
            if let Some(col) = args.column_number {
                params
                    .as_object_mut()
                    .unwrap()
                    .insert("columnNumber".to_string(), json!(col));
            }
            cdp_client
                .send_raw_command("Debugger.setBreakpointByUrl", params)
                .await
        } else if let Some(script_hash) = args.script_hash {
            let mut params = json!({
                "scriptHash": script_hash,
                "lineNumber": args.line_number,
            });
            if let Some(col) = args.column_number {
                params
                    .as_object_mut()
                    .unwrap()
                    .insert("columnNumber".to_string(), json!(col));
            }
            cdp_client
                .send_raw_command("Debugger.setBreakpointByUrl", params)
                .await
        } else {
            return Err(CallToolError::from_message(
                "Either script_id, url or script_hash must be provided.".to_string(),
            ));
        };

        let response = response.map_err(|e| {
            CallToolError::from_message(format!("Failed to set breakpoint: {:?}", e))
        })?;

        Ok(CallToolResult::text_content(vec![
            format!("Breakpoint set successfully. Response: {:?}", response).into(),
        ]))
    }
}
