use std::collections::BTreeMap;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use serde::de::{self, DeserializeSeed, IgnoredAny, MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::value::RawValue;
use serde_json::{Value, json};
use sha2::{Digest, Sha256};

use crate::query_db::{
    ObjectInstanceRecord, PublicationPointRecord, QUERY_DB_SCHEMA_VERSION, QueryDbError,
    QueryDbResult, QueryPage, RepositoryRecord, StatsRecord,
};

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ReportSummary {
    pub validation_time: Option<String>,
    pub publication_points: Vec<PublicationPointRecord>,
    pub repos: Vec<RepositoryRecord>,
    pub stats: ReportStatsSummary,
    pub query_audit: Option<Value>,
    pub download_stats: Option<Value>,
    pub vrps_count: u64,
    pub aspas_count: u64,
    pub warnings_count: u64,
    pub objects_count: u64,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ReportStatsSummary {
    pub object_type_counts: BTreeMap<String, u64>,
    pub result_counts: BTreeMap<String, u64>,
    pub source_counts: BTreeMap<String, u64>,
    pub reason_counts: BTreeMap<String, u64>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ObjectScope {
    All,
    Repo(String),
    PublicationPoint(String),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ObjectLookup {
    InstanceId(String),
    Uri(String),
    Sha256(String),
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ObjectResolutionMeta {
    pub mode: String,
    pub cache_hit: bool,
    pub scan_scope: String,
    pub scanned_publication_points: u64,
    pub scanned_objects: u64,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ObjectLookupResult {
    pub object: ObjectInstanceRecord,
    pub resolution: ObjectResolutionMeta,
}

pub fn summarize_report(report_path: &Path, run_id: &str) -> QueryDbResult<ReportSummary> {
    let file = File::open(report_path)?;
    let reader = BufReader::new(file);
    let mut deserializer = serde_json::Deserializer::from_reader(reader);
    ReportSummarySeed { run_id }
        .deserialize(&mut deserializer)
        .map_err(QueryDbError::from)
}

pub fn list_report_objects(
    report_path: &Path,
    run_id: &str,
    scope: ObjectScope,
    limit: usize,
    cursor: Option<&str>,
) -> QueryDbResult<QueryPage<ObjectInstanceRecord>> {
    let (start_pp_index, start_object_index) = parse_object_cursor(cursor)?;
    let file = File::open(report_path)?;
    let reader = BufReader::new(file);
    let mut deserializer = serde_json::Deserializer::from_reader(reader);
    let mut state =
        ObjectScanState::new_list(run_id, scope, limit, start_pp_index, start_object_index);
    ObjectScanSeed { state: &mut state }
        .deserialize(&mut deserializer)
        .map_err(QueryDbError::from)?;
    Ok(QueryPage {
        data: state.data,
        next_cursor: state.next_cursor,
        limit,
    })
}

pub fn lookup_report_object(
    report_path: &Path,
    run_id: &str,
    scope: ObjectScope,
    lookup: ObjectLookup,
) -> QueryDbResult<Option<ObjectLookupResult>> {
    let file = File::open(report_path)?;
    let reader = BufReader::new(file);
    let mut deserializer = serde_json::Deserializer::from_reader(reader);
    let mut state = ObjectScanState::new_lookup(run_id, scope, lookup);
    ObjectScanSeed { state: &mut state }
        .deserialize(&mut deserializer)
        .map_err(QueryDbError::from)?;
    Ok(state.found.map(|object| ObjectLookupResult {
        object,
        resolution: ObjectResolutionMeta {
            mode: "report_scan".to_string(),
            cache_hit: false,
            scan_scope: state.scope.label(),
            scanned_publication_points: state.scanned_publication_points,
            scanned_objects: state.scanned_objects,
        },
    }))
}

pub fn object_cursor(pp_index: u64, object_index: u64) -> String {
    format!("r1:{pp_index}:{object_index}")
}

fn parse_object_cursor(cursor: Option<&str>) -> QueryDbResult<(u64, u64)> {
    let Some(cursor) = cursor else {
        return Ok((0, 0));
    };
    let mut parts = cursor.split(':');
    match (parts.next(), parts.next(), parts.next(), parts.next()) {
        (Some("r1"), Some(pp), Some(object), None) => {
            let pp = pp.parse::<u64>().map_err(|_| {
                QueryDbError::InvalidArtifact(format!("invalid object cursor: {cursor}"))
            })?;
            let object = object.parse::<u64>().map_err(|_| {
                QueryDbError::InvalidArtifact(format!("invalid object cursor: {cursor}"))
            })?;
            Ok((pp, object))
        }
        _ => Err(QueryDbError::InvalidArtifact(format!(
            "invalid object cursor: {cursor}"
        ))),
    }
}

struct ReportSummarySeed<'a> {
    run_id: &'a str,
}

impl<'de> DeserializeSeed<'de> for ReportSummarySeed<'_> {
    type Value = ReportSummary;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(ReportSummaryVisitor {
            run_id: self.run_id,
        })
    }
}

struct ReportSummaryVisitor<'a> {
    run_id: &'a str,
}

impl<'de> Visitor<'de> for ReportSummaryVisitor<'_> {
    type Value = ReportSummary;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("audit report object")
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        let mut out = ReportSummary::default();
        while let Some(key) = map.next_key::<String>()? {
            match key.as_str() {
                "meta" => {
                    let meta: Value = map.next_value()?;
                    out.validation_time =
                        json_str(&meta, &["validation_time_rfc3339_utc"]).map(str::to_string);
                }
                "tree" => {
                    let tree: Value = map.next_value()?;
                    out.warnings_count = json_array_len(&tree, &["warnings"]).unwrap_or(0);
                }
                "publication_points" => {
                    let pps = map.next_value_seed(PublicationPointsSummarySeed {
                        run_id: self.run_id,
                    })?;
                    out.objects_count = pps.objects_count;
                    out.publication_points = pps.publication_points;
                    out.repos = pps.repos;
                    out.stats = pps.stats;
                }
                "vrps" => out.vrps_count = map.next_value::<CountSeq>()?.0,
                "aspas" => out.aspas_count = map.next_value::<CountSeq>()?.0,
                "download_stats" => out.download_stats = Some(map.next_value()?),
                "queryAudit" => out.query_audit = Some(map.next_value()?),
                _ => {
                    let _ = map.next_value::<IgnoredAny>()?;
                }
            }
        }
        Ok(out)
    }
}

