<?php

declare(strict_types=1);

namespace App\Tests\Mcp\Tool\Piece;

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

/**
 * @internal
 */
class PiecesCrudToolTest extends AbstractApiTestCase
{
    public function testListPieces(): void
    {
        $this->createPiece(name: 'Piece Alpha');
        $this->createPiece(name: 'Piece Beta', reference: 'REF-BETA');
        $session = $this->createMcpClient('ROLE_VIEWER');

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

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

    public function testGetPiece(): void
    {
        $constructeur = $this->createConstructeur(name: 'Fournisseur Piece');
        $modelType    = $this->createModelType(name: 'Type Piece', code: 'TP-001', category: ModelCategory::PIECE);
        $piece        = $this->createPiece(name: 'Piece Gamma', reference: 'REF-001', type: $modelType);

        $this->createPieceConstructeurLink($piece, $constructeur);

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

        $data = $this->callMcpTool($session, 'get_piece', ['pieceId' => $piece->getId()]);

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertSame('Piece Gamma', $data['_parsed']['name']);
        $this->assertSame('REF-001', $data['_parsed']['reference']);
        $this->assertNotNull($data['_parsed']['typePiece']);
        $this->assertSame('Type Piece', $data['_parsed']['typePiece']['name']);
        $this->assertCount(1, $data['_parsed']['constructeurs']);
        $this->assertSame('Fournisseur Piece', $data['_parsed']['constructeurs'][0]['name']);
    }

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

        $data = $this->callMcpTool($session, 'create_piece', [
            'name'      => 'Piece Nouveau',
            'reference' => 'REF-NEW',
            'prix'      => '42.99',
        ]);

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

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

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

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

    public function testUpdatePiece(): void
    {
        $piece   = $this->createPiece(name: 'Old Piece');
        $session = $this->createMcpClient('ROLE_GESTIONNAIRE');

        $data = $this->callMcpTool($session, 'update_piece', [
            'pieceId' => $piece->getId(),
            'name'    => 'Updated Piece',
            'prix'    => '99.00',
        ]);

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

    public function testDeletePiece(): void
    {
        $piece   = $this->createPiece(name: 'To Delete');
        $session = $this->createMcpClient('ROLE_GESTIONNAIRE');

        $data = $this->callMcpTool($session, 'delete_piece', ['pieceId' => $piece->getId()]);

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