package premium_store.model;

import javax.persistence.*;
import java.util.*;
import java.util.stream.Collectors;

@Entity
@Table(name = "NATIONS")
public class Nation {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "nation")
    private String nation;

    @OneToMany(mappedBy = "nation", fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
    private List<Tank> tanksOfThisNation = new ArrayList<>();

    public Nation() {
    }

    public Nation(String nation) {
        this.nation = nation;
    }

    //возвращает id
    public Long getId() {
        return id;
    }

    //возвращает нацию
    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public List<Tank> getTanksOfThisNation(){
        return tanksOfThisNation;
    }

    public void setTanksOfThisNation(Tank newTank){
        this.tanksOfThisNation.add(newTank);
    }

    //метод для сравнения
    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;

        if (o == null || getClass() != o.getClass())
            return false;

        Nation nation = (Nation) o;

        return Objects.equals(id, nation.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    //преобразование данных по объекту в строчку
    @Override
    public String toString() {
        return "Nation{" +
                "id=" + id +
                ", nation='" + nation + '\'' +
                ", tanksOfThisNation='" + tanksOfThisNation.stream().map(Object::toString).collect(Collectors.joining(", ")) + '\'' +
                '}';
    }
}

