<?php

declare(strict_types=1);

use Builder\Core\Foundation\Definition\Field;
use Builder\Core\Foundation\Definition\Section;

it('make creates a Section instance', function (): void {
    expect(Section::make('content'))->toBeInstanceOf(Section::class);
});

it('key defaults as label', function (): void {
    expect(Section::make('content')->getLabel())->toBe('content');
});

it('label sets and returns self', function (): void {
    $section = Section::make('content');
    $result = $section->label('Content');

    expect($result)->toBe($section)
        ->and($section->getLabel())->toBe('Content');
});

it('description defaults to null', function (): void {
    expect(Section::make('content')->getDescription())->toBeNull();
});

it('description sets and returns self', function (): void {
    $section = Section::make('content');
    $result = $section->description('Fill in the content');

    expect($result)->toBe($section)
        ->and($section->getDescription())->toBe('Fill in the content');
});

it('collapsible defaults to false', function (): void {
    expect(Section::make('seo')->isCollapsible())->toBeFalse();
});

it('collapsible sets to true', function (): void {
    expect(Section::make('seo')->collapsible()->isCollapsible())->toBeTrue();
});

it('collapsible sets to explicit false', function (): void {
    expect(Section::make('seo')->collapsible(false)->isCollapsible())->toBeFalse();
});

it('priority defaults to 0', function (): void {
    expect(Section::make('content')->getPriority())->toBe(0);
});

it('priority sets value and returns self', function (): void {
    $section = Section::make('seo');
    $result = $section->priority(10);

    expect($result)->toBe($section)
        ->and($section->getPriority())->toBe(10);
});

it('getKey returns the key', function (): void {
    expect(Section::make('metadata')->getKey())->toBe('metadata');
});

it('fields defaults to empty array', function (): void {
    expect(Section::make('content')->getFields())->toBe([]);
});

it('fields sets and returns self', function (): void {
    $field = Field::make('title');
    $section = Section::make('content');
    $result = $section->fields([$field]);

    expect($result)->toBe($section)
        ->and($section->getFields())->toBe([$field]);
});

it('toArray includes fields as arrays', function (): void {
    $section = Section::make('content')
        ->label('Content')
        ->description('Fill in content')
        ->collapsible()
        ->priority(5)
        ->fields([Field::make('title')->label('Title')]);

    $array = $section->toArray();

    expect($array['key'])->toBe('content')
        ->and($array['label'])->toBe('Content')
        ->and($array['description'])->toBe('Fill in content')
        ->and($array['collapsible'])->toBeTrue()
        ->and($array['priority'])->toBe(5)
        ->and($array['fields'])->toBeArray()
        ->and($array['fields'][0]['name'])->toBe('title');
});
