use crate::db::{Error, Pool};
use super::row::MessageTable;
pub async fn insert_error(
pool: &Pool,
agent_instance_hierarchy: &str,
response_id: Option<&str>,
error: &serde_json::Value,
timestamp: i64,
) -> Result<i64, Error> {
let (id,): (i64,) = sqlx::query_as(
"WITH data_ins AS (\
INSERT INTO objectiveai.errors \
(agent_instance_hierarchy, response_id, error) \
VALUES ($1, $2, $3) RETURNING id\
), msg_ins AS (\
INSERT INTO objectiveai.messages \
(response_id, \"table\", row_index, row_sub_index, \
agent_instance_hierarchy, \"timestamp\") \
SELECT $2, $4, id, NULL, $1, $5 FROM data_ins\
) \
SELECT id FROM data_ins",
)
.bind(agent_instance_hierarchy)
.bind(response_id)
.bind(error)
.bind(MessageTable::Error)
.bind(timestamp)
.fetch_one(&**pool)
.await?;
Ok(id)
}