<?php

declare(strict_types=1);

use Builder\Core\Models\Language;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

it('returns SUCCESS and outputs check labels when all checks pass', function (): void {
    // The migrations table, languages, and assets dir are set up by the test infrastructure.
    $this->artisan('builder:check')
        ->expectsOutputToContain('Builder Installation Check')
        ->expectsOutputToContain('Migrations run')
        ->expectsOutputToContain('Languages seeded')
        ->expectsOutputToContain('Default language set')
        ->expectsOutputToContain('Assets linked')
        ->assertSuccessful();
});

it('returns FAILURE when no languages are seeded', function (): void {
    Language::query()->delete();

    $this->artisan('builder:check')
        ->assertFailed();
});

it('returns FAILURE and shows FAIL when DB throws in migrationsAreUp', function (): void {
    DB::shouldReceive('table')
        ->andThrow(new RuntimeException('DB connection failed'));

    $this->artisan('builder:check')
        ->expectsOutputToContain('FAIL')
        ->assertFailed();
});

it('returns FAILURE when Schema throws in languagesSeeded', function (): void {
    Schema::shouldReceive('hasTable')
        ->andThrow(new RuntimeException('Schema error'));

    $this->artisan('builder:check')
        ->assertFailed();
});

it('returns FAILURE when Schema throws in defaultLanguageSet', function (): void {
    Schema::shouldReceive('hasTable')
        ->once()
        ->andReturn(true)
        ->shouldReceive('hasTable')
        ->andThrow(new RuntimeException('Schema error'));

    $this->artisan('builder:check')
        ->assertFailed();
});
