<?php

declare(strict_types=1);

use Builder\Core\Foundation\Definition\Field;
use Builder\Seo\Support\SeoField;

afterEach(function (): void {
    SeoField::reset();
});

it('getFields returns title, description, robots by default', function (): void {
    $fields = SeoField::getFields();
    $names = collect($fields)->pluck('name')->all();

    expect($names)->toBe(['title', 'description', 'robots']);
});

it('title field has max 70 rule and hint', function (): void {
    $field = collect(SeoField::getFields())->firstWhere('name', 'title');

    expect($field['rules'])->toContain('max:70')
        ->and($field['hint'])->not->toBeNull();
});

it('description field has max 170 rule and hint', function (): void {
    $field = collect(SeoField::getFields())->firstWhere('name', 'description');

    expect($field['rules'])->toContain('max:170')
        ->and($field['hint'])->not->toBeNull();
});

it('robots field has all six option keys', function (): void {
    $field = collect(SeoField::getFields())->firstWhere('name', 'robots');

    expect(array_keys($field['options']))->toBe([
        'index',
        'noindex',
        'nofollow',
        'noindex,nofollow',
        'noarchive',
        'nosnippet',
    ]);
});

it('robots field defaults to index', function (): void {
    $field = collect(SeoField::getFields())->firstWhere('name', 'robots');

    expect($field['default'])->toBe('index');
});

it('extend appends a custom field after the defaults', function (): void {
    SeoField::extend(Field::text('keywords')->label('Keywords'));

    $names = collect(SeoField::getFields())->pluck('name')->all();

    expect($names)->toBe(['title', 'description', 'robots', 'keywords']);
});

it('multiple extend calls accumulate in order', function (): void {
    SeoField::extend(Field::text('keywords')->label('Keywords'));
    SeoField::extend(Field::text('canonical')->label('Canonical URL'));

    $names = collect(SeoField::getFields())->pluck('name')->all();

    expect($names)->toBe(['title', 'description', 'robots', 'keywords', 'canonical']);
});

it('reset clears extended fields', function (): void {
    SeoField::extend(Field::text('keywords')->label('Keywords'));
    SeoField::reset();

    $names = collect(SeoField::getFields())->pluck('name')->all();

    expect($names)->toBe(['title', 'description', 'robots']);
});
