use dioxus::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DastTargetsResponse {
    pub data: Vec<serde_json::Value>,
    pub total: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DastScanRunsResponse {
    pub data: Vec<serde_json::Value>,
    pub total: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DastFindingsResponse {
    pub data: Vec<serde_json::Value>,
    pub total: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DastFindingDetailResponse {
    pub data: serde_json::Value,
}

#[server]
pub async fn fetch_dast_targets() -> Result<DastTargetsResponse, ServerFnError> {
    let resp = super::agent_client::agent_get("/api/v1/dast/targets")
        .await?
        .send()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    let body: DastTargetsResponse = resp
        .json()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    Ok(body)
}

#[server]
pub async fn fetch_dast_scan_runs() -> Result<DastScanRunsResponse, ServerFnError> {
    let resp = super::agent_client::agent_get("/api/v1/dast/scan-runs")
        .await?
        .send()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    let body: DastScanRunsResponse = resp
        .json()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    Ok(body)
}

#[server]
pub async fn fetch_dast_findings() -> Result<DastFindingsResponse, ServerFnError> {
    let resp = super::agent_client::agent_get("/api/v1/dast/findings")
        .await?
        .send()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    let body: DastFindingsResponse = resp
        .json()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    Ok(body)
}

#[server]
pub async fn fetch_dast_finding_detail(
    id: String,
) -> Result<DastFindingDetailResponse, ServerFnError> {
    let resp = super::agent_client::agent_get(&format!("/api/v1/dast/findings/{id}"))
        .await?
        .send()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    let body: DastFindingDetailResponse = resp
        .json()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    Ok(body)
}

#[server]
pub async fn add_dast_target(name: String, base_url: String) -> Result<(), ServerFnError> {
    super::agent_client::agent_request(reqwest::Method::POST, "/api/v1/dast/targets")
        .await?
        .json(&serde_json::json!({
            "name": name,
            "base_url": base_url,
        }))
        .send()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;
    Ok(())
}

#[server]
pub async fn trigger_dast_scan(target_id: String) -> Result<(), ServerFnError> {
    super::agent_client::agent_request(
        reqwest::Method::POST,
        &format!("/api/v1/dast/targets/{target_id}/scan"),
    )
    .await?
    .send()
    .await
    .map_err(|e| ServerFnError::new(e.to_string()))?;
    Ok(())
}
