<?php

declare(strict_types=1);

use Builder\Core\Foundation\Definition\Field;

it('viewPermission defaults to null', function (): void {
    expect(Field::text('title')->getViewPermission())->toBeNull();
});

it('updatePermission defaults to null', function (): void {
    expect(Field::text('title')->getUpdatePermission())->toBeNull();
});

it('canView sets the view permission', function (): void {
    $field = Field::text('notes')->canView('read:internal');

    expect($field->getViewPermission())->toBe('read:internal');
});

it('canUpdate sets the update permission', function (): void {
    $field = Field::text('notes')->canUpdate('update:internal');

    expect($field->getUpdatePermission())->toBe('update:internal');
});

it('canView accepts null to clear the permission', function (): void {
    $field = Field::text('notes')->canView('read:internal')->canView(null);

    expect($field->getViewPermission())->toBeNull();
});

it('canUpdate accepts null to clear the permission', function (): void {
    $field = Field::text('notes')->canUpdate('update:internal')->canUpdate(null);

    expect($field->getUpdatePermission())->toBeNull();
});

it('toArray includes viewPermission and updatePermission', function (): void {
    $field = Field::text('notes')
        ->canView('read:internal')
        ->canUpdate('update:internal');

    $array = $field->toArray();

    expect($array['viewPermission'])->toBe('read:internal')
        ->and($array['updatePermission'])->toBe('update:internal');
});

it('toArray has null viewPermission and updatePermission by default', function (): void {
    $array = Field::text('title')->toArray();

    expect($array['viewPermission'])->toBeNull()
        ->and($array['updatePermission'])->toBeNull();
});
