use serde_json::{json, Value};

use crate::{automation, error::HenError};

use super::{
    artifacts::{request_failure_json, run_record_json},
    common::with_type,
    json::hen_error_json,
    BodyReportOptions,
};

pub fn run_outcome_ndjson(
    outcome: &automation::RunOutcome,
    body_options: BodyReportOptions,
) -> String {
    let mut lines = vec![json!({
        "type": "run",
        "collection": {
            "path": outcome.collection.path.display().to_string(),
            "name": outcome.collection.name,
            "description": outcome.collection.description,
        },
        "plan": outcome.plan,
        "selectedRequests": outcome.selected_requests,
        "primaryTarget": outcome.primary_target,
        "executionFailed": outcome.execution_failed,
        "interrupted": outcome.interrupted.is_some(),
        "interruptSignal": outcome.interrupted.map(|signal| signal.as_str()),
        "recordCount": outcome.records.len(),
        "failureCount": outcome.failures.len(),
    })];

    lines.extend(
        outcome
            .records
            .iter()
            .map(|record| with_type(run_record_json(record, body_options), "record")),
    );
    lines.extend(
        outcome
            .failures
            .iter()
            .map(|failure| with_type(request_failure_json(failure, body_options), "failure")),
    );

    render_ndjson(lines)
}

pub fn verification_result_ndjson(result: &automation::VerificationResult) -> String {
    let mut lines = vec![json!({
        "type": "verify",
        "path": result.path.as_ref().map(|path| path.display().to_string()),
        "name": result.summary.name,
        "description": result.summary.description,
    })];

    lines.extend(result.summary.requests.iter().map(|request| {
        json!({
            "type": "request",
            "index": request.index,
            "description": request.description,
            "method": request.method,
            "url": request.url,
        })
    }));

    lines.extend(result.required_inputs.iter().map(|prompt| {
        json!({
            "type": "requiredInput",
            "name": prompt.name,
            "default": prompt.default,
        })
    }));

    render_ndjson(lines)
}

pub fn hen_error_ndjson(error: &HenError) -> String {
    render_ndjson(vec![with_type(hen_error_json(error), "error")])
}

fn render_ndjson(lines: Vec<Value>) -> String {
    lines
        .into_iter()
        .map(|line| serde_json::to_string(&line).expect("ndjson line should serialize"))
        .collect::<Vec<_>>()
        .join("\n")
}