package com.social.media.interfaces.web.mapper;

import java.util.Set;

import org.springframework.stereotype.Component;

import com.social.media.application.bot.command.CreateBotCommand;
import com.social.media.application.bot.command.UpdateBotCommand;
import com.social.media.domain.bot.aggregate.Bot;
import com.social.media.domain.bot.valueobject.BotId;
import com.social.media.domain.bot.valueobject.BotType;
import com.social.media.domain.company.valueobject.CompanyId;
import com.social.media.interfaces.web.dto.bot.BotResponse;
import com.social.media.interfaces.web.dto.bot.CreateBotRequest;
import com.social.media.interfaces.web.dto.bot.UpdateBotRequest;

/**
 * Mapper for Bot DTOs
 */
@Component
public class BotDtoMapper {

    /**
     * Convert CreateBotRequest to CreateBotCommand
     */
    public CreateBotCommand toCreateCommand(CreateBotRequest request) {
        return new CreateBotCommand(
                new CompanyId(request.companyId()),
                null, // createdBy - will be set by controller based on authentication
                request.name(),
                request.description(),
                BotType.valueOf(request.botType().toUpperCase()),
                null, // configuration - would need proper mapping
                Set.of(String.valueOf(request.socialAccountId())), // single social account
                Set.of() // targetUserListIds - not provided in request
        );
    }

    /**
     * Convert UpdateBotRequest to UpdateBotCommand
     */
    public UpdateBotCommand toUpdateCommand(BotId botId, UpdateBotRequest request) {
        return new UpdateBotCommand(
                botId,
                request.name(),
                request.description(),
                null, // configuration - would need proper mapping
                Set.of(), // targetSocialAccountIds - not updated here
                Set.of() // targetUserListIds - not updated here
        );
    }

    /**
     * Convert Bot aggregate to BotResponse
     */
    public BotResponse toResponse(Bot bot) {
        Long primarySocialAccountId = null;
        if (!bot.getTargetSocialAccounts().isEmpty()) {
            primarySocialAccountId = bot.getTargetSocialAccounts().iterator().next().value();
        }

        return new BotResponse(
                bot.getId().value(),
                bot.getCompanyId().value(),
                bot.getName(),
                bot.getDescription(),
                bot.getBotType().name(),
                null, // configuration - sensitive data not exposed
                bot.isActive(),
                primarySocialAccountId,
                null, // socialAccountName - would need to be retrieved separately
                bot.getLastExecutionAt(),
                bot.getCreatedAt(),
                bot.getUpdatedAt()
        );
    }
}
