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

import static de.srsoftware.umbrella.core.constants.Field.NAME;
import static java.text.MessageFormat.format;

import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.constants.Field;
import java.security.InvalidParameterException;
import java.util.Map;

public enum Permission implements Mappable {
	OWNER(1,true),
	EDIT(2,true),
	ASSIGNEE(3,true),
	READ_ONLY(4,false);

	private final int code;
	private final boolean mayWrite;

	Permission(int code, boolean mayWrite){
		this.code = code;
		this.mayWrite = mayWrite;
	}

	public int code(){
		return code;
	}

	public static Permission of(int code){
		for (var p : Permission.values()){
			if (p.code == code) return p;
		}
		throw new InvalidParameterException(format("{0} is not a valid permission code",code));
	}

	@Override
	public Map<String, Object> toMap() {
		return Map.of(NAME,name(), Field.CODE,code);
	}

	public boolean mayWrite() {
		return mayWrite;
	}
}
