package com.robocubs4205.cubscout.model.scorecard;

import com.robocubs4205.cubscout.model.Game;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.hateoas.Identifiable;

import javax.persistence.*;
import javax.validation.groups.Default;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Entity
public class Scorecard implements Identifiable<Long> {
    @Id
    @GeneratedValue
    private long id;

    @OneToOne(optional = false)
    private Game game;

    @OneToMany(cascade = CascadeType.PERSIST,mappedBy = "scorecard")
    @NotEmpty(groups = {Default.class,Creating.class})
    private Set<ScorecardSection> sections = new HashSet<>();

    @OneToMany(mappedBy = "scorecard")
    private Set<Result> results = new HashSet<>();

    @OneToMany(cascade = CascadeType.PERSIST,mappedBy = "scorecard")
    private Set<RobotRole> robotRoles = new HashSet<>();

    @OneToOne(cascade = CascadeType.PERSIST)
    private RobotRole defaultRole;

    public Scorecard() {
    }

    public Scorecard(long id){
        setId(id);
    }

    public Game getGame() {
        return game;
    }

    public void setGame(Game game) {
        this.game = game;
    }

    public Set<ScorecardSection> getSections() {
        return sections;
    }

    public List<FieldSection> getFields() {
        return sections.stream().filter(section -> section instanceof FieldSection)
                       .map(section -> (FieldSection) section).collect(Collectors.toList());
    }

    @Override
    public Long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Set<Result> getResults() {
        return results;
    }

    public Set<RobotRole> getRobotRoles() {
        return robotRoles;
    }

    public Set<RobotRole> getRoles() {
        return robotRoles;
    }

    public RobotRole getDefaultRole() {
        return defaultRole;
    }

    public void setDefaultRole(RobotRole defaultRole) {
        this.defaultRole = defaultRole;
    }

    public interface Creating{

    }
}

@@ -497,16 +497,47 @@
 neToOne(
+cascade = CascadeType.PERSIST, 
 optional
@@ -587,32 +587,33 @@
 oMany(cascade = 
+%7B
 CascadeType.PERS
@@ -608,32 +608,53 @@
 adeType.PERSIST,
+ CascadeType.REMOVE%7D,
 mappedBy = %22scor
@@ -887,32 +887,33 @@
 oMany(cascade = 
+%7B
 CascadeType.PERS
@@ -916,16 +916,37 @@
 PERSIST,
+ CascadeType.REMOVE%7D,
 mappedBy
@@ -1035,32 +1035,33 @@
 ToOne(cascade = 
+%7B
 CascadeType.PERS
@@ -1063,16 +1063,37 @@
 .PERSIST
+, CascadeType.REMOVE%7D
 )%0A    pr

add cascade types to fields in the scorecard entity to allow it to be deleted