// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.organization;

import com.yahoo.vespa.hosted.controller.api.identifiers.PropertyId;

import java.util.Objects;
import java.util.Optional;

/**
 * Represents an issue which needs to reported, typically from the controller, to a responsible party,
 * the identity of which is determined by the propertyId and, possibly, assignee fields.
 *
 * @author jvenstad
 */
public class Issue {

    private final String summary;
    private final String description;
    private final String label;
    private final User assignee;
    private final PropertyId propertyId;

    private Issue(String summary, String description, String label, User assignee, PropertyId propertyId) {
        if (summary.isEmpty()) throw new IllegalArgumentException("Issue summary can not be empty!");
        if (description.isEmpty()) throw new IllegalArgumentException("Issue description can not be empty!");
        Objects.requireNonNull(propertyId, "An issue must belong to a property!");

        this.summary = summary;
        this.description = description;
        this.label = label;
        this.assignee = assignee;
        this.propertyId = propertyId;
    }

    public Issue(String summary, String description, PropertyId propertyId) {
        this(summary, description, null, null, propertyId);
    }

    public Issue append(String appendage) {
        return new Issue(summary, description + appendage, label, assignee, propertyId);
    }

    public Issue withLabel(String label) {
        return new Issue(summary, description, label, assignee, propertyId);
    }

    public Issue withAssignee(User assignee) {
        return new Issue(summary, description, label, assignee, propertyId);
    }

    public Issue withPropertyId(PropertyId propertyId) {
        return new Issue(summary, description, label, assignee, propertyId);
    }

    public String summary() {
        return summary;
    }

    public String description() {
        return description;
    }

    public Optional<String> label() {
        return Optional.ofNullable(label);
    }

    public Optional<User> assignee() {
        return Optional.ofNullable(assignee);
    }

    public PropertyId propertyId() {
        return propertyId;
    }

}

@@ -731,16 +731,45 @@
 pertyId;
+%0A    private final Type type;
 %0A%0A    pr
@@ -858,32 +858,43 @@
 rtyId propertyId
+, Type type
 ) %7B%0A        if (
@@ -1345,24 +1345,50 @@
 propertyId;%0A
+        this.type = type;%0A
     %7D%0A%0A    p
@@ -1509,32 +1509,45 @@
 null, propertyId
+, Type.defect
 );%0A    %7D%0A%0A    pu
@@ -1662,32 +1662,38 @@
 gnee, propertyId
+, type
 );%0A    %7D%0A%0A    pu
@@ -1711,13 +1711,8 @@
 with
-Label
 (Str
@@ -1790,32 +1790,38 @@
 gnee, propertyId
+, type
 );%0A    %7D%0A%0A    pu
@@ -1839,16 +1839,8 @@
 with
-Assignee
 (Use
@@ -1919,32 +1919,38 @@
 gnee, propertyId
+, type
 );%0A    %7D%0A%0A    pu
@@ -1964,16 +1964,17 @@
 sue with
+(
 Property
@@ -1975,18 +1975,18 @@
 opertyId
-(P
+ p
 ropertyI
@@ -1982,35 +1982,149 @@
 d propertyId
- propertyId
+) %7B%0A        return new Issue(summary, description, label, assignee, propertyId, type);%0A    %7D%0A%0A    public Issue with(Type type
 ) %7B%0A        
@@ -2189,16 +2189,22 @@
 opertyId
+, type
 );%0A    %7D
@@ -2586,10 +2586,195 @@
 %0A    %7D%0A%0A
+    public Type type() %7B%0A        return type;%0A    %7D%0A%0A%0A    public enum Type %7B%0A%0A        defect, // A defect which needs fixing.%0A        task    // A task the humans must perform.%0A%0A    %7D%0A%0A
 %7D%0A

Add support for type