package com.cloudhandson.ddsbackoffice.domain;

import java.time.Instant;
import java.util.List;

/** Inventory observation. An unavailable observation must never be presented as a healthy grant. */
public record DdsGrantInventorySnapshot(
    boolean available,
    List<DdsGrantInventoryEntry> grants,
    Instant observedAt,
    String safeIssue
) {

  public DdsGrantInventorySnapshot {
    grants = grants == null ? List.of() : List.copyOf(grants);
    observedAt = observedAt == null ? Instant.now() : observedAt;
    safeIssue = safeIssue == null ? "" : safeIssue;
  }

  public boolean hasGrant(String grantName) {
    return grants.stream().anyMatch(grant -> grant.grantName().equalsIgnoreCase(grantName));
  }

  public boolean hasGrantee(String dataRole) {
    return grants.stream().anyMatch(grant -> grant.grantee().equalsIgnoreCase(dataRole));
  }
}