struct PublicationPointsSummarySeed<'a> {
    run_id: &'a str,
}

impl<'de> DeserializeSeed<'de> for PublicationPointsSummarySeed<'_> {
    type Value = PublicationPointsSummary;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(PublicationPointsSummaryVisitor {
            run_id: self.run_id,
        })
    }
}

#[derive(Default)]
struct PublicationPointsSummary {
    publication_points: Vec<PublicationPointRecord>,
    repos: Vec<RepositoryRecord>,
    stats: ReportStatsSummary,
    objects_count: u64,
}

struct PublicationPointsSummaryVisitor<'a> {
    run_id: &'a str,
}

impl<'de> Visitor<'de> for PublicationPointsSummaryVisitor<'_> {
    type Value = PublicationPointsSummary;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("publication point array")
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let mut out = PublicationPointsSummary::default();
        let mut repos = BTreeMap::<String, RepoBuilder>::new();
        while let Some(pp) = seq.next_element::<PublicationPointSummary>()? {
            let repo_uri = repo_uri_for_pp(&pp);
            let repo_id = stable_id(&repo_uri);
            let pp_id = pp_id_for(&pp);
            let pp_record = pp.to_record(self.run_id, repo_id.clone(), pp_id);
            repos
                .entry(repo_id.clone())
                .or_insert_with(|| RepoBuilder::new(repo_id, repo_uri))
                .add_pp(&pp_record);
            out.objects_count += pp_record.objects;
            merge_counts(
                &mut out.stats.object_type_counts,
                pp.object_stats.object_type_counts,
            );
            merge_counts(&mut out.stats.result_counts, pp.object_stats.result_counts);
            merge_counts(&mut out.stats.reason_counts, pp.object_stats.reason_counts);
            *out.stats
                .source_counts
                .entry("report".to_string())
                .or_default() += pp_record.objects;
            out.publication_points.push(pp_record);
        }
        out.repos = repos
            .into_values()
            .map(|builder| builder.finish(self.run_id))
            .collect();
        Ok(out)
    }
}

#[derive(Default, Deserialize)]
struct CountSeq(#[serde(deserialize_with = "count_seq")] u64);

fn count_seq<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
    D: Deserializer<'de>,
{
    struct CountVisitor;
    impl<'de> Visitor<'de> for CountVisitor {
        type Value = u64;
        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("array")
        }
        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: SeqAccess<'de>,
        {
            let mut count = 0u64;
            while seq.next_element::<IgnoredAny>()?.is_some() {
                count += 1;
            }
            Ok(count)
        }
    }
    deserializer.deserialize_seq(CountVisitor)
}

