package com.njcn.product.event.transientes.security;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.product.event.transientes.mapper.PqsUserMapper;
import com.njcn.product.event.transientes.mapper.PqsUserSetMapper;
import com.njcn.product.event.transientes.pojo.po.PqsUser;
import com.njcn.product.event.transientes.pojo.po.PqsUserSet;
import com.njcn.product.event.config.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class MyUserDetailsService implements UserDetailsService {

    private final PqsUserMapper pqsUserMapper;

    private final PqsUserSetMapper pqsUserSetMapper;

    private final RedisUtil redisUtil;



    @Override
    public MyUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {


        if("system_event".equals(username)){
            PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encodedPassword = passwordEncoder.encode("@#001njcnpqs");
            return new MyUserDetails("12345678910","system_event",encodedPassword,"10001",
                    new ArrayList<>());
        }

        if("cn_test_a".equals(username)){
            PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encodedPassword = passwordEncoder.encode("@#001njcnpqs");
            return new MyUserDetails("12345678910","system_event",encodedPassword,"10001",
                    new ArrayList<>());
        }


        if(redisUtil.hasKey("event_smart_"+username)){
        String password = redisUtil.getRawValue("event_smart_"+username);
        // 这里应该从数据库中获取用户信息，本示例使用硬编码用户
            PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encodedPassword = passwordEncoder.encode(password);

            LambdaQueryWrapper<PqsUser> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(PqsUser::getLoginname,username);
            PqsUser pqsUser = pqsUserMapper.selectOne(userWrapper);
            if(Objects.isNull(pqsUser)){
                throw new UsernameNotFoundException("User not found with username: " + username);
            }

            LambdaQueryWrapper<PqsUserSet> userSetWrapper = new LambdaQueryWrapper<>();
            userSetWrapper.eq(PqsUserSet::getUserIndex,pqsUser.getUserIndex());
            PqsUserSet pqsUserSet = pqsUserSetMapper.selectOne(userSetWrapper);
            String deptId = pqsUserSet.getDeptsIndex();


            return new MyUserDetails(pqsUser.getUserIndex(),pqsUser.getLoginname(), encodedPassword,deptId,
                    new ArrayList<>());
        }else {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }
    }
}
