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

import static de.srsoftware.umbrella.core.Util.mapMarkdown;
import static de.srsoftware.umbrella.core.constants.Field.*;
import static java.time.ZoneOffset.UTC;

import de.srsoftware.tools.Mappable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Map;

public class Note implements Mappable {
	private long authorId, id;
	private String entityId, module, text;
	private  LocalDateTime timestamp;

	public Note(long id, String module, String entityId, long authorId, String text, LocalDateTime timestamp){
		this.id = id;
		this.module = module;
		this.entityId = entityId;
		this.authorId = authorId;
		this.text = text;
		this.timestamp = timestamp;
	}

	public long authorId(){
		return authorId;
	}

	public String entityId(){
		return entityId;
	}

	public Note entityId(String newVal){
		entityId = newVal;
		return this;
	}

	public long id(){
		return id;
	}

	public String module(){
		return module;
	}

	public static Note of(ResultSet rs) throws SQLException {
		return new Note(
				rs.getLong(ID),
				rs.getString(MODULE),
				rs.getString(ENTITY_ID),
				rs.getLong(USER_ID),
				rs.getString(NOTE),
				LocalDateTime.ofEpochSecond(rs.getLong(TIMESTAMP),0, UTC)
		);
	}

	public String text() {
		return text;
	}

	public LocalDateTime timestamp(){
		return timestamp;
	}

	@Override
	public Map<String, Object> toMap() {
		return Map.of(
				ID,id,
				MODULE,module,
				ENTITY_ID,entityId,
				USER_ID,authorId,
				TEXT,mapMarkdown(text),
				TIMESTAMP,timestamp.withNano(0)
		);
	}
}
