<?php

use App\Models\Service;
use App\Models\Setting;
use App\Models\User;
use App\Services\ChurchToolsService;
use App\Services\ServiceImageResolver;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

beforeEach(function () {
    Storage::fake('public');
});

test('finalize nimmt snapshot des globalen hintergrunds wenn service spalte null ist', function () {
    Storage::disk('public')->put('slides/bg1.jpg', 'fake-content');
    Setting::set('current_background', 'slides/bg1.jpg');

    $service = Service::factory()->create([
        'background_filename' => null,
        'finalized_at' => null,
    ]);

    $this->actingAs(User::factory()->create())
        ->postJson(route('services.finalize', $service), ['confirmed' => true])
        ->assertOk();

    expect($service->fresh()->background_filename)->toBe('slides/bg1.jpg');
});

test('snapshot gewinnt: neuer globaler hintergrund beeinflusst abgeschlossenen service nicht', function () {
    Storage::disk('public')->put('slides/bg1.jpg', 'fake-content');
    Setting::set('current_background', 'slides/bg1.jpg');

    $service = Service::factory()->create([
        'background_filename' => null,
        'finalized_at' => null,
    ]);

    $this->actingAs(User::factory()->create())
        ->postJson(route('services.finalize', $service), ['confirmed' => true])
        ->assertOk();

    Storage::disk('public')->put('slides/bg2.jpg', 'fake-content');
    Setting::set('current_background', 'slides/bg2.jpg');

    $resolved = app(ServiceImageResolver::class)->backgroundFor($service->fresh());

    expect($resolved)->toBe('slides/bg1.jpg');
});

test('cts sync ueberschreibt key_visual_filename und moderator_name nicht', function () {
    Carbon::setTestNow('2026-03-01 09:00:00');

    $service = Service::factory()->create([
        'cts_event_id' => '424242',
        'key_visual_filename' => 'custom.jpg',
        'moderator_name' => 'Hans',
        'preacher_name_override' => 'Pastor X',
        'background_filename' => 'bg-custom.jpg',
    ]);

    $sync = new ChurchToolsService(
        eventFetcher: fn () => [
            new FakeEvent(
                id: 424242,
                title: 'Aktualisierter Titel',
                startDate: '2026-03-08T10:00:00+00:00',
                eventServices: [
                    new FakeEventService('Predigt', new FakePerson('Neuer', 'Prediger')),
                ],
            ),
        ],
        songFetcher: fn () => [],
        agendaFetcher: fn () => new FakeAgenda([]),
        eventServiceFetcher: fn (int $eventId) => [
            new FakeEventService('Predigt', new FakePerson('Neuer', 'Prediger')),
        ],
    );

    $sync->sync();

    $fresh = DB::table('services')->where('cts_event_id', '424242')->first();

    expect($fresh->key_visual_filename)->toBe('custom.jpg');
    expect($fresh->moderator_name)->toBe('Hans');
    expect($fresh->preacher_name_override)->toBe('Pastor X');
    expect($fresh->background_filename)->toBe('bg-custom.jpg');
    expect($fresh->title)->toBe('Aktualisierter Titel');
});
