package no.valg.valgnatt.database.hibernate;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import no.valg.valgnatt.database.domene.ValgType;
import org.hibernate.annotations.Immutable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
@Immutable
@Builder
@NoArgsConstructor //Hibernate-krav
@AllArgsConstructor //Builder-krav
public class KommuneId implements java.io.Serializable {

    @Getter
    @Column(name = "VALGAAR", nullable = false)
    private Integer valgaar;

    @Getter
    @Column(name = "KOMMNR", nullable = false, length = 16)
    private String kommnr;

    @Column(name = "VALGTYPE", nullable = false)
    private String valgtype;


    public ValgType getValgtype() {
        return ValgType.valueOf(this.valgtype);
    }

    @Override
    public boolean equals(Object other) {
        return other != null && other.getClass() == this.getClass() && id().equals(((KommuneId) other).id());
    }

    public String id() {
        return valgtype + "-" + valgaar + "-" + kommnr;
    }

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

    public String toString() {
        return id();
    }

}
