//! Archive lifecycle methods for `Archivist`.
//!
//! Phase 3 is **startup-only**: the archive registry is constructed from
//! `dirigent.toml` at boot and not mutated at runtime. Accordingly,
//! `add_archive`, `remove_archive`, and `set_default_archive` all return
//! [`ArchivistError::DynamicRegistryUnsupported`]. The `list_archives`
//! and `get_default_archive` read-paths continue to operate against the
//! new `Vec<Arc<ArchiveRegistration>>` storage.

use std::path::PathBuf;

use crate::coordinator::Archivist;
use crate::error::{ArchivistError, Result};
use crate::registry::FailureMode;

impl Archivist {
    /// **Deprecated in Phase 3.** Archive registry is configured at boot
    /// via `dirigent.toml`; runtime mutation is not supported.
    pub async fn add_archive(&self, _name: String, _path: PathBuf) -> Result<()> {
        Err(ArchivistError::DynamicRegistryUnsupported)
    }

    /// **Deprecated in Phase 3.** Archive registry is configured at boot
    /// via `dirigent.toml`; runtime mutation is not supported.
    pub async fn remove_archive(&self, _name: String, _force: bool) -> Result<()> {
        Err(ArchivistError::DynamicRegistryUnsupported)
    }

    /// List all configured archives. Session counts are reported as `0`
    /// because the Phase 3 multi-backend coordinator does not persist a
    /// per-archive connector index; counts will be reintroduced by the
    /// admin-status query in Task 23.
    pub async fn list_archives(&self) -> Result<Vec<super::types::ArchiveInfo>> {
        let regs = self.registrations.read().await;
        let primary_name = regs
            .iter()
            .filter(|r| {
                r.enabled && r.write_active && r.failure_mode == FailureMode::Required
            })
            .min_by_key(|r| r.read_priority)
            .map(|r| r.name.clone());

        Ok(regs
            .iter()
            .map(|r| super::types::ArchiveInfo {
                name: r.name.clone(),
                path: PathBuf::new(),
                created_at: chrono::Utc::now(),
                session_count: 0,
                is_default: primary_name.as_deref() == Some(r.name.as_str()),
            })
            .collect())
    }

    /// Get the name of the "default" archive — interpreted in Phase 3 as
    /// the enabled, write-active, `Required` backend with the lowest
    /// `read_priority`.
    pub async fn get_default_archive(&self) -> Result<String> {
        let regs = self.registrations.read().await;
        regs.iter()
            .filter(|r| {
                r.enabled && r.write_active && r.failure_mode == FailureMode::Required
            })
            .min_by_key(|r| r.read_priority)
            .map(|r| r.name.clone())
            .ok_or_else(|| ArchivistError::PrimaryUnavailable {
                name: "<default>".into(),
                reason: "no required write-active backend".into(),
            })
    }

    /// **Deprecated in Phase 3.** Archive registry is configured at boot
    /// via `dirigent.toml`; runtime mutation is not supported.
    pub async fn set_default_archive(&self, _name: String) -> Result<()> {
        Err(ArchivistError::DynamicRegistryUnsupported)
    }
}
