<?php

declare(strict_types=1);

namespace App\Tests\Mcp\Tool\ModelType;

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

/**
 * @internal
 */
class ModelTypeToolsTest extends AbstractApiTestCase
{
    public function testListModelTypes(): void
    {
        $this->createModelType(name: 'MT Alpha', code: 'MTA-'.bin2hex(random_bytes(3)), category: ModelCategory::COMPONENT);
        $this->createModelType(name: 'MT Beta', code: 'MTB-'.bin2hex(random_bytes(3)), category: ModelCategory::PIECE);
        $session = $this->createMcpClient('ROLE_VIEWER');

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

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

    public function testGetModelType(): void
    {
        $mt      = $this->createModelType(name: 'MT Detail', code: 'MTD-'.bin2hex(random_bytes(3)));
        $session = $this->createMcpClient('ROLE_VIEWER');

        $data = $this->callMcpTool($session, 'get_model_type', ['modelTypeId' => $mt->getId()]);

        $this->assertArrayHasKey('_parsed', $data);
        $this->assertSame('MT Detail', $data['_parsed']['name']);
        $this->assertSame('COMPONENT', $data['_parsed']['category']);
        $this->assertIsArray($data['_parsed']['skeletonPieceRequirements']);
    }

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

        $data = $this->callMcpTool($session, 'create_model_type', [
            'name'     => 'MT Nouveau',
            'category' => 'composant',
            'code'     => 'MTN-'.bin2hex(random_bytes(3)),
        ]);

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

    public function testDeleteModelType(): void
    {
        $mt      = $this->createModelType(name: 'MT To Delete', code: 'MTDEL-'.bin2hex(random_bytes(3)));
        $session = $this->createMcpClient('ROLE_GESTIONNAIRE');

        $data = $this->callMcpTool($session, 'delete_model_type', ['modelTypeId' => $mt->getId()]);

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