/* © SRSoftware 2025 */
package de.srsoftware.umbrella.messagebus.events;

import static de.srsoftware.umbrella.core.constants.Field.*;
import static de.srsoftware.umbrella.core.model.Translatable.t;

import de.srsoftware.umbrella.core.ModuleRegistry;
import de.srsoftware.umbrella.core.api.Owner;
import de.srsoftware.umbrella.core.model.*;
import java.util.Collection;
import java.util.List;

public class ItemEvent extends Event<Item>{
	public ItemEvent(UmbrellaUser initiator, String module, Item item, EventType type) {
		super(initiator, module, item, type);
	}

	@Override
	public Collection<UmbrellaUser> audience() {
		Owner owner = payload().location().resolve().owner().resolve();
		if (owner instanceof UmbrellaUser user) return List.of(user);
		if (owner instanceof Company company) return ModuleRegistry.companyService().getMembers(company.id());
		return List.of();
	}

	@Override
	public Translatable describe() {
		return switch (eventType()){
			case CREATE -> describeCreate();
			case null, default -> null;
		};
	}

	private Translatable describeCreate() {
		var loc  = payload().location().resolve().name();
		return t("{user} added \"{item}\" to \"{location}\"", USER,initiator().name(), ITEM, payload().name(), LOCATION, loc);
	}


	@Override
	public Translatable subject() {
		var loc  = payload().location().resolve().name();
		return switch (eventType()){
			case CREATE -> t("A new item has been added to \"{location}\":",LOCATION,loc);
			case null, default -> null;
		};
	}
}
