use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::pipe::Timing;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BridgeLifecycleCall {
    Connect,
    Start,
    Stop,
    SubmitTask,
}

impl BridgeLifecycleCall {
    pub fn bridge_name(self) -> &'static str {
        match self {
            Self::Connect => "sgclawConnect",
            Self::Start => "sgclawStart",
            Self::Stop => "sgclawStop",
            Self::SubmitTask => "sgclawSubmitTask",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BridgeBrowserActionRequest {
    pub action: String,
    pub params: Value,
    pub expected_domain: String,
}

impl BridgeBrowserActionRequest {
    pub fn new(
        action: impl Into<String>,
        params: Value,
        expected_domain: impl Into<String>,
    ) -> Self {
        Self {
            action: action.into(),
            params,
            expected_domain: expected_domain.into(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BridgeBrowserActionReply {
    Success(BridgeBrowserActionSuccess),
    Error(BridgeBrowserActionError),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BridgeBrowserActionSuccess {
    pub data: Value,
    pub aom_snapshot: Vec<Value>,
    pub timing: Timing,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BridgeBrowserActionError {
    pub message: String,
    pub details: Value,
}
