<?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);
});

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    $slide = Slide::factory()->create([
        'type' => 'moderation',
        '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('moderation slides on agenda items are scoped per item', function () {
    $service = Service::factory()->create();
    $item1 = ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Hinweise',
        'sort_order' => 1,
        'is_before_event' => false,
    ]);
    $item2 = ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Begrüßung',
        'sort_order' => 2,
        'is_before_event' => false,
    ]);

    $slide1 = Slide::factory()->create([
        'type' => 'moderation',
        'service_id' => $service->id,
        'service_agenda_item_id' => $item1->id,
    ]);
    $slide2 = Slide::factory()->create([
        'type' => 'moderation',
        '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 moderation 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' => 'moderation',
        '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', 'moderation')
        ->where('service_id', $service->id)
        ->get();

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