package samuelb.capripol;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import samuelb.capripol.Services.UserDetailsServiceImpl;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Objects;
/*
Entity representing a users base rating, has a User and a Focus
This is not to be confused with a users actual rating, the
actual rating is calculated on the fly with a stored procedure.
This rating represents a base value which can be edited and the summation
of sighting values is added to it.
 */
@Entity(name = "Ratings")
public class Rating {

    //Uses the RatingKey as its ID
    @EmbeddedId
    @GeneratedValue(strategy = GenerationType.AUTO)
    RatingKey id;

    @ManyToOne
    @MapsId("userID")
    @JoinColumn(name="userID")
    private User user;

    @ManyToOne
    @MapsId("focusID")
    @JoinColumn(name="focusID")
    private Focus focus;

    private BigDecimal baseRating;

    private static Logger logger = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    protected Rating(){}

    public Rating(User user, Focus focus, BigDecimal baseRating) {
        this.user = user;
        this.focus = focus;
        this.baseRating = baseRating;
        this.id = new RatingKey(user.getUserId(), focus.getFocusId());
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Focus getFocus() {
        return focus;
    }

    public void setFocus(Focus focus) {
        this.focus = focus;
    }

    public BigDecimal getBaseRating() {
        return baseRating;
    }

    public void setBaseRating(BigDecimal baseRating) {
        this.baseRating = baseRating;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass())
            return false;

        Rating that = (Rating) o;
        return Objects.equals(user, that.user) &&
                Objects.equals(focus, that.focus);
    }

    @Override
    public int hashCode() {
        return Objects.hash(user, focus);
    }

}
