<?php

namespace App\Mcp\Tools\Signal;

use App\Domain\Shared\Models\ContactIdentity;
use App\Domain\Signal\Jobs\EvaluateContactRiskJob;
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\IsDestructive;

#[IsDestructive]
#[AssistantTool('read')]
class ForceReevaluateContactRiskTool extends Tool
{
    use HasStructuredErrors;

    protected string $name = 'contact_risk_reevaluate';

    protected string $description = 'Force a fresh risk score evaluation for a contact identity by dispatching EvaluateContactRiskJob.';

    public function schema(JsonSchema $schema): array
    {
        return [
            'contact_id' => $schema->string()
                ->description('Contact identity UUID')
                ->required(),
        ];
    }

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

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

        $contact = ContactIdentity::withoutGlobalScopes()
            ->where('team_id', $teamId)
            ->find($request->get('contact_id'));

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

        EvaluateContactRiskJob::dispatch($contact->id);

        return Response::text(json_encode([
            'dispatched' => true,
            'contact_id' => $contact->id,
        ]));
    }
}
