use crate::evidence::{agent_brief, contains_prohibited_claim, evidence_cards};
use crate::image::MeasurementReport;
use crate::types::{AgentBrief, EvidenceCard};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ReportError {
    #[error("report text contains a prohibited claim")]
    ProhibitedClaim,
    #[error("report serialization failed")]
    Serialization,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct SdkReport {
    pub measurement: MeasurementReport,
    pub evidence_cards: Vec<EvidenceCard>,
    pub agent_brief: AgentBrief,
}

pub fn build_report(measurement: MeasurementReport) -> Result<SdkReport, ReportError> {
    let evidence_cards = evidence_cards(&measurement);
    let agent_brief = agent_brief(&measurement);
    let report = SdkReport {
        measurement,
        evidence_cards,
        agent_brief,
    };
    let serialized = serde_json::to_string(&report).map_err(|_| ReportError::Serialization)?;
    if contains_prohibited_claim(&serialized) {
        return Err(ReportError::ProhibitedClaim);
    }
    Ok(report)
}
