// AI edge-forslag dispatcher — delegerer til synops-suggest-edges CLI.
//
// Maskinrommet orkestrerer, CLI-verktøyet gjør jobben (LLM-kall,
// topic-opprettelse, edge-skriving, ressurslogging).
//
// Jobbtype: "suggest_edges"
// Payload: { "node_id": "<uuid>" }
//
// Ref: docs/retninger/unix_filosofi.md, docs/infra/ai_gateway.md,
//      docs/concepts/kunnskapsgrafen.md

use uuid::Uuid;

use crate::ai_admin;
use crate::cli_dispatch;
use crate::jobs::JobRow;

/// Synops-suggest-edges binary path.
fn suggest_edges_bin() -> String {
    std::env::var("SYNOPS_SUGGEST_EDGES_BIN")
        .unwrap_or_else(|_| "synops-suggest-edges".to_string())
}

/// Håndterer suggest_edges-jobb.
///
/// Spawner synops-suggest-edges med --write for å gjøre alt arbeidet:
/// LLM-kall, topic-opprettelse, edge-skriving, ressurslogging.
pub async fn handle_suggest_edges(
    job: &JobRow,
    db: &sqlx::PgPool,
) -> Result<serde_json::Value, String> {
    let node_id: Uuid = job
        .payload
        .get("node_id")
        .and_then(|v| v.as_str())
        .and_then(|s| s.parse().ok())
        .ok_or("Mangler gyldig node_id i payload")?;

    // requested_by er valgfri — bruk node_id som fallback for --write
    let requested_by = job
        .payload
        .get("requested_by")
        .and_then(|v| v.as_str())
        .unwrap_or(&node_id.to_string())
        .to_string();

    // Bygg kommando
    let bin = suggest_edges_bin();
    let mut cmd = tokio::process::Command::new(&bin);

    cmd.arg("--node-id").arg(node_id.to_string())
        .arg("--requested-by").arg(&requested_by)
        .arg("--write");

    // Sett miljøvariabler CLI-verktøyet trenger
    cli_dispatch::set_database_url(&mut cmd)?;
    cli_dispatch::forward_env(&mut cmd, "AI_GATEWAY_URL");
    cli_dispatch::forward_env(&mut cmd, "LITELLM_MASTER_KEY");

    // Modellalias fra ai_job_routing — admin kan endre uten redeploy
    let model_alias = ai_admin::resolve_routing_or_default(db, "suggest_edges").await;
    cmd.env("AI_EDGES_MODEL", &model_alias);

    tracing::info!(
        node_id = %node_id,
        bin = %bin,
        "Starter synops-suggest-edges"
    );

    let result = cli_dispatch::run_cli_tool(&bin, &mut cmd).await?;

    tracing::info!(
        node_id = %node_id,
        topics_created = result["topics_created"].as_u64().unwrap_or(0),
        edges_created = result["edges_created"].as_u64().unwrap_or(0),
        status = result["status"].as_str().unwrap_or("unknown"),
        "synops-suggest-edges fullført"
    );

    Ok(result)
}
