<?php

declare(strict_types=1);

namespace App\Tests\Mcp\Tool;

use App\Tests\AbstractApiTestCase;

/**
 * @internal
 */
class SearchInventoryToolTest extends AbstractApiTestCase
{
    public function testSearchFindsAcrossEntities(): void
    {
        $this->createMachine(name: 'Alpha Machine');
        $this->createPiece(name: 'Alpha Piece');
        $this->createSite(name: 'Alpha Site');

        $session = $this->createMcpClient('ROLE_VIEWER');

        $data = $this->callMcpTool($session, 'search_inventory', ['query' => 'Alpha']);

        $this->assertArrayHasKey('_parsed', $data);
        $results = $data['_parsed'];
        $this->assertIsArray($results);

        $types = array_unique(array_column($results, 'type'));
        $this->assertContains('machine', $types);
        $this->assertContains('piece', $types);
        $this->assertContains('site', $types);

        foreach ($results as $result) {
            $this->assertArrayHasKey('type', $result);
            $this->assertArrayHasKey('id', $result);
            $this->assertArrayHasKey('name', $result);
            $this->assertArrayHasKey('reference', $result);
            $this->assertStringContainsStringIgnoringCase('Alpha', $result['name']);
        }
    }

    public function testSearchFiltersByType(): void
    {
        $this->createMachine(name: 'Beta Machine');
        $this->createPiece(name: 'Beta Piece');
        $this->createSite(name: 'Beta Site');

        $session = $this->createMcpClient('ROLE_VIEWER');

        $data = $this->callMcpTool($session, 'search_inventory', [
            'query' => 'Beta',
            'types' => 'machine',
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $results = $data['_parsed'];
        $this->assertIsArray($results);
        $this->assertNotEmpty($results);

        $types = array_unique(array_column($results, 'type'));
        $this->assertSame(['machine'], $types);
    }
}
