package com.kospo.talk.model;

import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

@Getter
@Setter
@Entity
@Table(name = "users")
public class User implements UserDetails {
    @Id
    @Size(max = 10)
    @Column(name = "sabun", nullable = false, length = 10)
    private String sabun;

    @Size(max = 200)
    @Column(name = "name", length = 200)
    private String name;

    @Size(max = 20)
    @Column(name = "dept_cd", length = 20)
    private String deptCd;

    @Size(max = 20)
    @Column(name = "grade_cd", length = 20)
    private String gradeCd;

    @Size(max = 200)
    @Column(name = "grade_nm", length = 200)
    private String gradeNm;

    @Size(max = 100)
    @Column(name = "ptsn_nm", length = 100)
    private String ptsnNm;

    @Size(max = 20)
    @Column(name = "ofce_phn", length = 20)
    private String ofcePhn;


    @Size(max = 20)
    @Column(name = "mbl_phn", length = 20)
    private String mblPhn;

    @Size(max = 10)
    @Column(name = "state", length = 10)
    private String state;

    @Column(name = "online_yn")
    private boolean onlineYn = false;

    @Column(name = "stress")
    private String stress;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "sabun")
    List<Role> roles;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_cd", updatable = false, insertable = false)
    Dept dept;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles.stream().map(r -> new SimpleGrantedAuthority(r.getId().getRoleId())).toList();
    }

    @Override
    public String getPassword() {
        return "";
    }

    @Override
    public String getUsername() {
        return sabun;
    }

    @Override
    public boolean isAccountNonExpired() {return true;
    }

    @Override
    public boolean isAccountNonLocked() {return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {return true;
    }

    @Override
    public boolean isEnabled() {return true;
    }
}