package com.petshop.backend.security;

import com.petshop.backend.entity.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.security.Principal;
import java.util.Collection;
import java.util.List;

public class AppPrincipal implements Principal {

    private final Long userId;
    private final String username;
    private final User.Role role;
    private final Integer tokenVersion;
    private final List<GrantedAuthority> authorities;

    public AppPrincipal(Long userId, String username, User.Role role, Integer tokenVersion) {
        this.userId = userId;
        this.username = username;
        this.role = role;
        this.tokenVersion = tokenVersion;
        this.authorities = List.of(new SimpleGrantedAuthority("ROLE_" + role.name()));
    }

    public Long getUserId() {
        return userId;
    }

    @Override
    public String getName() {
        return username;
    }

    public String getUsername() {
        return username;
    }

    public User.Role getRole() {
        return role;
    }

    public Integer getTokenVersion() {
        return tokenVersion;
    }

    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }
}
