<?php

namespace App\Mcp\Tools\System;

use App\Domain\Agent\Models\Agent;
use App\Domain\Bridge\Models\BridgeConnection;
use App\Domain\Experiment\Models\Experiment;
use App\Domain\Project\Models\Project;
use App\Mcp\Attributes\AssistantTool;
use App\Mcp\Concerns\HasMcpAppUi;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\Annotations\IsIdempotent;
use Laravel\Mcp\Server\Tools\Annotations\IsReadOnly;

#[IsReadOnly]
#[IsIdempotent]
#[AssistantTool('read')]
class DashboardKpisTool extends Tool
{
    use HasMcpAppUi;

    protected string $name = 'system_dashboard_kpis';

    protected function uiResourceUri(): string
    {
        return 'ui://fleetq/dashboard';
    }

    protected string $description = 'Get dashboard KPIs: experiment counts (total, running, completed, failed, paused), project counts (total, active), agent counts (total, active), bridge connection status.';

    public function schema(JsonSchema $schema): array
    {
        return [];
    }

    public function handle(Request $request): Response
    {
        $bridge = BridgeConnection::query()->active()->orderByDesc('connected_at')->first();

        return Response::text(json_encode([
            'experiments' => [
                'total' => Experiment::query()->count(),
                'running' => Experiment::query()->where('status', 'executing')->count(),
                'completed' => Experiment::query()->where('status', 'completed')->count(),
                'failed' => Experiment::query()->whereIn('status', [
                    'scoring_failed',
                    'planning_failed',
                    'building_failed',
                    'execution_failed',
                ])->count(),
                'paused' => Experiment::query()->where('status', 'paused')->count(),
            ],
            'projects' => [
                'total' => Project::query()->count(),
                'active' => Project::query()->where('status', 'active')->count(),
            ],
            'agents' => [
                'total' => Agent::query()->count(),
                'active' => Agent::query()->where('status', 'active')->count(),
            ],
            'bridge' => [
                'connected' => $bridge !== null,
                'llm_count' => $bridge?->onlineLlmCount() ?? 0,
                'agent_count' => $bridge?->foundAgentCount() ?? 0,
                'last_seen_at' => $bridge?->last_seen_at?->toISOString(),
            ],
        ]));
    }
}
