package de.avatic.lcc.config;

import de.avatic.lcc.model.db.users.Group;
import de.avatic.lcc.model.db.users.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;

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

public class LccOidcUser extends DefaultOidcUser {

    private final Integer userId;

    public LccOidcUser(Collection<? extends GrantedAuthority> authorities,
                       OidcIdToken idToken,
                       OidcUserInfo userInfo,
                       String nameAttributeKey,
                       Integer userId) {
        super(authorities, idToken, userInfo, nameAttributeKey);
        this.userId = userId;
    }

    public static User createDatabaseUser(String email, String firstName, String lastName, String workdayId, boolean isFirstUser) {
        User user = new User();

        var groups = new ArrayList<Group>();

        if(isFirstUser) {
            var g = new Group();
            g.setName("service");
            groups.add(g);

            g = new Group();
            g.setName("right-management");
            groups.add(g);
        } else {
            var g = new Group();
            g.setName("none");
            groups.add(g);
        }


        user.setEmail(email);
        user.setFirstName(firstName == null ? "" : firstName);
        user.setLastName(lastName == null ? "" : lastName);
        user.setGroups(groups);
        user.setWorkdayId(workdayId == null ? "" : workdayId);
        user.setActive(false);

        return user;
    }

    public Integer getSqlUserId() {
        return userId;
    }

}
