<?php

namespace App\Mcp\Tools\GitRepository;

use App\Domain\GitRepository\Actions\TestGitConnectionAction;
use App\Domain\GitRepository\Models\GitRepository;
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]
class GitRepositoryTestTool extends Tool
{
    use HasStructuredErrors;

    protected string $name = 'git_repository_test';

    protected string $description = 'Test connectivity to a git repository. Updates the last_ping_at and status fields.';

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

    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.');
        }
        $repo = GitRepository::withoutGlobalScopes()->where('team_id', $teamId)->find($request->get('id'));

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

        $result = app(TestGitConnectionAction::class)->execute($repo);

        return Response::text(json_encode($result));
    }
}
