package de.assecutor.votianlt.mcp.tools;

import de.assecutor.votianlt.mcp.dto.TaskCompletionResult;
import de.assecutor.votianlt.service.JobStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * MCP Tool for task completion statistics and data.
 */
@Component
@Slf4j
public class TaskCompletionTool {

    private final JobStatisticsService statisticsService;

    public TaskCompletionTool(JobStatisticsService statisticsService) {
        this.statisticsService = statisticsService;
    }

    @Tool(description = "Get overall task completion statistics including total, completed, pending tasks and completion rate")
    public TaskCompletionResult getTaskCompletionStats() {
        log.info("MCP Tool: Getting task completion statistics");

        Map<String, Long> stats = statisticsService.getTaskCompletionStats();
        long total = stats.getOrDefault("total", 0L);
        long completed = stats.getOrDefault("completed", 0L);
        long pending = stats.getOrDefault("pending", 0L);

        double completionRate = total > 0 ? (double) completed / total * 100.0 : 0.0;

        return TaskCompletionResult.builder().totalTasks(total).completedTasks(completed).pendingTasks(pending)
                .completionRate(completionRate).build();
    }

    @Tool(description = "Get a summary of task completion as a formatted string")
    public String getTaskCompletionSummary() {
        log.info("MCP Tool: Getting task completion summary");

        Map<String, Long> stats = statisticsService.getTaskCompletionStats();
        long total = stats.getOrDefault("total", 0L);
        long completed = stats.getOrDefault("completed", 0L);
        long pending = stats.getOrDefault("pending", 0L);

        double completionRate = total > 0 ? (double) completed / total * 100.0 : 0.0;

        return String.format("Task Statistics: %d total tasks, %d completed (%.1f%%), %d pending", total, completed,
                completionRate, pending);
    }
}
