<?php

namespace App\Mcp\Tools\Experiment;

use App\Domain\Experiment\Actions\RetryFromStepAction;
use App\Domain\Experiment\Models\Experiment;
use App\Domain\Experiment\Models\PlaybookStep;
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 ExperimentRetryFromStepTool extends Tool
{
    use HasStructuredErrors;

    protected string $name = 'experiment_retry_from_step';

    protected string $description = 'Retry a workflow experiment from a specific step. Resets the target step and all downstream steps, then re-dispatches execution from that point.';

    public function schema(JsonSchema $schema): array
    {
        return [
            'experiment_id' => $schema->string()
                ->description('The experiment UUID')
                ->required(),
            'step_id' => $schema->string()
                ->description('The playbook step UUID to retry from')
                ->required(),
        ];
    }

    public function handle(Request $request): Response
    {
        $validated = $request->validate([
            'experiment_id' => 'required|string',
            'step_id' => 'required|string',
        ]);

        $teamId = (app()->bound('mcp.team_id') ? app('mcp.team_id') : null) ?? auth()->user()?->current_team_id;
        if (! $teamId) {
            return $this->permissionDeniedError('No current team.');
        }
        $experiment = Experiment::withoutGlobalScopes()->where('team_id', $teamId)->find($validated['experiment_id']);

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

        $step = PlaybookStep::where('experiment_id', $experiment->id)
            ->where('id', $validated['step_id'])
            ->first();

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

        try {
            app(RetryFromStepAction::class)->execute($experiment, $step);

            return Response::text(json_encode([
                'success' => true,
                'message' => 'Retry from step initiated.',
                'experiment_id' => $experiment->id,
                'step_id' => $step->id,
            ]));
        } catch (\Throwable $e) {
            throw $e;
        }
    }
}
