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

import lombok.*;

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

/**
 * Match entity class
 *
 * @author Petr Balazy (uco: 527362)
 */
@Getter
@Setter
@AllArgsConstructor
@RequiredArgsConstructor
@NoArgsConstructor
@Entity(name = "Matches")
public class Match {

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

    @NonNull
    private LocalDate startDate;

    private String playPlace;

    @NotNull
    @ManyToOne
    private Team challenger;

    @NotNull
    @ManyToOne
    private Team defender;

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

    private Integer defenderGoals;

    private Integer challengerGoals;

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

        Match match = (Match) o;

        if (!getStartDate().equals(match.getStartDate())) return false;
        if (getPlayPlace() != null ? !getPlayPlace().equals(match.getPlayPlace()) : match.getPlayPlace() != null)
            return false;
        if (getChallenger() != null ? !getChallenger().equals(match.getChallenger()) : match.getChallenger() != null)
            return false;
        if (getDefender() != null ? !getDefender().equals(match.getDefender()) : match.getDefender() != null)
            return false;
        return getLeague() != null ? getLeague().equals(match.getLeague()) : match.getLeague() == null;
    }

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