<?php

declare(strict_types=1);

use Builder\Seo\Settings\SeoSettings;
use Builder\Seo\Tests\SeoTestUser;
use Inertia\Testing\AssertableInertia;

beforeEach(function (): void {
    (new SeoSettings)->seedDefaults();
    app()->forgetInstance(SeoSettings::class);
});

it('unauthenticated request redirects to login', function (): void {
    $this->get(route('builder.settings.show', 'seo'))
        ->assertRedirect();
});

it('renders the SEO settings page', function (): void {
    $user = SeoTestUser::create([
        'name' => 'Admin',
        'email' => 'admin@example.com',
        'password' => 'secret',
    ]);

    $this->actingAs($user)
        ->get(route('builder.settings.show', 'seo'))
        ->assertOk()
        ->assertInertia(fn (AssertableInertia $page): AssertableInertia => $page
            ->component('Settings/Show')
            ->where('settingsDefinition.key', 'seo'),
        );
});

it('PUT saves SEO settings and redirects', function (): void {
    $user = SeoTestUser::create([
        'name' => 'Admin',
        'email' => 'admin@example.com',
        'password' => 'secret',
    ]);

    $this->actingAs($user)
        ->put(route('builder.settings.update', 'seo'), [
            'site_name' => 'Acme',
            'tagline' => 'Making great things',
            'separator' => '|',
            'default_og_image_url' => null,
            'schema_type' => 'Organization',
            'schema_name' => 'Acme Corp',
            'schema_url' => 'https://acme.example',
            'schema_logo_url' => null,
            'schema_description' => null,
        ])
        ->assertRedirect();

    app()->forgetInstance(SeoSettings::class);
    expect(app(SeoSettings::class)->site_name)->toBe('Acme')
        ->and(app(SeoSettings::class)->separator)->toBe('|');
});

it('seedDefaults pre-fills schema_url from app.url when empty', function (): void {
    config(['app.url' => 'https://my-site.example']);

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

    (new SeoSettings)->seedDefaults();
    app()->forgetInstance(SeoSettings::class);

    expect(app(SeoSettings::class)->schema_url)->toBe('https://my-site.example');
});

it('seedDefaults pre-fills site_name from app.name when empty', function (): void {
    config(['app.name' => 'My Awesome Site']);

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

    (new SeoSettings)->seedDefaults();
    app()->forgetInstance(SeoSettings::class);

    expect(app(SeoSettings::class)->site_name)->toBe('My Awesome Site');
});

it('PUT saves null fields without error', function (): void {
    $user = SeoTestUser::create([
        'name' => 'Admin',
        'email' => 'admin-null@example.com',
        'password' => 'secret',
    ]);

    $this->actingAs($user)
        ->put(route('builder.settings.update', 'seo'), [
            'site_name' => null,
            'tagline' => null,
            'separator' => ' | ',
            'default_og_image_url' => null,
            'schema_type' => 'Organization',
            'schema_name' => null,
            'schema_url' => null,
            'schema_logo_url' => null,
            'schema_description' => null,
        ])
        ->assertRedirect();

    app()->forgetInstance(SeoSettings::class);
    expect(app(SeoSettings::class)->site_name)->toBeNull();
});
