package com.livingworld.modules;

import com.livingworld.core.services.ServiceKey;
import com.livingworld.core.services.ServiceRegistry;

/**
 * Dependency container passed to modules during initialisation.
 *
 * <p>This class provides controlled access to registered services through the
 * {@link ServiceRegistry}. Modules should only request the services they are
 * allowed to use via this context.</p>
 */
public final class ModuleContext {

    private final ServiceRegistry services;

    /**
     * Creates a new ModuleContext with the given service registry.
     *
     * @param services the service registry (must not be null)
     * @throws IllegalArgumentException if services is null
     */
    public ModuleContext(ServiceRegistry services) {
        if (services == null) {
            throw new IllegalArgumentException("services must not be null");
        }
        this.services = services;
    }

    /**
     * Returns the service associated with the given key.
     *
     * @param <T>   the service type
     * @param key   the service key (must not be null)
     * @return the registered service
     * @throws IllegalArgumentException if key is null or services are missing
     */
    public <T> T getService(ServiceKey<T> key) {
        return services.get(key);
    }

    /**
     * Returns whether a service with the given key has been registered.
     *
     * @param key the service key (must not be null)
     * @return true if the service is registered
     */
    public boolean hasService(ServiceKey<?> key) {
        return services.isRegistered(key);
    }
}