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


import static de.srsoftware.umbrella.core.constants.Field.*;

import de.srsoftware.tools.Mappable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Map;
import org.json.JSONObject;

public class CustomerSettings implements Mappable {
	private static final System.Logger LOG = System.getLogger(CustomerSettings.class.getSimpleName());
	private String footer, header, mailText;
	private HashSet<String> dirtyFields = new HashSet<>();

	public CustomerSettings(String header, String footer, String mailText){
		this.header = header;
		this.footer = footer;
		this.mailText = mailText;
	}

	public CustomerSettings clear(){
		dirtyFields.clear();
		return this;
	}
	public static CustomerSettings empty() {
		return new CustomerSettings("","","");
	}

	public String footer(){
		return footer;
	}

	public String header() {
		return header;
	}

	public boolean isDirty() {
		return !dirtyFields.isEmpty();
	}

	public String mailText(){
		return mailText;
	}

	public static CustomerSettings of(ResultSet rs) throws SQLException {
		var header = rs.getString(DEFAULT_HEADER);
		var footer = rs.getString(DEFAULT_FOOTER);
		var mailText = rs.getString(DEFAULT_MAIL);
		return new CustomerSettings(header,footer,mailText);
	}

	@Override
	public Map<String, Object> toMap() {
		return Map.of(FOOTER,footer,HEAD,header, CONTENT,mailText);
	}

	public CustomerSettings patch(JSONObject json) {
		for (var key : json.keySet()){
			var valid = true;
			switch (key){
				case FOOTER:
					footer = json.getString(key); break;
				case HEAD:
					header = json.getString(key); break;
				default:
					valid = false;
			}
			if (valid) dirtyFields.add(key);
		}
		return this;
	}
}
