unit class Image::Reference::Audit;

has Str $.subject is required;
has Str $.aspect-ratio = '';
has %.reference-roles;
has %.constraints;

method issues(--> Array) {
    my @issues;
    @issues.push('subject is too short') if $!subject.trim.chars < 8;
    @issues.push('aspect ratio is missing') unless $!aspect-ratio.trim.chars;
    @issues.push('at least one reference role is required') unless %!reference-roles.elems;

    for %!reference-roles.kv -> $id, $role {
        @issues.push("reference {$id} has no role") unless $role.Str.trim.chars;
    }

    @issues.Array;
}

method is-valid(--> Bool) {
    self.issues.elems == 0;
}

method summary(--> Str) {
    my $roles = %!reference-roles.sort.map({ "{.key}={.value}" }).join(', ');
    my $constraints = %!constraints.sort.map({ "{.key}={.value}" }).join(', ');
    (
        "Subject: $!subject",
        "Aspect ratio: $!aspect-ratio",
        "Reference roles: $roles",
        $constraints ?? "Constraints: $constraints" !! Nil
    ).grep(*.defined).join("\n");
}
