<?php

declare(strict_types=1);

namespace App\Tests\Mcp\Tool\CustomField;

use App\Tests\AbstractApiTestCase;

/**
 * @internal
 */
class CustomFieldToolsTest extends AbstractApiTestCase
{
    public function testListCustomFieldValues(): void
    {
        $machine     = $this->createMachine(name: 'Machine CF');
        $customField = $this->createCustomField(name: 'Serial Number', type: 'text', machine: $machine);
        $this->createCustomFieldValue(customField: $customField, value: 'SN-12345', machine: $machine);

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

        $data = $this->callMcpTool($session, 'list_custom_field_values', [
            'entityType' => 'machine',
            'entityId'   => $machine->getId(),
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $parsed = $data['_parsed'];
        $this->assertSame('machine', $parsed['entityType']);
        $this->assertSame($machine->getId(), $parsed['entityId']);
        $this->assertSame(1, $parsed['total']);
        $this->assertSame('SN-12345', $parsed['values'][0]['value']);
        $this->assertSame('Serial Number', $parsed['values'][0]['customFieldName']);
        $this->assertSame('text', $parsed['values'][0]['customFieldType']);
    }

    public function testUpsertCustomFieldValues(): void
    {
        $machine     = $this->createMachine(name: 'Machine Upsert');
        $customField = $this->createCustomField(name: 'Voltage', type: 'text', machine: $machine);

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

        // Create
        $data = $this->callMcpTool($session, 'upsert_custom_field_values', [
            'entityType' => 'machine',
            'entityId'   => $machine->getId(),
            'fields'     => [
                ['customFieldId' => $customField->getId(), 'value' => '220V'],
            ],
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $parsed = $data['_parsed'];
        $this->assertSame(1, $parsed['total']);
        $this->assertSame('created', $parsed['results'][0]['action']);
        $this->assertSame('220V', $parsed['results'][0]['value']);

        $createdId = $parsed['results'][0]['id'];

        // Update (upsert same field)
        $data = $this->callMcpTool($session, 'upsert_custom_field_values', [
            'entityType' => 'machine',
            'entityId'   => $machine->getId(),
            'fields'     => [
                ['customFieldId' => $customField->getId(), 'value' => '380V'],
            ],
        ]);

        $parsed = $data['_parsed'];
        $this->assertSame('updated', $parsed['results'][0]['action']);
        $this->assertSame('380V', $parsed['results'][0]['value']);
        $this->assertSame($createdId, $parsed['results'][0]['id']);
    }

    public function testDeleteCustomFieldValue(): void
    {
        $machine     = $this->createMachine(name: 'Machine Delete CF');
        $customField = $this->createCustomField(name: 'Weight', type: 'text', machine: $machine);
        $cfv         = $this->createCustomFieldValue(customField: $customField, value: '150kg', machine: $machine);

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

        $data = $this->callMcpTool($session, 'delete_custom_field_value', [
            'customFieldValueId' => $cfv->getId(),
        ]);

        $this->assertArrayHasKey('_parsed', $data);
        $parsed = $data['_parsed'];
        $this->assertTrue($parsed['deleted']);
        $this->assertSame($cfv->getId(), $parsed['id']);

        // Verify it's gone
        $listData = $this->callMcpTool($session, 'list_custom_field_values', [
            'entityType' => 'machine',
            'entityId'   => $machine->getId(),
        ]);

        $this->assertSame(0, $listData['_parsed']['total']);
    }
}
