package com.sikey.wa04.business.infrastructure.adapter.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.sikey.wa04.business.domain.adapter.UserAdapter;
import com.sikey.wa04.business.domain.entity.PhoneNumber;
import com.sikey.wa04.business.domain.entity.User;
import com.sikey.wa04.business.domain.entity.UserId;
import com.sikey.wa04.business.infrastructure.dto.model.UserModel;
import com.sikey.wa04.business.infrastructure.repository.UserRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Slf4j
@Component
public class UserAdapterImpl implements UserAdapter {

    @Resource
    private UserRepository userRepository;

    @Override
    public User findByPhoneNumber(PhoneNumber phoneNumber) {
        UserModel userModel = userRepository.findByPhoneNumberAndAreaCode(phoneNumber.number(), phoneNumber.areaCode());
        if (userModel == null) {
            return null;
        }
        return new User()
                .setId(new UserId(userModel.getId()))
                .setPhoneNumber(new PhoneNumber(userModel.getPhoneNumber(), userModel.getAreaCode()))
                .setPassword(userModel.getPassword())
                .setIsRegistered(userModel.getIsRegistered())
                .setEmail(userModel.getEmail())
                .setCreateAt(userModel.getCreatedAt())
                .setUpdateAt(userModel.getUpdatedAt());
    }

    @Override
    public User findById(UserId userId) {
        Optional<UserModel> optional = userRepository.findById(userId.getId());
        if (optional.isPresent()) {
            UserModel userModel = optional.get();
            return new User()
                    .setId(new UserId(userModel.getId()))
                    .setPhoneNumber(new PhoneNumber(userModel.getPhoneNumber(), userModel.getAreaCode()))
                    .setPassword(userModel.getPassword())
                    .setIsRegistered(userModel.getIsRegistered())
                    .setEmail(userModel.getEmail())
                    .setCreateAt(userModel.getCreatedAt())
                    .setUpdateAt(userModel.getUpdatedAt());
        }
        return null;
    }

    @Override
    public List<User> findByUserIdIn(List<String> userIds) {
        List<UserModel> userModels = userRepository.findByIdIn(userIds);
        if (CollectionUtil.isNotEmpty(userModels)) {
            List<User> users = new ArrayList<>(userModels.size());
            userModels.forEach(userModel -> {
                User user = new User();
                user.setId(new UserId(userModel.getId()));
                user.setPhoneNumber(new PhoneNumber(userModel.getPhoneNumber(), userModel.getAreaCode()));
                user.setPassword(userModel.getPassword());
                user.setIsRegistered(userModel.getIsRegistered());
                user.setEmail(userModel.getEmail());
                users.add(user);
            });

            return users;
        }

        return Collections.emptyList();
    }

    @Override
    public void updatePhoneNumberById(String phoneNumber,String id) {
        userRepository.updatePhoneNumberById(phoneNumber,id);
    }
}
