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


import static de.srsoftware.umbrella.user.model.DbUser.Permission.*;
import static de.srsoftware.umbrella.user.model.DbUser.Permission.IMPERSONATE;
import static de.srsoftware.umbrella.user.model.DbUser.Permission.LIST_USERS;
import static de.srsoftware.umbrella.user.model.DbUser.Permission.MANAGE_LOGIN_SERVICES;

import de.srsoftware.umbrella.core.model.EmailAddress;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.util.Map;
import java.util.Set;

public class DbUser extends UmbrellaUser {

	public enum Permission {
		CREATE_USERS,
		UPDATE_USERS,
		DELETE_USERS,
		LIST_USERS,
		IMPERSONATE,
		MANAGE_LOGIN_SERVICES
	}

	public static Set<Permission> ADMIN_PERMISSIONS = Set.of(CREATE_USERS, UPDATE_USERS, DELETE_USERS, IMPERSONATE, MANAGE_LOGIN_SERVICES,LIST_USERS);

	private final Set<Permission> permissions;
	private final Password hashedPass;
	private final Long lastLogoff;

	public DbUser(long id, String name, EmailAddress email, Password hashedPassword, String theme, String languageCode, Set<Permission> permissions, Long lastLogoff) {
		super(id, name, email, theme, languageCode);
		this.hashedPass = hashedPassword;
		this.permissions = permissions;
		this.lastLogoff = lastLogoff;
	}

	public Password hashedPassword(){
		return hashedPass;
	}

	public Long lastLogoff() {
		return lastLogoff;
	}

	public boolean may(Permission permission){
		return permissions.contains(permission);
	}

	public Set<Permission> permissions() {
		return permissions;
	}

	@Override
	public Map<String, Object> toMap() {
		var map = super.toMap();
		map.put("permissions",permissions);
		return map;
	}
}
