use dbnest_core::{
    Engine, InstanceSpec, InstanceStatus, PostgresSpec, provision, remove_instance,
    restart_instance, start_instance, status_one, stop_instance,
};

#[test]
#[ignore = "requires a running Docker daemon and pulls postgres:16-alpine if missing"]
fn postgres_docker_lifecycle_smoke_test() {
    let password = format!("dbnest-{}", uuid::Uuid::new_v4().simple());
    let inst = provision(InstanceSpec {
        engine: Engine::Postgres,
        sqlite: None,
        postgres: Some(PostgresSpec {
            user: "dbnest".into(),
            password,
            db: "dbnest".into(),
            image: None,
        }),
    })
    .expect("postgres docker provisioning should succeed");

    let id = inst.id.clone();
    let result = (|| -> dbnest_core::Result<()> {
        let container = inst
            .container
            .as_ref()
            .expect("postgres has container info");
        assert!(container.volume.is_some());

        let report = status_one(&id)?;
        assert!(matches!(report.status, InstanceStatus::Running));

        stop_instance(&id)?;
        let report = status_one(&id)?;
        assert!(matches!(report.status, InstanceStatus::Stopped));

        start_instance(&id)?;
        restart_instance(&id)?;

        Ok(())
    })();

    let cleanup = remove_instance(&id, true, true);
    assert!(cleanup.is_ok(), "cleanup failed: {cleanup:?}");
    result.unwrap();
}
