//! VPC (Virtual Private Cloud) types

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

/// Unique identifier for a VPC
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct VpcId(Uuid);

impl VpcId {
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }

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

    pub fn as_uuid(&self) -> &Uuid {
        &self.0
    }
}

impl Default for VpcId {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for VpcId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// VPC status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum VpcStatus {
    #[default]
    Provisioning,
    Active,
    Updating,
    Deleting,
    Error,
}


/// Virtual Private Cloud
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Vpc {
    pub id: VpcId,
    pub org_id: String,
    pub project_id: String,
    pub name: String,
    pub description: Option<String>,
    pub cidr_block: String,
    pub status: VpcStatus,
    pub created_at: u64,
    pub updated_at: u64,
}

impl Vpc {
    pub fn new(
        name: impl Into<String>,
        org_id: impl Into<String>,
        project_id: impl Into<String>,
        cidr_block: impl Into<String>,
    ) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self {
            id: VpcId::new(),
            org_id: org_id.into(),
            project_id: project_id.into(),
            name: name.into(),
            description: None,
            cidr_block: cidr_block.into(),
            status: VpcStatus::Provisioning,
            created_at: now,
            updated_at: now,
        }
    }
}

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

    #[test]
    fn test_vpc_creation() {
        let vpc = Vpc::new("test-vpc", "org-1", "proj-1", "10.0.0.0/16");
        assert_eq!(vpc.name, "test-vpc");
        assert_eq!(vpc.cidr_block, "10.0.0.0/16");
        assert_eq!(vpc.status, VpcStatus::Provisioning);
    }
}