#[derive(Clone, Debug, Default)]
struct PublicationPointSummary {
    node_id: Option<u64>,
    parent_node_id: Option<u64>,
    rsync_base_uri: Option<String>,
    manifest_rsync_uri: Option<String>,
    publication_point_rsync_uri: Option<String>,
    rrdp_notification_uri: Option<String>,
    source: Option<String>,
    repo_sync_source: Option<String>,
    repo_sync_phase: Option<String>,
    repo_sync_duration_ms: Option<u64>,
    repo_sync_error: Option<String>,
    repo_terminal_state: Option<String>,
    this_update: Option<String>,
    next_update: Option<String>,
    verified_at: Option<String>,
    warnings: u64,
    object_stats: ObjectStats,
}

impl PublicationPointSummary {
    fn to_record(&self, run_id: &str, repo_id: String, pp_id: String) -> PublicationPointRecord {
        PublicationPointRecord {
            schema_version: QUERY_DB_SCHEMA_VERSION,
            run_id: run_id.to_string(),
            pp_id,
            repo_id,
            node_id: self.node_id,
            parent_node_id: self.parent_node_id,
            rsync_base_uri: self.rsync_base_uri.clone(),
            manifest_rsync_uri: self.manifest_rsync_uri.clone(),
            publication_point_rsync_uri: self.publication_point_rsync_uri.clone(),
            rrdp_notification_uri: self.rrdp_notification_uri.clone(),
            source: self.source.clone(),
            repo_sync_source: self.repo_sync_source.clone(),
            repo_sync_phase: self.repo_sync_phase.clone(),
            repo_sync_duration_ms: self.repo_sync_duration_ms,
            repo_sync_error: self.repo_sync_error.clone(),
            repo_terminal_state: self.repo_terminal_state.clone(),
            this_update: self.this_update.clone(),
            next_update: self.next_update.clone(),
            verified_at: self.verified_at.clone(),
            objects: self.object_stats.count,
            rejected_objects: self.object_stats.rejected,
            warnings: self.warnings,
        }
    }
}

impl<'de> Deserialize<'de> for PublicationPointSummary {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(PublicationPointSummaryVisitor)
    }
}

struct PublicationPointSummaryVisitor;

impl<'de> Visitor<'de> for PublicationPointSummaryVisitor {
    type Value = PublicationPointSummary;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("publication point object")
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        let mut pp = PublicationPointSummary::default();
        while let Some(key) = map.next_key::<String>()? {
            match key.as_str() {
                "node_id" => pp.node_id = map.next_value()?,
                "parent_node_id" => pp.parent_node_id = map.next_value()?,
                "rsync_base_uri" => pp.rsync_base_uri = map.next_value()?,
                "manifest_rsync_uri" => pp.manifest_rsync_uri = map.next_value()?,
                "publication_point_rsync_uri" => {
                    pp.publication_point_rsync_uri = map.next_value()?
                }
                "rrdp_notification_uri" => pp.rrdp_notification_uri = map.next_value()?,
                "source" => pp.source = map.next_value()?,
                "repo_sync_source" => pp.repo_sync_source = map.next_value()?,
                "repo_sync_phase" => pp.repo_sync_phase = map.next_value()?,
                "repo_sync_duration_ms" => pp.repo_sync_duration_ms = map.next_value()?,
                "repo_sync_error" => pp.repo_sync_error = map.next_value()?,
                "repo_terminal_state" => pp.repo_terminal_state = map.next_value()?,
                "this_update_rfc3339_utc" => pp.this_update = map.next_value()?,
                "next_update_rfc3339_utc" => pp.next_update = map.next_value()?,
                "verified_at_rfc3339_utc" => pp.verified_at = map.next_value()?,
                "warnings" => pp.warnings = map.next_value::<CountSeq>()?.0,
                "objects" => pp.object_stats = map.next_value::<ObjectStatsSeq>()?.0,
                _ => {
                    let _ = map.next_value::<IgnoredAny>()?;
                }
            }
        }
        Ok(pp)
    }
}

#[derive(Clone, Debug, Default)]
struct ObjectStats {
    count: u64,
    rejected: u64,
    object_type_counts: BTreeMap<String, u64>,
    result_counts: BTreeMap<String, u64>,
    reason_counts: BTreeMap<String, u64>,
}

#[derive(Clone, Debug, Default)]
struct ObjectStatsSeq(ObjectStats);

