/* © SRSoftware 2025 */
package de.srsoftware.umbrella.journal;

import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingField;
import static de.srsoftware.umbrella.journal.Constants.CONFIG_DATABASE;
import static de.srsoftware.umbrella.messagebus.MessageBus.messageBus;
import static java.lang.System.Logger.Level.DEBUG;

import de.srsoftware.configuration.Configuration;
import de.srsoftware.umbrella.core.BaseHandler;
import de.srsoftware.umbrella.core.ModuleRegistry;
import de.srsoftware.umbrella.messagebus.EventListener;
import de.srsoftware.umbrella.messagebus.events.Event;


public class JournalModule extends BaseHandler implements EventListener {

	private final JournalDb journalDb;

	public JournalModule(Configuration config){
		super();
		var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingField(CONFIG_DATABASE));
		journalDb = new SqliteDb(connect(dbFile));
		ModuleRegistry.add(this);
		messageBus().register(this);
	}

	@Override
	public void onEvent(Event<?> event) {
		LOG.log(DEBUG,"{0} @ {1} (by {2})",event.eventType(),event.module(),event.initiator().name());
		journalDb.logEvent(event);
	}
}
