package cz.muni.fi.pa165.soccermanager.entity;

import java.util.List;

import lombok.*;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * Team entity class
 *
 * @author Petr Adamec (uco: 485584)
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor
@Entity
public class Team {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NonNull
    private String name;

    @NotNull
    private String ground;

    private int points;

    @OneToMany(mappedBy = "team", fetch = FetchType.LAZY)
    private List<Player> players;

    @ManyToOne
    @JoinColumn(name = "leagueId")
    private League league;

    @ManyToOne
    @JoinColumn(name = "championshipId")
    private Championship championship;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "team_match",
        joinColumns=@JoinColumn(name="teamId", referencedColumnName="id"),
        inverseJoinColumns=@JoinColumn(name="matchId", referencedColumnName="id"))
    private List<Match> matches;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userId")
    private User user;

    public Team(String name, String ground) {
        this.name = name;
        this.ground = ground;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Team)) return false;

        Team team = (Team) o;

        if (!getName().equals(team.getName())) return false;
        if (!getGround().equals(team.getGround())) return false;
        if (getPlayers() != null ? !getPlayers().equals(team.getPlayers()) : team.getPlayers() != null) return false;
        if (getLeague() != null ? !getLeague().equals(team.getLeague()) : team.getLeague() != null) return false;
        if (getChampionship() != null ? !getChampionship().equals(team.getChampionship()) : team.getChampionship() != null)
            return false;
        if (getMatches() != null ? !getMatches().equals(team.getMatches()) : team.getMatches() != null) return false;
        return getUser() != null ? getUser().equals(team.getUser()) : team.getUser() == null;
    }

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

    public void removeChampionship() {
        this.championship = null;
    }
}
