package com.social.media.application.bot.handler;

import com.social.media.application.bot.command.StopBotCommand;
import com.social.media.application.bot.dto.BotResponseDto;
import com.social.media.domain.bot.aggregate.Bot;
import com.social.media.domain.bot.repository.BotRepository;
import com.social.media.domain.bot.valueobject.UserListId;
import com.social.media.domain.socialaccount.valueobject.SocialAccountId;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class StopBotCommandHandler {
    
    private final BotRepository botRepository;
    
    public StopBotCommandHandler(BotRepository botRepository) {
        this.botRepository = botRepository;
    }
    
    public BotResponseDto handle(StopBotCommand command) {
        // Find existing bot
        Bot bot = botRepository.findById(command.botId())
            .orElseThrow(() -> new RuntimeException(
                "Bot not found: " + command.botId()
            ));
        
        // Stop the bot execution
        bot.stop();
        
        // Save stopped bot
        Bot stoppedBot = botRepository.save(bot);
        
        // Return DTO
        return mapToDto(stoppedBot);
    }
    
    private BotResponseDto mapToDto(Bot bot) {
        return BotResponseDto.builder()
            .id(bot.getId().value().toString())
            .companyId(bot.getCompanyId().value().toString())
            .createdBy(bot.getCreatedBy().value().toString())
            .name(bot.getName())
            .description(bot.getDescription())
            .botType(bot.getBotType().name())
            .status(bot.getStatus().name())
            .configuration(bot.getConfiguration() != null ? Map.of("hasConfig", true) : Collections.emptyMap())
            .targetSocialAccountIds(bot.getTargetSocialAccounts().stream()
                .map(id -> id.value().toString())
                .collect(Collectors.toSet()))
            .targetUserListIds(bot.getTargetUserLists().stream()
                .map(id -> id.value().toString())
                .collect(Collectors.toSet()))
            .lastExecutionAt(bot.getLastExecutionAt())
            .nextExecutionAt(bot.getNextExecutionAt())
            .totalExecutions(bot.getTotalExecutions())
            .successfulExecutions(bot.getSuccessfulExecutions())
            .failedExecutions(bot.getFailedExecutions())
            .successRate(bot.getSuccessRate())
            .failureRate(bot.getFailureRate())
            .createdAt(bot.getCreatedAt())
            .updatedAt(bot.getUpdatedAt())
            .build();
    }
}

