//! Node types and IDs
//!

use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Unique identifier for a database node (Entry or Group).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum NodeId {
    /// UUID-based ID (KDBX format)
    Uuid(Uuid),
    /// Integer-based ID (KDB format)
    Int(i32),
}

impl NodeId {
    pub fn new_uuid() -> Self {
        Self::Uuid(Uuid::new_v4())
    }

    pub fn from_uuid(uuid: Uuid) -> Self {
        Self::Uuid(uuid)
    }

    pub fn from_int(id: i32) -> Self {
        Self::Int(id)
    }

    pub fn as_uuid(&self) -> Option<&Uuid> {
        match self {
            NodeId::Uuid(u) => Some(u),
            _ => None,
        }
    }

    /// Create from u32 (KDB format)
    pub fn from_u32(id: u32) -> Self {
        Self::Int(id as i32)
    }

    /// Get as u32 (KDB format)
    pub fn as_u32(&self) -> Option<u32> {
        match self {
            NodeId::Int(i) => Some(*i as u32),
            _ => None,
        }
    }
}

/// Node type indicator
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeType {
    Group,
    Entry,
}

/// Base trait for database nodes (groups and entries).
pub trait Node: Send + Sync {
    fn node_id(&self) -> &NodeId;
    fn node_type(&self) -> NodeType;
    fn title(&self) -> &str;
    fn last_modified(&self) -> i64;
}
