<?php

declare(strict_types=1);

namespace App\Tests\Mcp\Tool\Composant;

use App\Enum\ModelCategory;
use App\Tests\AbstractApiTestCase;

/**
 * @internal
 */
class ComposantsCrudToolTest extends AbstractApiTestCase
{
    public function testListComposants(): void
    {
        $this->createComposant(name: 'Composant Alpha');
        $this->createComposant(name: 'Composant Beta');
        $session = $this->createMcpClient('ROLE_VIEWER');

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

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

    public function testGetComposant(): void
    {
        $constructeur = $this->createConstructeur(name: 'Fournisseur Comp');
        $modelType    = $this->createModelType(name: 'Type Composant', code: 'TC-001', category: ModelCategory::COMPONENT);
        $composant    = $this->createComposant(name: 'Composant Gamma', type: $modelType);

        $this->createComposantConstructeurLink($composant, $constructeur);

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

        $data = $this->callMcpTool($session, 'get_composant', ['composantId' => $composant->getId()]);

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertSame('Composant Gamma', $data['_parsed']['name']);
        $this->assertNotNull($data['_parsed']['typeComposant']);
        $this->assertSame('Type Composant', $data['_parsed']['typeComposant']['name']);
        $this->assertCount(1, $data['_parsed']['constructeurs']);
        $this->assertSame('Fournisseur Comp', $data['_parsed']['constructeurs'][0]['name']);
    }

    public function testCreateComposant(): void
    {
        $session = $this->createMcpClient('ROLE_GESTIONNAIRE');

        $data = $this->callMcpTool($session, 'create_composant', [
            'name'        => 'Composant Nouveau',
            'reference'   => 'REF-COMP',
            'description' => 'Un composant de test',
            'prix'        => '42.99',
        ]);

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

    public function testCreateComposantRequiresGestionnaire(): void
    {
        $session = $this->createMcpClient('ROLE_VIEWER');

        $data = $this->callMcpTool($session, 'create_composant', ['name' => 'Forbidden']);

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

    public function testUpdateComposant(): void
    {
        $composant = $this->createComposant(name: 'Old Composant');
        $session   = $this->createMcpClient('ROLE_GESTIONNAIRE');

        $data = $this->callMcpTool($session, 'update_composant', [
            'composantId' => $composant->getId(),
            'name'        => 'Updated Composant',
            'prix'        => '99.00',
        ]);

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

    public function testDeleteComposant(): void
    {
        $composant = $this->createComposant(name: 'To Delete');
        $session   = $this->createMcpClient('ROLE_GESTIONNAIRE');

        $data = $this->callMcpTool($session, 'delete_composant', ['composantId' => $composant->getId()]);

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