<?php

declare(strict_types=1);

use Builder\Core\Foundation\Definition\Field;
use Builder\Core\Foundation\Enums\Icon;
use Builder\Seo\Settings\SeoSettings;

it('group returns seo', function (): void {
    expect(SeoSettings::group())->toBe('seo');
});

it('getKey returns seo', function (): void {
    $settings = app(SeoSettings::class);

    expect($settings->getKey())->toBe('seo');
});

it('getLabel returns translated label', function (): void {
    $settings = app(SeoSettings::class);

    expect($settings->getLabel())->toBeString()->not->toBeEmpty();
});

it('getIcon returns Search icon value', function (): void {
    $settings = app(SeoSettings::class);

    expect($settings->getIcon())->toBe(Icon::Search->value);
});

it('getPriority returns 30', function (): void {
    $settings = app(SeoSettings::class);

    expect($settings->getPriority())->toBe(30);
});

it('getPermission returns null (access controlled by admin middleware)', function (): void {
    $settings = app(SeoSettings::class);

    expect($settings->getPermission())->toBeNull();
});

it('getParent returns null', function (): void {
    $settings = app(SeoSettings::class);

    expect($settings->getParent())->toBeNull();
});

it('fields contains all ten expected field names', function (): void {
    $settings = app(SeoSettings::class);
    $names = collect($settings->fields()->toFields())->map(fn (Field $f): string => $f->getName())->all();

    expect($names)->toContain(
        'site_name',
        'tagline',
        'separator',
        'favicon_url',
        'default_og_image_url',
        'schema_type',
        'schema_name',
        'schema_url',
        'schema_logo_url',
        'schema_description',
    );
});

it('rules contain keys for all ten properties', function (): void {
    $settings = app(SeoSettings::class);
    $rules = $settings->rules();

    expect(array_keys($rules))->toContain(
        'site_name',
        'tagline',
        'separator',
        'favicon_url',
        'default_og_image_url',
        'schema_type',
        'schema_name',
        'schema_url',
        'schema_logo_url',
        'schema_description',
    );
});

it('default separator property is bare pipe symbol', function (): void {
    $settings = app(SeoSettings::class);

    expect($settings->separator)->toBe('|');
});

it('settingsToArray returns raw separator symbol', function (): void {
    $settings = app(SeoSettings::class);

    expect($settings->settingsToArray()['separator'])->toBe('|');
});

it('default schema_type is Organization', function (): void {
    $settings = app(SeoSettings::class);

    expect($settings->schema_type)->toBe('Organization');
});

it('settingsToArray pre-fills site_name from app.name when null', function (): void {
    config(['app.name' => 'Test App Name']);

    $settings = app(SeoSettings::class);
    $settings->site_name = null;

    $data = $settings->settingsToArray();

    expect($data['site_name'])->toBe('Test App Name');
});

it('settingsToArray pre-fills schema_url from app.url when null', function (): void {
    config(['app.url' => 'https://test-app.example']);

    $settings = app(SeoSettings::class);
    $settings->schema_url = null;

    $data = $settings->settingsToArray();

    expect($data['schema_url'])->toBe('https://test-app.example');
});

it('settingsToArray passes null separator through unchanged', function (): void {
    $settings = app(SeoSettings::class);
    $settings->separator = null;

    $data = $settings->settingsToArray();

    expect($data['separator'])->toBeNull();
});

it('navGroup returns empty string to opt out of settings nav', function (): void {
    $settings = app(SeoSettings::class);

    expect($settings->navGroup())->toBe('');
});

it('fields uses FieldMediaUrlInput for image fields when media module is bound', function (): void {
    app()->bind('builder.media', fn (): bool => true);

    $settings = app(SeoSettings::class);
    $fields = collect($settings->fields()->toFields());

    $ogField = $fields->firstWhere(fn (Field $f): bool => $f->getName() === 'default_og_image_url');

    expect($ogField?->getComponent())->toBe('FieldMediaUrlInput');
});
