use crate::{Context, Html, LiveView};
use std::fmt;

/// Stable server-owned identifier for a nested LiveView instance.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NestedLiveViewId(String);

impl NestedLiveViewId {
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for NestedLiveViewId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl From<&str> for NestedLiveViewId {
    fn from(value: &str) -> Self {
        Self::new(value)
    }
}

impl From<String> for NestedLiveViewId {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

/// Runtime lifecycle state for one nested LiveView.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NestedLiveViewState {
    /// Child is mounted and accepts scoped events.
    Mounted,
    /// Child keeps server state but does not accept browser events.
    Suspended,
    /// Child has terminated and no longer accepts events or renders.
    Terminated,
}

/// Public snapshot of one nested LiveView runtime boundary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NestedLiveViewSnapshot {
    pub id: NestedLiveViewId,
    pub target: String,
    pub state: NestedLiveViewState,
    pub route_path: String,
}

pub(crate) struct NestedLiveViewInstance {
    pub(crate) view: Box<dyn LiveView>,
    pub(crate) ctx: Context,
    pub(crate) state: NestedLiveViewState,
    pub(crate) last_render: Option<Html>,
}

impl NestedLiveViewInstance {
    pub(crate) fn snapshot(&self, id: NestedLiveViewId) -> NestedLiveViewSnapshot {
        NestedLiveViewSnapshot {
            id,
            target: self.ctx.target_dom_id().to_string(),
            state: self.state,
            route_path: self.ctx.route_path().to_string(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{NestedLiveViewId, NestedLiveViewState};

    #[test]
    fn nested_live_view_id_round_trips() {
        let id = NestedLiveViewId::new("sidebar");
        assert_eq!(id.as_str(), "sidebar");
        assert_eq!(id.to_string(), "sidebar");
        assert_eq!(NestedLiveViewId::from("sidebar"), id);
    }

    #[test]
    fn nested_live_view_state_is_copyable() {
        let state = NestedLiveViewState::Mounted;
        assert_eq!(state, NestedLiveViewState::Mounted);
    }
}
