package com.sn.sowsysrestapi.domain.security.dao;

import com.sn.sowsysrestapi.domain.repository.UserRepo;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

@Repository
@AllArgsConstructor
public class UserDao {

    private UserRepo userRepo;

    public UserDetails findUserByUsername(String username) throws UsernameNotFoundException {
        com.sn.sowsysrestapi.domain.entity.User user = userRepo.findByUsernameIgnoreCase(username)
                .orElseThrow(() -> new UsernameNotFoundException("User not found"));

        String[] roles = user.isAdmin() ? new String[]{"ADMIN", "USER"} : new String[]{"USER"};

        return org.springframework.security.core.userdetails.User.builder().username(user.getUsername())
                .password(user.getPassword()).roles(roles).build();
    }

//    private final static List<UserDetails> APPLICATION_USERS = Arrays.asList(
//            new User(
//                    "john.doe",
//                    "123",
//                    Collections.singleton(new SimpleGrantedAuthority("ROLE_ADMIN"))
//            ),
//            new User(
//                    "mary.jane",
//                    "123",
//                    Collections.singleton(new SimpleGrantedAuthority("ROLE_USER"))
//            )
//    );
//
//    public UserDetails findUserByUsername(String username) {
//        return APPLICATION_USERS
//                .stream()
//                .filter(userDetails -> userDetails.getUsername().equals(username))
//                .findFirst()
//                .orElseThrow(() -> new UsernameNotFoundException("No user was faound with the given username"));
//    }


}
