package de.thpeetz.kontor.admin.data;

import de.thpeetz.kontor.common.data.AbstractEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@Getter
@Setter
@ToString
@Entity
@Table(indexes = @Index(columnList = "userName"), uniqueConstraints = @UniqueConstraint(columnNames = {"userName"}))
public class Profile extends AbstractEntity {

    private String firstName;

    private String lastName;

    @NotEmpty
    private String userName;

    private String email;

    private String password;

    private boolean enabled;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "profile")
    @Nullable
    private List<Assignment> assignments = new LinkedList<>();

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "profile")
    @Nullable
    private List<Token> tokens = new LinkedList<>();

    public String getFullName() {
        StringBuilder fullNameBuilder = new StringBuilder();
        if (firstName != null) {
            fullNameBuilder.append(firstName);
        }
        if (lastName != null) {
            if (!fullNameBuilder.isEmpty()) {
                fullNameBuilder.append(" ");
            }
            fullNameBuilder.append(lastName);
        }
        return fullNameBuilder.toString();
    }
}
