/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;

import static java.text.MessageFormat.format;

import java.util.Collection;
import java.util.Objects;

public abstract class Message<T> {
	private final Collection<Attachment> attachments;
	private final T body, subject;
	private final UmbrellaUser sender;
	private long utcTime;

	public Message(UmbrellaUser sender, T subject, T body, Collection<Attachment> attachments){
		this.sender      = sender;
		this.subject     = subject;
		this.body        = body;
		this.attachments = attachments;
		this.utcTime     = System.currentTimeMillis();
	}

	public Collection<Attachment> attachments(){
		return attachments;
	}

	public T body(){
		return body;
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Message<?> message)) return false;
		return Objects.equals(sender, message.sender) && Objects.equals(subject, message.subject) && Objects.equals(body, message.body) && Objects.equals(attachments, message.attachments);
	}

	@Override
	public int hashCode() {
		return Objects.hash(subject, body, attachments);
	}

	public UmbrellaUser sender(){
		return sender;
	}

	public T subject(){
		return subject;
	}

	@Override
	public String toString() {
		return format("{0}(from: {1}), subject: {2}",getClass().getSimpleName(),sender,subject);
	}

	public long utcTime() {
		return utcTime;
	}

	public Message<T> utcTime(long newValue){
		utcTime = newValue;
		return this;
	}
}
