<?php

declare(strict_types=1);

use Builder\Core\Foundation\Registry;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Schema;

it('loads configuration', function (): void {
    expect(config('builder.prefix'))->toBe('admin');
});

it('registers registry as singleton', function (): void {
    expect(app(Registry::class))->toBe(app(Registry::class));
});

it('blade directive with empty expression defaults to builder entry', function (): void {
    $directive = Blade::getCustomDirectives()['builderAssets'];
    $result = $directive('');

    expect($result)->toBe("<?php echo \\Builder\\Core\\Foundation\\Assets::tags('builder'); ?>");
});

it('blade directive with expression uses the given expression', function (): void {
    $directive = Blade::getCustomDirectives()['builderAssets'];
    $result = $directive("'custom'");

    expect($result)->toBe("<?php echo \\Builder\\Core\\Foundation\\Assets::tags('custom'); ?>");
});

it('Blueprint::publishingColumns macro creates status and publish_at columns', function (): void {
    Schema::create('test_publishing_macro_cols', function (Blueprint $table): void {
        $table->id();
        $table->publishingColumns();
        $table->timestamps();
    });

    expect(Schema::hasColumn('test_publishing_macro_cols', 'status'))->toBeTrue()
        ->and(Schema::hasColumn('test_publishing_macro_cols', 'publish_at'))->toBeTrue();

    Schema::dropIfExists('test_publishing_macro_cols');
});

it('registers builder scheduled commands', function (): void {
    $schedule = app(Schedule::class);
    $commands = collect($schedule->events())->map(fn (Event $e) => $e->command ?? '');

    expect($commands->some(fn (string $c): bool => str_contains($c, 'builder:prune-revisions')))->toBeTrue()
        ->and($commands->some(fn (string $c): bool => str_contains($c, 'builder:publish')))->toBeTrue();
});
