//! Integration tests for SystemBuilder multi-circuit API (Story 13-1).
//!
//! Verifies that a minimal two-circuit system can be built via the builder,
//! finalized, and exposes the expected circuit topology.

use entropyk::{CircuitId, SystemBuilder};
use entropyk_components::{Component, ComponentError, JacobianBuilder, ResidualVector};

struct MockComponent {
    n_eqs: usize,
}

impl Component for MockComponent {
    fn compute_residuals(
        &self,
        _state: &[f64],
        _residuals: &mut ResidualVector,
    ) -> Result<(), ComponentError> {
        Ok(())
    }

    fn jacobian_entries(
        &self,
        _state: &[f64],
        _jacobian: &mut JacobianBuilder,
    ) -> Result<(), ComponentError> {
        Ok(())
    }

    fn n_equations(&self) -> usize {
        self.n_eqs
    }

    fn get_ports(&self) -> &[entropyk_components::ConnectedPort] {
        &[]
    }
}

#[test]
fn test_builder_two_circuits_build_and_finalize() {
    let system = SystemBuilder::new()
        .component_in_circuit("a", Box::new(MockComponent { n_eqs: 1 }), CircuitId::ZERO)
        .unwrap()
        .component_in_circuit("b", Box::new(MockComponent { n_eqs: 1 }), CircuitId::ZERO)
        .unwrap()
        .component_in_circuit("c", Box::new(MockComponent { n_eqs: 1 }), CircuitId(1))
        .unwrap()
        .component_in_circuit("d", Box::new(MockComponent { n_eqs: 1 }), CircuitId(1))
        .unwrap()
        .edge("a", "b")
        .unwrap()
        .edge("c", "d")
        .unwrap()
        .build()
        .expect("build should succeed");

    assert_eq!(system.circuit_count(), 2);
    assert_eq!(system.circuit_nodes(CircuitId::ZERO).count(), 2);
    assert_eq!(system.circuit_nodes(CircuitId(1)).count(), 2);
    assert_eq!(system.node_count(), 4);
    assert_eq!(system.edge_count(), 2);
}
