<?php

namespace App\Mcp\Tools\Memory;

use App\Domain\Memory\Models\Memory;
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 MemoryGetTool extends Tool
{
    use HasStructuredErrors;

    protected string $name = 'memory_get';

    protected string $description = 'Get a specific memory entry by ID.';

    public function schema(JsonSchema $schema): array
    {
        return [
            'memory_id' => $schema->string()->description('The memory entry ID.')->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.');
        }

        $memory = Memory::withoutGlobalScopes()->where('team_id', $teamId)->find($request->get('memory_id'));
        if (! $memory) {
            return $this->notFoundError('memory entry');
        }

        return Response::text(json_encode([
            'id' => $memory->id,
            'content' => $memory->content,
            'topic' => $memory->topic,
            'tags' => $memory->tags,
            'content_hash' => $memory->content_hash,
            'belief_status' => $memory->belief_status->value,
            'rejected_alternatives' => $memory->rejected_alternatives ?? [],
            'supersedes_id' => $memory->supersedes_id,
            'conflict_flag' => (bool) $memory->conflict_flag,
            'conflict_with_id' => $memory->conflict_with_id,
            'created_at' => $memory->created_at,
            'updated_at' => $memory->updated_at,
        ]));
    }
}
