package com.luojia_channel.modules.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luojia_channel.common.exception.UserException;
import com.luojia_channel.common.utils.UserContext;
import com.luojia_channel.modules.file.service.impl.FileServiceImpl;
import com.luojia_channel.modules.user.dto.UserChangeInfoDTO;
import com.luojia_channel.modules.user.vo.UserInfoVO;
import com.luojia_channel.modules.user.entity.User;
import com.luojia_channel.modules.user.mapper.UserMapper;
import com.luojia_channel.modules.user.service.UserInfoService;
import com.luojia_channel.modules.user.utils.ValidateUserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl extends ServiceImpl<UserMapper, User> implements UserInfoService {

    private final UserMapper userMapper;
    private final ValidateUserUtil validateUserUtil;
    private final FileServiceImpl fileService;

    @Override
    public void updateInfo(UserChangeInfoDTO userChangeInfoDTO) {
        Long userId = UserContext.getUserId();
        User currentUser = userMapper.selectById(userId);
        if(currentUser == null){
            throw new UserException("用户不存在");
        }
        validateUserUtil.validateFormats(userChangeInfoDTO, userId);
        User user = BeanUtil.copyProperties(userChangeInfoDTO, User.class);
        user.setId(userId);
        user.setUpdateTime(LocalDateTime.now());
        updateById(user);
    }

    @Override
    public void updatePassword(String password) {
        Long userId = UserContext.getUserId();
        User user = userMapper.selectById(userId);
        if(user == null){
            throw new UserException("用户不存在");
        }
        if (!password.matches(ValidateUserUtil.PASSWORD_REGEX)) {
            throw new UserException("密码格式不符合要求");
        }
        if (BCrypt.checkpw(password, user.getPassword())) {
            throw new UserException("修改密码不能与原密码相同");
        }
        String encodedPassword = BCrypt.hashpw(password, BCrypt.gensalt());
        user.setPassword(encodedPassword);
        user.setUpdateTime(LocalDateTime.now());
        updateById(user);
    }

    @Override
    public String uploadAvatar(MultipartFile file) {
        return fileService.uploadFile(file);
    }
    
    @Override
    public UserInfoVO getUserInfo(Long userId) {
        // 如果userId为null，则获取当前登录用户的ID
        if (userId == null) {
            userId = UserContext.getUserId();
            if (userId == null) {
                throw new UserException("用户未登录");
            }
        }
        
        // 从数据库获取用户信息
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new UserException("用户不存在");
        }
        
        // 转换为DTO对象
        UserInfoVO userInfoVO = UserInfoVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .phone(user.getPhone())
                .email(user.getEmail())
                .avatar(user.getAvatar())
                .gender(user.getGender())
                .college(user.getCollege())
                .role(user.getRole())
                .status(user.getStatus())
                .build();
        
        return userInfoVO;
    }
}