impl<'de> Deserialize<'de> for ObjectStatsSeq {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ObjectStatsVisitor;
        impl<'de> Visitor<'de> for ObjectStatsVisitor {
            type Value = ObjectStatsSeq;
            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("object audit array")
            }
            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                let mut stats = ObjectStats::default();
                while let Some(object) = seq.next_element::<ReportObjectSummary>()? {
                    stats.count += 1;
                    if object.result == "error" {
                        stats.rejected += 1;
                    }
                    *stats
                        .object_type_counts
                        .entry(object.object_type)
                        .or_default() += 1;
                    *stats.result_counts.entry(object.result).or_default() += 1;
                    if let Some(detail) = object.detail.filter(|value| !value.is_empty()) {
                        bump_limited_count(&mut stats.reason_counts, &detail, 512);
                    }
                }
                Ok(ObjectStatsSeq(stats))
            }
        }
        deserializer.deserialize_seq(ObjectStatsVisitor)
    }
}

#[derive(Clone, Debug, Default)]
struct ReportObjectSummary {
    object_type: String,
    result: String,
    detail: Option<String>,
}

impl<'de> Deserialize<'de> for ReportObjectSummary {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(ReportObjectSummaryVisitor)
    }
}

struct ReportObjectSummaryVisitor;

impl<'de> Visitor<'de> for ReportObjectSummaryVisitor {
    type Value = ReportObjectSummary;
    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("object summary")
    }
    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        let mut object = ReportObjectSummary {
            object_type: "other".to_string(),
            result: "unknown".to_string(),
            detail: None,
        };
        let mut uri: Option<String> = None;
        while let Some(key) = map.next_key::<String>()? {
            match key.as_str() {
                "kind" => {
                    object.object_type = map
                        .next_value::<Option<String>>()?
                        .unwrap_or_else(|| "other".to_string())
                }
                "result" => {
                    object.result = map
                        .next_value::<Option<String>>()?
                        .unwrap_or_else(|| "unknown".to_string())
                }
                "detail" => object.detail = map.next_value()?,
                "rsync_uri" | "uri" => uri = map.next_value()?,
                _ => {
                    let _ = map.next_value::<IgnoredAny>()?;
                }
            }
        }
        if object.object_type == "other" {
            if let Some(uri) = uri.as_deref() {
                object.object_type = object_type_from_uri(uri);
            }
        }
        Ok(object)
    }
}

struct ObjectScanState<'a> {
    run_id: &'a str,
    scope: ObjectScope,
    mode: ObjectScanMode,
    limit: usize,
    start_pp_index: u64,
    start_object_index: u64,
    data: Vec<ObjectInstanceRecord>,
    next_cursor: Option<String>,
    found: Option<ObjectInstanceRecord>,
    scanned_publication_points: u64,
    scanned_objects: u64,
}

impl<'a> ObjectScanState<'a> {
    fn new_list(
        run_id: &'a str,
        scope: ObjectScope,
        limit: usize,
        start_pp_index: u64,
        start_object_index: u64,
    ) -> Self {
        Self {
            run_id,
            scope,
            mode: ObjectScanMode::List,
            limit,
            start_pp_index,
            start_object_index,
            data: Vec::new(),
            next_cursor: None,
            found: None,
            scanned_publication_points: 0,
            scanned_objects: 0,
        }
    }

    fn new_lookup(run_id: &'a str, scope: ObjectScope, lookup: ObjectLookup) -> Self {
        Self {
            run_id,
            scope,
            mode: ObjectScanMode::Lookup(lookup),
            limit: 1,
            start_pp_index: 0,
            start_object_index: 0,
            data: Vec::new(),
            next_cursor: None,
            found: None,
            scanned_publication_points: 0,
            scanned_objects: 0,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum ObjectScanMode {
    List,
    Lookup(ObjectLookup),
}

struct ObjectScanSeed<'a, 's> {
    state: &'s mut ObjectScanState<'a>,
}

impl<'de> DeserializeSeed<'de> for ObjectScanSeed<'_, '_> {
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(ObjectScanReportVisitor { state: self.state })
    }
}

struct ObjectScanReportVisitor<'a, 's> {
    state: &'s mut ObjectScanState<'a>,
}

impl<'de> Visitor<'de> for ObjectScanReportVisitor<'_, '_> {
    type Value = ();
    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("audit report object")
    }
    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        while let Some(key) = map.next_key::<String>()? {
            if key == "publication_points" {
                map.next_value_seed(ObjectScanPpsSeed { state: self.state })?;
            } else {
                let _ = map.next_value::<IgnoredAny>()?;
            }
        }
        Ok(())
    }
}

