<?php

namespace App\Mcp\Tools\Tool;

use App\Domain\Agent\Models\Agent;
use App\Domain\Tool\Enums\ToolStatus;
use App\Domain\Tool\Models\Tool as ToolModel;
use App\Domain\Tool\Models\ToolFederationGroup;
use App\Mcp\Attributes\AssistantTool;
use App\Mcp\Concerns\HasStructuredErrors;
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 ToolFederationStatusTool extends Tool
{
    use HasStructuredErrors;

    protected string $name = 'tool_federation_status';

    protected string $description = 'Show the tool federation configuration for a specific agent — whether federation is enabled, the active group, and the pool size.';

    public function schema(JsonSchema $schema): array
    {
        return [
            'agent_id' => $schema->string()
                ->description('Agent ID to inspect'),
        ];
    }

    public function handle(Request $request): Response
    {
        $teamId = (app()->bound('mcp.team_id') ? app('mcp.team_id') : null) ?? auth()->user()?->current_team_id;

        if (! $teamId) {
            return $this->permissionDeniedError('No current team.');
        }

        $agent = Agent::withoutGlobalScopes()
            ->where('team_id', $teamId)
            ->find($request->get('agent_id'));

        if (! $agent) {
            return $this->notFoundError('agent');
        }

        $enabled = (bool) ($agent->config['use_tool_federation'] ?? false);
        $groupId = $agent->config['tool_federation_group_id'] ?? null;

        $group = $groupId
            ? ToolFederationGroup::withoutGlobalScopes()->where('team_id', $teamId)->find($groupId)
            : null;

        $poolCount = ToolModel::withoutGlobalScopes()
            ->where('team_id', $teamId)
            ->where('status', ToolStatus::Active)
            ->when($group && ! empty($group->tool_ids), fn ($q) => $q->whereIn('id', $group->tool_ids))
            ->count();

        return Response::text(json_encode([
            'agent_id' => $agent->id,
            'agent_name' => $agent->name,
            'federation_enabled' => $enabled,
            'federation_group' => $group ? ['id' => $group->id, 'name' => $group->name] : null,
            'pool_size' => $enabled ? $poolCount : null,
            'explicit_tool_count' => $agent->tools()->count(),
        ]));
    }
}
