//! Registry management and federation support

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// Registry catalog response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Catalog {
    pub repositories: Vec<String>,
}

/// Image tags list
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TagsList {
    pub name: String,
    pub tags: Vec<String>,
}

/// Registry configuration
#[derive(Debug, Clone)]
pub struct RegistryConfig {
    /// Primary registry URL
    pub primary_url: String,
    /// Upstream registries for federation (fallback)
    pub upstream: Vec<String>,
    /// Storage backend (memory, filesystem, s3)
    pub storage: StorageBackend,
    /// Enable authentication
    pub auth_enabled: bool,
}

/// Storage backend type
#[derive(Debug, Clone)]
pub enum StorageBackend {
    Memory,
    Filesystem(String),
    S3 { bucket: String, region: String },
}

impl RegistryConfig {
    /// Create default in-memory configuration
    pub fn default_memory() -> Self {
        Self {
            primary_url: "http://localhost:8084".to_string(),
            upstream: vec![],
            storage: StorageBackend::Memory,
            auth_enabled: false,
        }
    }
}

/// Registry federation for distributed extension management
pub struct RegistryFederation {
    config: RegistryConfig,
    #[allow(dead_code)]
    cache: HashMap<String, Vec<u8>>,
}

impl RegistryFederation {
    /// Create new federation
    pub fn new(config: RegistryConfig) -> Self {
        Self {
            config,
            cache: HashMap::new(),
        }
    }

    /// Get registry configuration
    pub fn config(&self) -> &RegistryConfig {
        &self.config
    }

    /// Check upstream registries for blob
    pub async fn pull_from_upstream(&mut self, digest: &str) -> Result<Vec<u8>, String> {
        // In Phase 4D, upstream federation delegates to actual registry proxying
        // Full implementation with HTTP client to upstream registries comes in Phase 4+
        Err(format!("Blob not found in upstream: {}", digest))
    }
}

/// Repository metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RepositoryMetadata {
    pub name: String,
    pub description: String,
    pub owner: String,
    pub created_at: String,
    pub updated_at: String,
    pub tags_count: u32,
}
