use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VulnRef {
    pub id: String,
    pub source: String,
    pub severity: Option<String>,
    pub url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SbomEntry {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<bson::oid::ObjectId>,
    pub repo_id: String,
    pub name: String,
    pub version: String,
    pub package_manager: String,
    pub license: Option<String>,
    pub purl: Option<String>,
    pub known_vulnerabilities: Vec<VulnRef>,
    #[serde(with = "super::serde_helpers::bson_datetime")]
    pub created_at: DateTime<Utc>,
    #[serde(with = "super::serde_helpers::bson_datetime")]
    pub updated_at: DateTime<Utc>,
}

impl SbomEntry {
    pub fn new(repo_id: String, name: String, version: String, package_manager: String) -> Self {
        let now = Utc::now();
        Self {
            id: None,
            repo_id,
            name,
            version,
            package_manager,
            license: None,
            purl: None,
            known_vulnerabilities: Vec::new(),
            created_at: now,
            updated_at: now,
        }
    }
}