struct ObjectScanPpsSeed<'a, 's> {
    state: &'s mut ObjectScanState<'a>,
}

impl<'de> DeserializeSeed<'de> for ObjectScanPpsSeed<'_, '_> {
    type Value = ();
    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(ObjectScanPpsVisitor { state: self.state })
    }
}

struct ObjectScanPpsVisitor<'a, 's> {
    state: &'s mut ObjectScanState<'a>,
}

impl<'de> Visitor<'de> for ObjectScanPpsVisitor<'_, '_> {
    type Value = ();
    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("publication point array")
    }
    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let mut pp_index = 0u64;
        while let Some(raw_pp) = seq.next_element::<Box<RawValue>>()? {
            process_raw_publication_point(self.state, pp_index, raw_pp.get())
                .map_err(de::Error::custom)?;
            pp_index += 1;
        }
        Ok(())
    }
}

fn process_raw_publication_point(
    state: &mut ObjectScanState<'_>,
    pp_index: u64,
    raw_pp: &str,
) -> QueryDbResult<()> {
    state.scanned_publication_points += 1;
    let pp: PublicationPointForObjects = serde_json::from_str(raw_pp)?;
    let ctx = PpObjectContext::from_pp(state.run_id, &pp.summary);
    if !state.scope.matches(&ctx) {
        return Ok(());
    }
    for (object_index, object) in pp.objects.into_iter().enumerate() {
        state.scanned_objects += 1;
        let record = object.to_record(state.run_id, &ctx);
        match &state.mode {
            ObjectScanMode::List => {
                let object_index = object_index as u64;
                let after_cursor = pp_index > state.start_pp_index
                    || (pp_index == state.start_pp_index
                        && object_index >= state.start_object_index);
                if after_cursor && state.data.len() < state.limit {
                    state.data.push(record);
                    if state.data.len() == state.limit {
                        state.next_cursor = Some(object_cursor(pp_index, object_index + 1));
                    }
                }
            }
            ObjectScanMode::Lookup(lookup) => {
                if state.found.is_none() && lookup.matches(&record) {
                    state.found = Some(record);
                }
            }
        }
    }
    Ok(())
}

#[derive(Clone, Debug, Default)]
struct PublicationPointForObjects {
    summary: PublicationPointSummary,
    objects: Vec<ReportObject>,
}

impl<'de> Deserialize<'de> for PublicationPointForObjects {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(PublicationPointForObjectsVisitor)
    }
}

struct PublicationPointForObjectsVisitor;

impl<'de> Visitor<'de> for PublicationPointForObjectsVisitor {
    type Value = PublicationPointForObjects;
    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("publication point object for object scan")
    }
    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        let mut pp = PublicationPointForObjects::default();
        while let Some(key) = map.next_key::<String>()? {
            match key.as_str() {
                "node_id" => pp.summary.node_id = map.next_value()?,
                "parent_node_id" => pp.summary.parent_node_id = map.next_value()?,
                "rsync_base_uri" => pp.summary.rsync_base_uri = map.next_value()?,
                "manifest_rsync_uri" => pp.summary.manifest_rsync_uri = map.next_value()?,
                "publication_point_rsync_uri" => {
                    pp.summary.publication_point_rsync_uri = map.next_value()?
                }
                "rrdp_notification_uri" => pp.summary.rrdp_notification_uri = map.next_value()?,
                "source" => pp.summary.source = map.next_value()?,
                "repo_sync_source" => pp.summary.repo_sync_source = map.next_value()?,
                "repo_sync_phase" => pp.summary.repo_sync_phase = map.next_value()?,
                "repo_sync_duration_ms" => pp.summary.repo_sync_duration_ms = map.next_value()?,
                "repo_sync_error" => pp.summary.repo_sync_error = map.next_value()?,
                "repo_terminal_state" => pp.summary.repo_terminal_state = map.next_value()?,
                "this_update_rfc3339_utc" => pp.summary.this_update = map.next_value()?,
                "next_update_rfc3339_utc" => pp.summary.next_update = map.next_value()?,
                "verified_at_rfc3339_utc" => pp.summary.verified_at = map.next_value()?,
                "objects" => pp.objects = map.next_value()?,
                _ => {
                    let _ = map.next_value::<IgnoredAny>()?;
                }
            }
        }
        Ok(pp)
    }
}

#[derive(Clone, Debug, Default)]
struct ReportObject {
    uri: String,
    sha256: String,
    object_type: String,
    result: String,
    detail: Option<String>,
}

