package com.imprimelibros.erp.users;

import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;

import java.util.Set;

import org.hibernate.annotations.Formula;

@Entity
@Table(name = "users", uniqueConstraints = {
    @UniqueConstraint(name = "uk_users_username", columnNames = "username")
})
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "fullname")
    @NotBlank(message = "{validation.required}")
    private String fullName;

    @Column(name = "username", nullable = false, length = 190)
    @Email(message = "{validation.email}")
    @NotBlank(message = "{validation.required}")
    private String userName;

    @Column(name = "password")
    @NotBlank(message = "{validation.required}")
    private String password;

    @Column(name = "enabled")
    private boolean enabled;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles = new java.util.HashSet<>(); 

    // SUPERADMIN=3, ADMIN=2, USER=1 (ajusta a tus nombres reales)
    @Formula("""
        (
          select coalesce(max(
            case r.name
              when 'SUPERADMIN' then 3
              when 'ADMIN'      then 2
              else 1
            end
          ), 0)
          from users_roles ur
          join roles r on r.id = ur.role_id
          where ur.user_id = id
        )
        """)
    private Integer roleRank;

    @Formula("""
        (select group_concat(lower(r.name) order by r.name separator ', ')
            from users_roles ur join roles r on r.id = ur.role_id
            where ur.user_id = id)
            """)
    private String rolesConcat;

    /* Constructors */
    public User() {
    }

    public User(String fullName, String userName, String password, boolean enabled) {
        this.fullName = fullName;
        this.userName = userName;
        this.password = password;
        this.enabled = enabled;
    }

    public User(String fullName, String userName, String password, boolean enabled,
            Set<Role> roles) {
        this.fullName = fullName;
        this.userName = userName;
        this.password = password;
        this.enabled = enabled;
        this.roles = roles;
    }

    /* Getters and Setters */

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public Integer getRoleRank() {
        return roleRank;
    }

    public String getRolesConcat() {
        return rolesConcat;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", fullName='" + fullName + '\'' +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", enabled=" + enabled +
                ", roles=" + roles +
                '}';
    }

}
