use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Represents a job flowing through a pipeline.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Job {
    /// ULID generated by Bria or from config id_field.
    pub id: String,
    /// The source that produced this job.
    pub source: String,
    /// The job payload as a JSON value.
    pub payload: serde_json::Value,
    /// Optional correlation key for multi-source merging.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub correlation_key: Option<String>,
    /// Labels attached by the source and/or pipeline configuration.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub labels: HashMap<String, String>,
}

/// Result of executing a single pipeline step.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepResult {
    /// Captured stdout (if mode was capture).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stdout: Option<String>,
    /// Captured stderr (if mode was capture).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stderr: Option<String>,
    /// Exit code of the subprocess.
    pub exit_code: i32,
    /// Duration of the step in milliseconds.
    pub duration_ms: u64,
    /// Attempt number (1-indexed).
    pub attempt: u32,
    /// Named outputs extracted from stdout (JSON parse).
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub outputs: HashMap<String, serde_json::Value>,
}

/// The full context passed through a pipeline's steps.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Context {
    /// The job being processed.
    pub job: Job,
    /// Results of each step, keyed by step id.
    pub steps: HashMap<String, StepResult>,
}

impl Context {
    /// Create a new context from a job.
    pub fn new(job: Job) -> Self {
        Self {
            job,
            steps: HashMap::new(),
        }
    }

    /// Insert a step result.
    pub fn set_step(&mut self, id: String, result: StepResult) {
        self.steps.insert(id, result);
    }
}

/// Result of a completed pipeline run.
#[derive(Debug, Clone, Serialize)]
pub struct PipelineResult {
    /// The pipeline that ran.
    pub pipeline_id: String,
    /// The job that was processed.
    pub job: Job,
    /// Status: "success" or "failure".
    pub status: String,
    /// Total wall-clock duration in milliseconds.
    pub duration_ms: u64,
    /// Per-step results.
    pub steps: HashMap<String, StepResult>,
    /// Timestamp of completion (ISO-8601).
    pub occurred_at: String,
}

impl PipelineResult {
    pub fn success(
        pipeline_id: String,
        job: Job,
        steps: HashMap<String, StepResult>,
        duration_ms: u64,
    ) -> Self {
        Self {
            pipeline_id,
            job,
            status: "success".to_string(),
            duration_ms,
            steps,
            occurred_at: chrono::Utc::now().to_rfc3339(),
        }
    }

    pub fn failure(
        pipeline_id: String,
        job: Job,
        steps: HashMap<String, StepResult>,
        duration_ms: u64,
    ) -> Self {
        Self {
            pipeline_id,
            job,
            status: "failure".to_string(),
            duration_ms,
            steps,
            occurred_at: chrono::Utc::now().to_rfc3339(),
        }
    }
}
