package com.spring.domain.user.service;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spring.domain.user.dto.SignUpRequest;
import com.spring.domain.user.error.UserIdConflictException;
import com.spring.domain.user.repository.AgentUserRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class SignUpService {
    
    private final AgentUserRepository agentUserRepository;
    private final PasswordEncoder passwordEncoder;

    @Transactional(readOnly = true)
    public boolean isConflictUserId(String userId) {
        boolean result = agentUserRepository.existsByUserId(userId);
        if (result) throw new UserIdConflictException();
        return result;
    }

    @Transactional
    public void signUp(SignUpRequest request) {
        request.encodePassword(passwordEncoder.encode(request.getUserPassword()));
        agentUserRepository.save(request.toEntity());
    }

}
