<?php

use App\Models\Service;
use Illuminate\Support\Facades\Schema;

beforeEach(function () {
    // Migration rollback is validated via schema assertions; run `ddev exec php artisan migrate:rollback --step=1` manually if needed.
});

test('key visual url returns storage path when filename exists', function () {
    $service = Service::factory()->create([
        'key_visual_filename' => 'slides/a.jpg',
    ]);

    expect($service->keyVisualUrl)->toBe('/storage/slides/a.jpg');
});

test('key visual url returns null when filename is missing', function () {
    $service = Service::factory()->create([
        'key_visual_filename' => null,
    ]);

    expect($service->keyVisualUrl)->toBeNull();
});

test('service fillable persists image and name override columns', function () {
    $service = Service::factory()->create();

    $service->fill([
        'key_visual_filename' => 'slides/key.jpg',
        'background_filename' => 'slides/background.jpg',
        'moderator_name' => 'Max Mustermann',
        'preacher_name_override' => 'Lisa Beispiel',
    ]);

    $service->save();

    $fresh = $service->fresh();

    expect($fresh->key_visual_filename)->toBe('slides/key.jpg')
        ->and($fresh->background_filename)->toBe('slides/background.jpg')
        ->and($fresh->moderator_name)->toBe('Max Mustermann')
        ->and($fresh->preacher_name_override)->toBe('Lisa Beispiel')
        ->and($fresh->backgroundUrl)->toBe('/storage/slides/background.jpg');
});

test('services table has the new image and override columns', function () {
    expect(Schema::hasColumn('services', 'key_visual_filename'))->toBeTrue()
        ->and(Schema::hasColumn('services', 'background_filename'))->toBeTrue()
        ->and(Schema::hasColumn('services', 'moderator_name'))->toBeTrue()
        ->and(Schema::hasColumn('services', 'preacher_name_override'))->toBeTrue();
});
