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

import static de.srsoftware.umbrella.core.constants.Field.*;
import static de.srsoftware.umbrella.core.constants.Module.PROJECT;
import static de.srsoftware.umbrella.core.model.Translatable.t;
import static de.srsoftware.umbrella.messagebus.events.Event.EventType.MEMBER_ADDED;

import de.srsoftware.umbrella.core.constants.Field;
import de.srsoftware.umbrella.core.model.*;
import java.util.Collection;
import java.util.Map;


public class ProjectEvent extends Event<Project>{
	private final UmbrellaUser newMember;

	public ProjectEvent(UmbrellaUser initiator, Project project, EventType type){
		super(initiator, PROJECT, project, type);
		newMember = null;
	}

	public ProjectEvent(UmbrellaUser initiator, Project project, Map<String, Object> oldData){
		super(initiator, PROJECT, project, oldData);
		newMember = null;
	}

	public ProjectEvent(UmbrellaUser initiator, Project project, UmbrellaUser newMember){
		super(initiator, PROJECT, project, MEMBER_ADDED);
		this.newMember = newMember;
	}

	@Override
	public Collection<UmbrellaUser> audience() {
		return payload().members().values().stream().map(Member::user).toList();
	}

	@Override
	public Translatable describe() {
		return switch (eventType()){
			case CREATE -> describeCreate();
			case DELETE -> t("The project '{project}' has been deleted by {user}", Field.PROJECT, payload().name(), USER, initiator().name());
			case MEMBER_ADDED -> describeMemberAdded();
			case UPDATE -> describeUpdate();
		};
	}

	private Translatable describeCreate() {
		var head =  t("You have been added to the new project '{project}', created by {user}:\n\n{body}", Field.PROJECT, payload().name(), BODY, payload().description(), USER, initiator().name());
		return t("{head}\n\n{link}","head",head,"link",link());
	}

	private Translatable describeMemberAdded() {
		var head = t("'{name}' has been added to '{object}' by '{user}'.",NAME,newMember.name(),Field.OBJECT,payload().name(),USER,initiator().name());
		return t("{head}\n\n{link}","head",head,"link",link());
	}

	private Translatable describeUpdate() {
		var head = t("Changes in project '{project}':\n\n{body}",Field.PROJECT,payload().name(),BODY,diff().orElse(""));
		return t("{head}\n\n{link}","head",head,"link",link());
	}

	@Override
	protected Map<String, Object> filter(Map<String, Object> map) {
		map.remove(MEMBERS);
		return super.filter(map);
	}

	@Override
	public boolean isIntendedFor(UmbrellaUser user) {
		for (var member : payload().members().values()){
			if (member.user().equals(user)) return true;
		}
		return false;
	}

	private Translatable link() {
		return t("You can view/edit this project at {base_url}/project/{id}/view",ID,payload().id());
	}

	@Override
	public Translatable subject() {
		return switch (eventType()){
			case CREATE -> t("The project '{project}' has been created", Field.PROJECT, payload().name());
			case DELETE -> t("The project '{project}' has been deleted",Field.PROJECT, payload().name());
			case MEMBER_ADDED, UPDATE -> t("Project '{project}' was edited",Field.PROJECT,payload().name());
		};
	}}
