<?php

declare(strict_types=1);

namespace App\Tests\Api\Controller;

use App\Tests\AbstractApiTestCase;

/**
 * @internal
 */
class MachineStructureCustomFieldValuesTest extends AbstractApiTestCase
{
    public function testStructureIncludesCustomFieldValues(): void
    {
        $machine = $this->createMachine('Machine CFV');
        $cf      = $this->createCustomField('Tension', 'number', $machine);
        $this->createCustomFieldValue($cf, '220', $machine);

        $client = $this->createViewerClient();
        $client->request('GET', sprintf('/api/machines/%s/structure', $machine->getId()));

        $this->assertResponseIsSuccessful();
        $data = $client->getResponse()->toArray();

        $this->assertArrayHasKey('machine', $data);
        $this->assertArrayHasKey('customFieldValues', $data['machine']);
        $this->assertIsArray($data['machine']['customFieldValues']);
        $this->assertNotEmpty($data['machine']['customFieldValues'], 'customFieldValues should not be empty');

        $cfv = $data['machine']['customFieldValues'][0];
        $this->assertSame('220', $cfv['value']);
        $this->assertArrayHasKey('customField', $cfv);
        $this->assertSame('Tension', $cfv['customField']['name']);
        $this->assertSame('number', $cfv['customField']['type']);
    }

    public function testStructureReturnsEmptyArrayWhenNoCustomFieldValues(): void
    {
        $machine = $this->createMachine('Machine sans CFV');

        $client = $this->createViewerClient();
        $client->request('GET', sprintf('/api/machines/%s/structure', $machine->getId()));

        $this->assertResponseIsSuccessful();
        $data = $client->getResponse()->toArray();

        $this->assertArrayHasKey('customFieldValues', $data['machine']);
        $this->assertSame([], $data['machine']['customFieldValues']);
    }
}
