package com.capgemini.reports.database.javabean;

import com.capgemini.framework.common.activedirectory.ActiveDirectory;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.sql.Date;

/** Persisted object in table REP_USER. **/
@Getter
@Setter
@ToString
public class DbUser implements Serializable, IPersistedObject {

    /** Required by serialization. **/
    private static final long serialVersionUID = 1L;

    /** Unique identifier recommended for using all features from Hibernate. **/
    private Long id;

    /** User login: must be unique in database. **/
    private String login;

    /** User email (synchronized with the LDAP). **/
    private String email;

    /** User surname (synchronized with the LDAP). **/
    private String surname;

    /** User first name (synchronized with the LDAP). **/
    private String firstname;

    /** Date of the last connection of the user, matching with the last LDAP data synchronization. **/
    private Date lastConnectionDate;

    /** user role, default to false. **/
    private String isAdmin = "FALSE";

    /** JAVA convention requires an empty constructor for each JAVA bean. isAdmin **/
    public DbUser() {
        // Nothing to see, move along...
    }

    /**
     * Returns true if both this DB entry and the provided user data match (all fields must be equal).
     * 
     * @param user the {@link UserData} to be compared to this
     * @return true if they are the same, false otherwise
     */
    public boolean sameAs(final UserData user) {
        if (user == null) {
            return false;
        }
        boolean loginOk = login != null && login.equals(user.login);
        boolean emailOk = email != null && email.equals(user.email);
        boolean surnameOk = surname != null && surname.equals(user.surname);
        boolean firstNameOk = firstname != null && firstname.equals(user.firstName);
        return loginOk && emailOk && surnameOk && firstNameOk;
    }

    /** POJO containing various information on a user. */
    @Getter
    public static class UserData {
        private final String login;
        private final String email;
        private final String firstName;
        private final String surname;
        private final boolean admin;

      public UserData(String login, String email, String firstName, String surname, boolean admin) {
        this.login = login;
        this.email = email;
        this.firstName = firstName;
        this.surname = surname;
        this.admin = admin;
      }
    }

    /**
     * Returns true if the user is Guest (identified by login='Invité(e)').
     * 
     * @return true if the user is a guest
     */
    public boolean isGuest() {
        return ActiveDirectory.GUEST_LOGIN.equals(login);
    }
}
