package de.thpeetz.kontor.tysc.data;

import java.util.LinkedList;
import java.util.List;

import de.thpeetz.kontor.common.data.AbstractEntity;
import jakarta.annotation.Nullable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Index;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotEmpty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Getter
@Setter
@ToString
@EqualsAndHashCode(callSuper=false)
@Entity
@Table(indexes = {@Index(columnList = "name")},
        uniqueConstraints = {@UniqueConstraint(columnNames = { "name" })}
)
public class Sport extends AbstractEntity {

    @NotEmpty
    @Column(unique = true)
    private String name;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "sport")
    @Nullable
    private List<Team> teams = new LinkedList<>();

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "sport")
    @Nullable
    private List<FieldPosition> positions = new LinkedList<>();

    public String updateName(String value) {
        if (!this.getName().equals(value)) {
            this.setName(value);
            log.info("update name");
            return "updated " + this.getId() + " with " + value;
        }
        return "no changes for " + this.getId();
    }
}
