<?php

declare(strict_types=1);

use Builder\Core\Foundation\Definition\Section;

it('viewPermission defaults to null', function (): void {
    expect(Section::make('general')->getViewPermission())->toBeNull();
});

it('updatePermission defaults to null', function (): void {
    expect(Section::make('general')->getUpdatePermission())->toBeNull();
});

it('canView sets the view permission', function (): void {
    $section = Section::make('seo')->canView('read:seo');

    expect($section->getViewPermission())->toBe('read:seo');
});

it('canUpdate sets the update permission', function (): void {
    $section = Section::make('seo')->canUpdate('update:seo');

    expect($section->getUpdatePermission())->toBe('update:seo');
});

it('canView accepts null to clear the permission', function (): void {
    $section = Section::make('seo')->canView('read:seo')->canView(null);

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

it('canUpdate accepts null to clear the permission', function (): void {
    $section = Section::make('seo')->canUpdate('update:seo')->canUpdate(null);

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

it('toArray includes viewPermission and updatePermission', function (): void {
    $section = Section::make('seo')
        ->canView('read:seo')
        ->canUpdate('update:seo');

    $array = $section->toArray();

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

it('toArray has null viewPermission and updatePermission by default', function (): void {
    $array = Section::make('general')->toArray();

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

it('canView is fluent and chainable', function (): void {
    $section = Section::make('advanced')
        ->label('Advanced')
        ->canView('read:advanced')
        ->canUpdate('update:advanced');

    expect($section->getViewPermission())->toBe('read:advanced')
        ->and($section->getUpdatePermission())->toBe('update:advanced')
        ->and($section->getLabel())->toBe('Advanced');
});

it('sidebar defaults to false', function (): void {
    $array = Section::make('general')->toArray();

    expect($array['sidebar'])->toBeFalse();
});

it('sidebar() sets sidebar to true', function (): void {
    $section = Section::make('meta')->sidebar();

    expect($section->toArray()['sidebar'])->toBeTrue();
});

it('sidebar(false) sets sidebar back to false', function (): void {
    $section = Section::make('meta')->sidebar()->sidebar(false);

    expect($section->toArray()['sidebar'])->toBeFalse();
});

it('tabGroup defaults to null', function (): void {
    $array = Section::make('general')->toArray();

    expect($array['tabGroup'])->toBeNull();
});

it('tab() sets the tabGroup', function (): void {
    $section = Section::make('content')->tab('main');

    expect($section->toArray()['tabGroup'])->toBe('main');
});

it('toArray includes sidebar and tabGroup', function (): void {
    $section = Section::make('seo')->sidebar()->tab('metadata');

    $array = $section->toArray();

    expect($array['sidebar'])->toBeTrue()
        ->and($array['tabGroup'])->toBe('metadata');
});
