<?php

declare(strict_types=1);

namespace App\Tests\Mcp\Tool\Machine;

use App\Tests\AbstractApiTestCase;

/**
 * @internal
 */
class MachinesCrudToolTest extends AbstractApiTestCase
{
    public function testListMachines(): void
    {
        $site = $this->createSite(name: 'Site Usine');
        $this->createMachine(name: 'Machine Alpha', site: $site);
        $this->createMachine(name: 'Machine Beta', site: $site);
        $session = $this->createMcpClient('ROLE_VIEWER');

        $data = $this->callMcpTool($session, 'list_machines');

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertGreaterThanOrEqual(2, $data['_parsed']['total']);
    }

    public function testGetMachine(): void
    {
        $site         = $this->createSite(name: 'Site Principal');
        $constructeur = $this->createConstructeur(name: 'Fournisseur M');
        $machine      = $this->createMachine(name: 'Machine Gamma', site: $site, reference: 'REF-M001');

        $this->createMachineConstructeurLink($machine, $constructeur);

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

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

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertSame('Machine Gamma', $data['_parsed']['name']);
        $this->assertSame('REF-M001', $data['_parsed']['reference']);
        $this->assertNotNull($data['_parsed']['site']);
        $this->assertSame('Site Principal', $data['_parsed']['site']['name']);
        $this->assertCount(1, $data['_parsed']['constructeurs']);
        $this->assertSame('Fournisseur M', $data['_parsed']['constructeurs'][0]['name']);
    }

    public function testCreateMachine(): void
    {
        $site    = $this->createSite(name: 'Site Création');
        $session = $this->createMcpClient('ROLE_GESTIONNAIRE');

        $data = $this->callMcpTool($session, 'create_machine', [
            'name'      => 'Machine Nouvelle',
            'siteId'    => $site->getId(),
            'reference' => 'REF-NEW',
            'prix'      => '42.99',
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertSame('Machine Nouvelle', $data['_parsed']['name']);
        $this->assertNotEmpty($data['_parsed']['id']);
    }

    public function testCreateMachineRequiresGestionnaire(): void
    {
        $site    = $this->createSite(name: 'Site Forbidden');
        $session = $this->createMcpClient('ROLE_VIEWER');

        $data = $this->callMcpTool($session, 'create_machine', [
            'name'   => 'Forbidden',
            'siteId' => $site->getId(),
        ]);

        $this->assertArrayHasKey('error', $data, 'Should fail with VIEWER role');
    }

    public function testUpdateMachine(): void
    {
        $site    = $this->createSite(name: 'Site Update');
        $machine = $this->createMachine(name: 'Old Machine', site: $site);
        $session = $this->createMcpClient('ROLE_GESTIONNAIRE');

        $data = $this->callMcpTool($session, 'update_machine', [
            'machineId' => $machine->getId(),
            'name'      => 'Updated Machine',
            'prix'      => '99.00',
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertSame('Updated Machine', $data['_parsed']['name']);
    }

    public function testDeleteMachine(): void
    {
        $site    = $this->createSite(name: 'Site Delete');
        $machine = $this->createMachine(name: 'To Delete', site: $site);
        $session = $this->createMcpClient('ROLE_GESTIONNAIRE');

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

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