package com.kospo.talk.service.impl;

import com.kospo.talk.model.User;
import com.kospo.talk.payload.StressDto;
import com.kospo.talk.repository.UserRepository;
import com.kospo.talk.service.StressService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class StressServiceImpl implements StressService {
    @Value("${rabbitmq.user.exchange}")
    private String userExchange;
    @Value("${rabbitmq.user.route}")
    private String userRoute;
    private final RabbitTemplate userTemplate;
    private final UserRepository userRepository;


    @Transactional
    @Override
    public void stress(String sabun, String stress) {
        User user = userRepository.findById(sabun).get();
        user.setStress(stress);
        userRepository.save(user);
        userTemplate.convertAndSend(userExchange, userRoute + user.getSabun(), StressDto.builder()
                .type("STRESS")
                .stress(stress)
                .build());
    }
}
