//! Minimal stateful Component used to demonstrate a warm Actor Store.
//!
//! Invoke returns an eight-byte little-endian counter. The value survives
//! calls to the same Actor but resets on `init`, restart, or process recovery.

use core::sync::atomic::{AtomicU64, Ordering};

mod bindings {
    // Bindings are generated from `wit/world.wit` plus materialized
    // `wit/deps`; WIT is a compile-time ABI contract, not downloaded at run
    // time and not executed as code.
    wit_bindgen::generate!({
        path: "wit",
        world: "counter-component",
    });
}

static COUNTER: AtomicU64 = AtomicU64::new(0);

struct Counter;

impl bindings::Guest for Counter {
    fn init(_config: Vec<u8>) -> Result<(), bindings::ServiceError> {
        // Calls are serialized by the host Actor, so this component intentionally
        // uses one mutable global to make resident memory observable in tests.
        COUNTER.store(0, Ordering::Relaxed);
        Ok(())
    }

    fn invoke(_input: Vec<u8>) -> Result<Vec<u8>, bindings::ServiceError> {
        let next = COUNTER.fetch_add(1, Ordering::Relaxed).wrapping_add(1);
        Ok(next.to_le_bytes().to_vec())
    }
}

bindings::export!(Counter with_types_in bindings);
