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

import static de.srsoftware.umbrella.core.ModuleRegistry.userService;

import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.constants.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

public record Account(long id, String name, String currency, long ownerId) implements Mappable {
	public static Account of(ResultSet rs) throws SQLException {
		var id       = rs.getLong(Field.ID);
		var name     = rs.getString(Field.NAME);
		var owner    = rs.getLong(Field.OWNER);
		var currency = rs.getString(Field.CURRENCY);
		return new Account(id,name,currency,owner);
	}

	@Override
	public Map<String, Object> toMap() {
		var owner = userService().loadUser(ownerId);
		return Map.of(
				Field.ID, id,
				Field.NAME, name,
				Field.CURRENCY, currency,
				Field.OWNER, owner.toMap()
		);
	}

	public Account withId(long newId) {
		return new Account(newId,name,currency,ownerId);
	}
}
