// Copyright (C) 2025  The Software Heritage developers
// See the AUTHORS file at the top-level directory of this distribution
// License: GNU General Public License version 3, or any later version
// See top-level LICENSE file for more information

use std::sync::atomic::AtomicU64;

use serde::Deserialize;

#[cfg(feature = "arrow")]
pub mod bvgraph_to_parquet;
pub mod cherrypicks;
pub mod commit2vuln;
#[cfg(feature = "arrow")]
pub mod commit2vuln_parquet_to_bvgraph;
pub mod db;
pub mod git;
pub mod kb;
pub mod multimap;
pub mod packages;
pub mod reference_metrics;
pub mod szz;
pub mod traversals;
pub mod utils;
#[cfg(feature = "arrow")]
pub mod vuln2commit_parquet;
#[cfg(feature = "arrow")]
pub mod vulnerabilities;

#[derive(Debug, Default)]
pub struct Stats {
    pub total_invalid_commit_ids: AtomicU64,
    pub total_unknown_commits: AtomicU64,
    pub total_vulnerable_commits_x_vulnerabilities_x_ranges: AtomicU64,
    pub total_vulnerable_commits_x_vulnerabilities: AtomicU64,
    pub total_successful_ranges: AtomicU64,
    pub total_successful_documents_with_git_ranges: AtomicU64,
    pub total_documents_without_git_ranges: AtomicU64,
}

#[repr(usize)]
#[derive(Debug, PartialEq, Eq, Deserialize, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum EventType {
    #[serde(alias = "INTRODUCED")]
    Introduced,
    #[serde(alias = "FIXED")]
    Fixed,
    #[serde(alias = "LAST_AFFECTED")]
    LastAffected,
    #[serde(alias = "LIMIT")]
    Limit,
}

/// The current version of swh-graph-provenance.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg(feature = "arrow")]
/// Returns metadata to write in the header of produced parquet files
pub fn parquet_metadata<G: swh_graph::graph::SwhGraph>(
    graph: &G,
) -> Vec<dataset_writer::parquet::file::metadata::KeyValue> {
    use dataset_writer::parquet::format::KeyValue;
    vec![
        KeyValue {
            key: "swh_graph_version".into(),
            value: Some(swh_graph::VERSION.into()),
        },
        KeyValue {
            key: "swh_osv".into(),
            value: Some(crate::VERSION.into()),
        },
        KeyValue {
            key: "swh_graph_path".into(),
            value: Some(graph.path().display().to_string()),
        },
        KeyValue {
            key: "creation_date".into(),
            value: Some(chrono::Local::now().to_rfc3339()),
        },
    ]
}

#[derive(
    Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct VulnerabilityId(pub u64);