impl ReportObject {
    fn to_record(self, run_id: &str, ctx: &PpObjectContext) -> ObjectInstanceRecord {
        let object_type = if self.object_type.is_empty() || self.object_type == "other" {
            object_type_from_uri(&self.uri)
        } else {
            self.object_type
        };
        let uri_hash = stable_id(&self.uri);
        let object_instance_id = stable_id(&format!(
            "{run_id}\0{}\0{}\0{}",
            self.uri, self.sha256, ctx.pp_id
        ));
        let rejected = self.result == "error";
        ObjectInstanceRecord {
            schema_version: QUERY_DB_SCHEMA_VERSION,
            run_id: run_id.to_string(),
            object_instance_id,
            uri: self.uri,
            uri_hash,
            sha256: self.sha256,
            object_type,
            result: self.result,
            detail_summary: self.detail.clone(),
            repo_id: ctx.repo_id.clone(),
            pp_id: ctx.pp_id.clone(),
            source_section: "report".to_string(),
            rejected,
            reject_reason: if rejected { self.detail } else { None },
        }
    }
}

impl<'de> Deserialize<'de> for ReportObject {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(ReportObjectVisitor)
    }
}

struct ReportObjectVisitor;

impl<'de> Visitor<'de> for ReportObjectVisitor {
    type Value = ReportObject;
    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("report object")
    }
    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        let mut object = ReportObject {
            result: "unknown".to_string(),
            object_type: "other".to_string(),
            ..ReportObject::default()
        };
        while let Some(key) = map.next_key::<String>()? {
            match key.as_str() {
                "rsync_uri" | "uri" => {
                    object.uri = map.next_value::<Option<String>>()?.unwrap_or_default()
                }
                "sha256_hex" | "sha256" => {
                    object.sha256 = map.next_value::<Option<String>>()?.unwrap_or_default()
                }
                "kind" => {
                    object.object_type = map
                        .next_value::<Option<String>>()?
                        .unwrap_or_else(|| "other".to_string())
                }
                "result" => {
                    object.result = map
                        .next_value::<Option<String>>()?
                        .unwrap_or_else(|| "unknown".to_string())
                }
                "detail" => object.detail = map.next_value()?,
                _ => {
                    let _ = map.next_value::<IgnoredAny>()?;
                }
            }
        }
        Ok(object)
    }
}

#[derive(Clone, Debug, Default)]
struct PpObjectContext {
    repo_id: String,
    pp_id: String,
}

impl PpObjectContext {
    fn from_pp(_run_id: &str, pp: &PublicationPointSummary) -> Self {
        let repo_uri = repo_uri_for_pp(pp);
        Self {
            repo_id: stable_id(&repo_uri),
            pp_id: pp_id_for(pp),
        }
    }
}

impl ObjectScope {
    fn matches(&self, ctx: &PpObjectContext) -> bool {
        match self {
            ObjectScope::All => true,
            ObjectScope::Repo(repo_id) => repo_id == &ctx.repo_id,
            ObjectScope::PublicationPoint(pp_id) => pp_id == &ctx.pp_id,
        }
    }

    fn label(&self) -> String {
        match self {
            ObjectScope::All => "all".to_string(),
            ObjectScope::Repo(_) => "repo".to_string(),
            ObjectScope::PublicationPoint(_) => "publication_point".to_string(),
        }
    }
}

impl ObjectLookup {
    fn matches(&self, object: &ObjectInstanceRecord) -> bool {
        match self {
            ObjectLookup::InstanceId(id) => id == &object.object_instance_id,
            ObjectLookup::Uri(uri) => uri == &object.uri,
            ObjectLookup::Sha256(sha256) => sha256.eq_ignore_ascii_case(&object.sha256),
        }
    }
}

#[derive(Clone, Debug)]
struct RepoBuilder {
    repo_id: String,
    uri: String,
    publication_points: u64,
    objects: u64,
    rejected_objects: u64,
    sync_duration_ms_total: u64,
    phases: BTreeMap<String, u64>,
    terminal_states: BTreeMap<String, u64>,
}

impl RepoBuilder {
    fn new(repo_id: String, uri: String) -> Self {
        Self {
            repo_id,
            uri,
            publication_points: 0,
            objects: 0,
            rejected_objects: 0,
            sync_duration_ms_total: 0,
            phases: BTreeMap::new(),
            terminal_states: BTreeMap::new(),
        }
    }

