package dev.sheldan.abstracto.moderation.command;

import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.interaction.slash.SlashCommandConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.moderation.config.ModerationModuleDefinition;
import dev.sheldan.abstracto.moderation.config.ModerationSlashCommandNames;
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureDefinition;
import dev.sheldan.abstracto.moderation.model.template.command.MyWarningsModel;
import dev.sheldan.abstracto.moderation.service.management.WarnManagementService;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
public class MyWarnings extends AbstractConditionableCommand {

    public static final String MY_WARNINGS_RESPONSE_EMBED_TEMPLATE = "myWarnings_response";
    private static final String MY_WARNINGS_COMMAND = "myWarnings";
    @Autowired
    private ChannelService channelService;

    @Autowired
    private WarnManagementService warnManagementService;

    @Autowired
    private UserInServerManagementService userInServerManagementService;

    @Autowired
    private InteractionService interactionService;

    @Override
    public CommandResult execute(CommandContext commandContext) {
        AUserInAServer userInAServer = userInServerManagementService.loadOrCreateUser(commandContext.getAuthor());
        Long currentWarnCount = warnManagementService.getActiveWarnCountForUser(userInAServer);
        Long totalWarnCount = warnManagementService.getTotalWarnsForUser(userInAServer);
        MyWarningsModel model = MyWarningsModel
                .builder()
                .member(commandContext.getAuthor())
                .totalWarnCount(totalWarnCount)
                .currentWarnCount(currentWarnCount)
                .build();
        channelService.sendEmbedTemplateInTextChannelList(MY_WARNINGS_RESPONSE_EMBED_TEMPLATE, model, commandContext.getChannel());
        return CommandResult.fromIgnored();
    }

    @Override
    public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
        AUserInAServer userInAServer = userInServerManagementService.loadOrCreateUser(event.getMember());
        Long currentWarnCount = warnManagementService.getActiveWarnCountForUser(userInAServer);
        Long totalWarnCount = warnManagementService.getTotalWarnsForUser(userInAServer);
        MyWarningsModel model = MyWarningsModel
                .builder()
                .member(event.getMember())
                .totalWarnCount(totalWarnCount)
                .currentWarnCount(currentWarnCount)
                .build();
        return interactionService.replyEmbed(MY_WARNINGS_RESPONSE_EMBED_TEMPLATE, model, event)
                .thenApply(interactionHook -> CommandResult.fromSuccess());
    }

    @Override
    public CommandConfiguration getConfiguration() {
        List<Parameter> parameters = new ArrayList<>();
        HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
        List<String> aliases = Arrays.asList("myWarns");

        SlashCommandConfig slashCommandConfig = SlashCommandConfig
                .builder()
                .enabled(true)
                .rootCommandName(ModerationSlashCommandNames.WARNINGS_PUBLIC)
                .commandName(MY_WARNINGS_COMMAND)
                .build();

        return CommandConfiguration.builder()
                .name(MY_WARNINGS_COMMAND)
                .module(ModerationModuleDefinition.MODERATION)
                .templated(true)
                .slashCommandConfig(slashCommandConfig)
                .supportsEmbedException(true)
                .causesReaction(true)
                .aliases(aliases)
                .parameters(parameters)
                .help(helpInfo)
                .build();
    }

    @Override
    public FeatureDefinition getFeature() {
        return ModerationFeatureDefinition.WARNING;
    }
}
