<?php

declare(strict_types=1);

namespace App\Tests\Mcp\Tool\Machine;

use App\Tests\AbstractApiTestCase;

/**
 * @internal
 */
class MachineLinksToolTest extends AbstractApiTestCase
{
    public function testListMachineLinks(): void
    {
        $machine   = $this->createMachine(name: 'Machine Links Test');
        $composant = $this->createComposant(name: 'Comp A');
        $piece     = $this->createPiece(name: 'Piece A');

        $compLink  = $this->createMachineComponentLink($machine, $composant);
        $pieceLink = $this->createMachinePieceLink($machine, $piece, parentLink: $compLink, quantity: 3);

        $session = $this->createMcpClient('ROLE_VIEWER');
        $data    = $this->callMcpTool($session, 'list_machine_links', ['machineId' => $machine->getId()]);

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

        $this->assertSame($machine->getId(), $parsed['machineId']);
        $this->assertCount(1, $parsed['componentLinks']);
        $this->assertCount(1, $parsed['pieceLinks']);
        $this->assertSame($compLink->getId(), $parsed['componentLinks'][0]['id']);
        $this->assertSame($pieceLink->getId(), $parsed['pieceLinks'][0]['id']);
        $this->assertSame(3, $parsed['pieceLinks'][0]['quantity']);
    }

    public function testAddMachineComponentLink(): void
    {
        $machine   = $this->createMachine(name: 'Machine Add Test');
        $composant = $this->createComposant(name: 'Comp B');

        $session = $this->createMcpClient('ROLE_GESTIONNAIRE');
        $data    = $this->callMcpTool($session, 'add_machine_links', [
            'machineId' => $machine->getId(),
            'links'     => [
                [
                    'type'         => 'composant',
                    'entityId'     => $composant->getId(),
                    'nameOverride' => 'Custom Name',
                ],
            ],
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $created = $data['_parsed']['created'];
        $this->assertCount(1, $created);
        $this->assertSame('composant', $created[0]['type']);
        $this->assertSame($composant->getId(), $created[0]['entityId']);
        $this->assertNotEmpty($created[0]['id']);
    }

    public function testRemoveMachineLink(): void
    {
        $machine   = $this->createMachine(name: 'Machine Remove Test');
        $composant = $this->createComposant(name: 'Comp C');
        $link      = $this->createMachineComponentLink($machine, $composant);

        $session = $this->createMcpClient('ROLE_GESTIONNAIRE');
        $data    = $this->callMcpTool($session, 'remove_machine_link', [
            'linkId'   => $link->getId(),
            'linkType' => 'composant',
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertTrue($data['_parsed']['deleted']);

        // Verify the link is gone by listing
        $listData = $this->callMcpTool($session, 'list_machine_links', ['machineId' => $machine->getId()]);
        $this->assertCount(0, $listData['_parsed']['componentLinks']);
    }
}