    fn add_pp(&mut self, pp: &PublicationPointRecord) {
        self.publication_points += 1;
        self.objects += pp.objects;
        self.rejected_objects += pp.rejected_objects;
        self.sync_duration_ms_total = self
            .sync_duration_ms_total
            .saturating_add(pp.repo_sync_duration_ms.unwrap_or(0));
        if let Some(phase) = pp.repo_sync_phase.as_ref() {
            *self.phases.entry(phase.clone()).or_default() += 1;
        }
        if let Some(state) = pp.repo_terminal_state.as_ref() {
            *self.terminal_states.entry(state.clone()).or_default() += 1;
        }
    }

    fn finish(self, run_id: &str) -> RepositoryRecord {
        RepositoryRecord {
            schema_version: QUERY_DB_SCHEMA_VERSION,
            run_id: run_id.to_string(),
            repo_id: self.repo_id,
            uri: self.uri.clone(),
            host: uri_host(&self.uri),
            transport: infer_transport(&self.uri),
            publication_points: self.publication_points,
            objects: self.objects,
            rejected_objects: self.rejected_objects,
            download_bytes: None,
            sync_duration_ms_total: self.sync_duration_ms_total,
            phases: self.phases,
            terminal_states: self.terminal_states,
        }
    }
}

pub fn stats_records_from_summary(
    run_id: &str,
    summary: &ReportSummary,
    artifacts: Value,
) -> Vec<StatsRecord> {
    vec![
        stats_record(
            run_id,
            "overview",
            "counts",
            json!({
                "publicationPoints": summary.publication_points.len() as u64,
                "objects": summary.objects_count,
                "repos": summary.repos.len() as u64,
                "vrps": summary.vrps_count,
                "aspas": summary.aspas_count,
            }),
        ),
        stats_record(
            run_id,
            "objects",
            "by_type",
            json!(summary.stats.object_type_counts),
        ),
        stats_record(
            run_id,
            "validation",
            "by_result",
            json!(summary.stats.result_counts),
        ),
        stats_record(
            run_id,
            "objects",
            "by_source",
            json!(summary.stats.source_counts),
        ),
        stats_record(
            run_id,
            "validation",
            "reasons",
            json!(summary.stats.reason_counts),
        ),
        stats_record(
            run_id,
            "validation_events",
            "manifest",
            summary.query_audit.clone().unwrap_or_else(|| json!({})),
        ),
        stats_record(run_id, "validation_events", "by_type", json!({})),
        stats_record(run_id, "validation_events", "by_result", json!({})),
        stats_record(run_id, "validation_events", "reasons", json!({})),
        stats_record(
            run_id,
            "downloads",
            "summary",
            summary.download_stats.clone().unwrap_or_else(|| json!({})),
        ),
        stats_record(run_id, "artifacts", "manifest", artifacts),
    ]
}

fn stats_record(run_id: &str, scope: &str, name: &str, value: Value) -> StatsRecord {
    StatsRecord {
        schema_version: QUERY_DB_SCHEMA_VERSION,
        run_id: run_id.to_string(),
        scope: scope.to_string(),
        name: name.to_string(),
        value,
    }
}

fn merge_counts(target: &mut BTreeMap<String, u64>, source: BTreeMap<String, u64>) {
    for (key, value) in source {
        *target.entry(key).or_default() += value;
    }
}

fn bump_limited_count(counts: &mut BTreeMap<String, u64>, key: &str, limit: usize) {
    if counts.contains_key(key) || counts.len() < limit {
        *counts.entry(key.to_string()).or_default() += 1;
    } else {
        *counts.entry("__other__".to_string()).or_default() += 1;
    }
}

fn repo_uri_for_pp(pp: &PublicationPointSummary) -> String {
    pp.rrdp_notification_uri
        .as_deref()
        .or(pp.rsync_base_uri.as_deref())
        .or(pp.publication_point_rsync_uri.as_deref())
        .or(pp.manifest_rsync_uri.as_deref())
        .unwrap_or("unknown")
        .to_string()
}

fn pp_id_for(pp: &PublicationPointSummary) -> String {
    if let Some(node_id) = pp.node_id {
        return format!("node_{node_id}");
    }
    stable_id(
        pp.manifest_rsync_uri
            .as_deref()
            .or(pp.publication_point_rsync_uri.as_deref())
            .unwrap_or("unknown"),
    )
}

fn infer_transport(uri: &str) -> String {
    if uri.starts_with("http://") || uri.starts_with("https://") {
        "rrdp".to_string()
    } else if uri.starts_with("rsync://") {
        "rsync".to_string()
    } else {
        "unknown".to_string()
    }
}

