<?php

use App\Models\Service;
use App\Models\ServiceAgendaItem;
use App\Models\Slide;
use App\Models\User;

beforeEach(function () {
    $this->user = User::factory()->create();
    $this->actingAs($this->user);
});

/*
|--------------------------------------------------------------------------
| Sermon Block — Service-Specific Slides
|--------------------------------------------------------------------------
*/

test('sermon slides are service-specific', function () {
    $serviceA = Service::factory()->create(['title' => 'Service A']);
    $serviceB = Service::factory()->create(['title' => 'Service B']);

    // Create sermon slides for Service A
    $slideA1 = Slide::factory()->create([
        'type' => 'sermon',
        'service_id' => $serviceA->id,
        'original_filename' => 'sermon-a-1.jpg',
    ]);

    $slideA2 = Slide::factory()->create([
        'type' => 'sermon',
        'service_id' => $serviceA->id,
        'original_filename' => 'sermon-a-2.jpg',
    ]);

    // Create sermon slides for Service B
    $slideB1 = Slide::factory()->create([
        'type' => 'sermon',
        'service_id' => $serviceB->id,
        'original_filename' => 'sermon-b-1.jpg',
    ]);

    // Verify Service A only has its own sermon slides
    $serviceASermonSlides = Slide::where('type', 'sermon')
        ->where('service_id', $serviceA->id)
        ->get();

    expect($serviceASermonSlides)->toHaveCount(2);
    expect($serviceASermonSlides->pluck('id'))->toContain($slideA1->id, $slideA2->id);
    expect($serviceASermonSlides->pluck('id'))->not->toContain($slideB1->id);

    // Verify Service B only has its own sermon slides
    $serviceBSermonSlides = Slide::where('type', 'sermon')
        ->where('service_id', $serviceB->id)
        ->get();

    expect($serviceBSermonSlides)->toHaveCount(1);
    expect($serviceBSermonSlides->pluck('id'))->toContain($slideB1->id);
    expect($serviceBSermonSlides->pluck('id'))->not->toContain($slideA1->id, $slideA2->id);
});

test('sermon slides do not include information slides', function () {
    $service = Service::factory()->create();

    // Create sermon slide
    $sermonSlide = Slide::factory()->create([
        'type' => 'sermon',
        'service_id' => $service->id,
    ]);

    // Create information slide for same service
    $informationSlide = Slide::factory()->create([
        'type' => 'information',
        'service_id' => $service->id,
    ]);

    // Create moderation slide for same service
    $moderationSlide = Slide::factory()->create([
        'type' => 'moderation',
        'service_id' => $service->id,
    ]);

    // Query only sermon slides
    $sermonSlides = Slide::where('type', 'sermon')
        ->where('service_id', $service->id)
        ->get();

    expect($sermonSlides)->toHaveCount(1);
    expect($sermonSlides->first()->id)->toBe($sermonSlide->id);
    expect($sermonSlides->pluck('id'))->not->toContain($informationSlide->id, $moderationSlide->id);
});

test('sermon slides require service_id', function () {
    // Attempt to create sermon slide without service_id
    $slide = Slide::factory()->make([
        'type' => 'sermon',
        'service_id' => null,
    ]);

    // This should fail validation in the controller
    // (SlideController validates that sermon slides require service_id)
    expect($slide->service_id)->toBeNull();
});

test('sermon block filters slides correctly', function () {
    $serviceA = Service::factory()->create();
    $serviceB = Service::factory()->create();

    // Create mixed slides
    $sermonA = Slide::factory()->create(['type' => 'sermon', 'service_id' => $serviceA->id]);
    $sermonB = Slide::factory()->create(['type' => 'sermon', 'service_id' => $serviceB->id]);
    $infoA = Slide::factory()->create(['type' => 'information', 'service_id' => $serviceA->id]);
    $modA = Slide::factory()->create(['type' => 'moderation', 'service_id' => $serviceA->id]);

    // Simulate SermonBlock filtering for Service A
    $allSlides = Slide::all();
    $filteredSlides = $allSlides->filter(
        fn ($slide) => $slide->type === 'sermon' && $slide->service_id === $serviceA->id
    );

    expect($filteredSlides)->toHaveCount(1);
    expect($filteredSlides->first()->id)->toBe($sermonA->id);
});

test('sermon slides do not have expire_date field', function () {
    $service = Service::factory()->create();

    // Create sermon slide with expire_date (should be ignored or null)
    $slide = Slide::factory()->create([
        'type' => 'sermon',
        'service_id' => $service->id,
        'expire_date' => null,
    ]);

    expect($slide->expire_date)->toBeNull();
});

/*
|--------------------------------------------------------------------------
| Sermon Slides — Agenda Item Association
|--------------------------------------------------------------------------
*/

test('sermon slides can be linked to agenda items', function () {
    $service = Service::factory()->create();
    $agendaItem = ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Predigt: Hoffnung',
        'sort_order' => 1,
        'is_before_event' => false,
    ]);

    $slide = Slide::factory()->create([
        'type' => 'sermon',
        'service_id' => $service->id,
        'service_agenda_item_id' => $agendaItem->id,
    ]);

    expect($slide->serviceAgendaItem)->not->toBeNull();
    expect($slide->serviceAgendaItem->id)->toBe($agendaItem->id);
    expect($agendaItem->slides)->toHaveCount(1);
    expect($agendaItem->slides->first()->id)->toBe($slide->id);
});

test('sermon slides on agenda items are scoped per item', function () {
    $service = Service::factory()->create();
    $item1 = ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Predigt Teil 1',
        'sort_order' => 1,
        'is_before_event' => false,
    ]);
    $item2 = ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Predigt Teil 2',
        'sort_order' => 2,
        'is_before_event' => false,
    ]);

    $slide1 = Slide::factory()->create([
        'type' => 'sermon',
        'service_id' => $service->id,
        'service_agenda_item_id' => $item1->id,
    ]);
    $slide2 = Slide::factory()->create([
        'type' => 'sermon',
        'service_id' => $service->id,
        'service_agenda_item_id' => $item2->id,
    ]);

    expect($item1->slides)->toHaveCount(1);
    expect($item1->slides->first()->id)->toBe($slide1->id);
    expect($item2->slides)->toHaveCount(1);
    expect($item2->slides->first()->id)->toBe($slide2->id);
});

test('legacy sermon slides without agenda item still work', function () {
    $service = Service::factory()->create();

    // Legacy slide: service_id set, no agenda item link
    $slide = Slide::factory()->create([
        'type' => 'sermon',
        'service_id' => $service->id,
        'service_agenda_item_id' => null,
    ]);

    expect($slide->service_agenda_item_id)->toBeNull();
    expect($slide->serviceAgendaItem)->toBeNull();

    // Still queryable via legacy filter
    $legacySlides = Slide::where('type', 'sermon')
        ->where('service_id', $service->id)
        ->get();

    expect($legacySlides)->toHaveCount(1);
    expect($legacySlides->first()->id)->toBe($slide->id);
});