fn uri_host(uri: &str) -> String {
    let without_scheme = uri.split_once("://").map(|(_, rest)| rest).unwrap_or(uri);
    without_scheme
        .split('/')
        .next()
        .filter(|s| !s.is_empty())
        .unwrap_or("unknown")
        .to_string()
}

fn object_type_from_uri(uri: &str) -> String {
    let lower = uri.to_ascii_lowercase();
    for (suffix, kind) in [
        (".mft", "manifest"),
        (".crl", "crl"),
        (".cer", "certificate"),
        (".roa", "roa"),
        (".asa", "aspa"),
        (".gbr", "gbr"),
    ] {
        if lower.ends_with(suffix) {
            return kind.to_string();
        }
    }
    "other".to_string()
}

fn stable_id(value: &str) -> String {
    let digest = Sha256::digest(value.as_bytes());
    hex::encode(&digest[..12])
}

fn json_str<'a>(value: &'a Value, path: &[&str]) -> Option<&'a str> {
    let mut current = value;
    for key in path {
        current = current.get(*key)?;
    }
    current.as_str()
}

fn json_array_len(value: &Value, path: &[&str]) -> Option<u64> {
    let mut current = value;
    for key in path {
        current = current.get(*key)?;
    }
    current.as_array().map(|items| items.len() as u64)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn cursor_round_trip_and_rejects_bad_input() {
        assert_eq!(parse_object_cursor(None).unwrap(), (0, 0));
        let cursor = object_cursor(3, 9);
        assert_eq!(parse_object_cursor(Some(&cursor)).unwrap(), (3, 9));
        assert!(parse_object_cursor(Some("bad")).is_err());
    }

    #[test]
    fn summary_and_object_listing_stream_report() {
        let temp = tempfile::tempdir().expect("tempdir");
        let report_path = temp.path().join("report.json");
        fs::write(
            &report_path,
            r#"{
              "meta":{"validation_time_rfc3339_utc":"2026-06-16T00:00:00Z"},
              "tree":{"warnings":[{}]},
              "publication_points":[{
                "node_id":1,
                "rsync_base_uri":"rsync://repo.example/rpki/",
                "manifest_rsync_uri":"rsync://repo.example/rpki/m.mft",
                "publication_point_rsync_uri":"rsync://repo.example/rpki/",
                "rrdp_notification_uri":"https://repo.example/rrdp/notification.xml",
                "source":"rrdp",
                "repo_sync_source":"rrdp",
                "repo_sync_phase":"rrdp_delta",
                "repo_sync_duration_ms":7,
                "repo_terminal_state":"fresh",
                "warnings":[],
                "objects":[
                  {"rsync_uri":"rsync://repo.example/rpki/m.mft","sha256_hex":"11","kind":"manifest","result":"ok"},
                  {"rsync_uri":"rsync://repo.example/rpki/a.roa","sha256_hex":"22","kind":"roa","result":"error","detail":"bad roa"}
                ]
              }],
              "vrps":[{},{}],
              "aspas":[{}],
              "download_stats":{"eventsTotal":0},
              "queryAudit":{"status":"complete","eventsPath":"validation-events.jsonl"}
            }"#,
        )
        .expect("report");
        let summary = summarize_report(&report_path, "run_0001").expect("summary");
        assert_eq!(
            summary.validation_time.as_deref(),
            Some("2026-06-16T00:00:00Z")
        );
        assert_eq!(summary.publication_points.len(), 1);
        assert_eq!(summary.repos.len(), 1);
        assert_eq!(summary.objects_count, 2);
        assert_eq!(summary.vrps_count, 2);
        assert_eq!(summary.aspas_count, 1);
        assert_eq!(summary.stats.object_type_counts["roa"], 1);
        assert_eq!(summary.stats.reason_counts["bad roa"], 1);

        let page =
            list_report_objects(&report_path, "run_0001", ObjectScope::All, 1, None).expect("page");
        assert_eq!(page.data.len(), 1);
        assert!(page.next_cursor.is_some());
        let page2 = list_report_objects(
            &report_path,
            "run_0001",
            ObjectScope::All,
            10,
            page.next_cursor.as_deref(),
        )
        .expect("page2");
        assert_eq!(page2.data.len(), 1);
        assert_eq!(page2.data[0].object_type, "roa");

        let found = lookup_report_object(
            &report_path,
            "run_0001",
            ObjectScope::All,
            ObjectLookup::Uri("rsync://repo.example/rpki/a.roa".to_string()),
        )
        .expect("lookup")
        .expect("found");
        assert_eq!(found.object.result, "error");
        assert_eq!(found.resolution.mode, "report_scan");
    }
}
