<?php

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

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

/*
|--------------------------------------------------------------------------
| Information Block — Global Slides with Expire Date Filtering
|--------------------------------------------------------------------------
*/

test('information slides shown dynamically by expire date', function () {
    Carbon::setTestNow('2026-03-15 10:00:00');

    $service = Service::factory()->create(['date' => '2026-03-15']);

    // Slide expires tomorrow (should show)
    $validSlide = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-16',
        'uploaded_at' => '2026-03-14 10:00:00',
    ]);

    // Slide expired yesterday (should NOT show)
    Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-14',
        'uploaded_at' => '2026-03-14 10:00:00',
    ]);

    $response = $this->get(route('services.edit', $service));

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->component('Services/Edit')
            ->has('informationSlides', 1)
            ->where('informationSlides.0.id', $validSlide->id)
    );
});

test('information slides expire on service date are still shown', function () {
    Carbon::setTestNow('2026-04-01 10:00:00');

    $service = Service::factory()->create(['date' => '2026-04-10']);

    // Slide expires exactly on service date (should show — expire_date >= service.date)
    $sameDay = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-04-10',
        'uploaded_at' => '2026-03-30 10:00:00',
    ]);

    // Slide expires one day before service (should NOT show)
    Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-04-09',
        'uploaded_at' => '2026-03-30 10:00:00',
    ]);

    $response = $this->get(route('services.edit', $service));

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('informationSlides', 1)
            ->where('informationSlides.0.id', $sameDay->id)
    );
});

test('information slides are global and appear in all services where not expired', function () {
    Carbon::setTestNow('2026-03-01 10:00:00');

    $serviceA = Service::factory()->create(['date' => '2026-03-10']);
    $serviceB = Service::factory()->create(['date' => '2026-03-20']);

    // Global slide expiring 2026-03-15 — should appear in Service A, NOT in Service B
    $slideEarly = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-15',
        'uploaded_at' => '2026-02-28 10:00:00',
    ]);

    // Global slide expiring 2026-03-25 — should appear in BOTH services
    $slideLate = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-25',
        'uploaded_at' => '2026-02-28 10:00:00',
    ]);

    // Service A: both slides should appear (both expire_date >= 2026-03-10)
    $responseA = $this->get(route('services.edit', $serviceA));
    $responseA->assertOk();
    $responseA->assertInertia(
        fn ($page) => $page
            ->has('informationSlides', 2)
    );

    // Service B: only slideLate should appear (slideEarly expire_date < 2026-03-20)
    $responseB = $this->get(route('services.edit', $serviceB));
    $responseB->assertOk();
    $responseB->assertInertia(
        fn ($page) => $page
            ->has('informationSlides', 1)
            ->where('informationSlides.0.id', $slideLate->id)
    );
});

test('soft deleted information slides are not shown', function () {
    Carbon::setTestNow('2026-03-01 10:00:00');

    $service = Service::factory()->create(['date' => '2026-03-10']);

    // Valid slide
    $activeSlide = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-20',
        'uploaded_at' => '2026-02-28 10:00:00',
    ]);

    // Soft-deleted slide
    $deletedSlide = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-20',
        'uploaded_at' => '2026-02-28 10:00:00',
    ]);
    $deletedSlide->delete();

    $response = $this->get(route('services.edit', $service));

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('informationSlides', 1)
            ->where('informationSlides.0.id', $activeSlide->id)
    );
});

test('information slides do not include moderation or sermon slides', function () {
    Carbon::setTestNow('2026-03-01 10:00:00');

    $service = Service::factory()->create(['date' => '2026-03-10']);

    // Information slide (should show)
    $infoSlide = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-20',
        'uploaded_at' => '2026-02-28 10:00:00',
    ]);

    // Moderation slide (should NOT show)
    Slide::factory()->create([
        'type' => 'moderation',
        'service_id' => $service->id,
    ]);

    // Sermon slide (should NOT show)
    Slide::factory()->create([
        'type' => 'sermon',
        'service_id' => $service->id,
    ]);

    $response = $this->get(route('services.edit', $service));

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('informationSlides', 1)
            ->where('informationSlides.0.id', $infoSlide->id)
    );
});

test('information slides without expire_date are shown', function () {
    Carbon::setTestNow('2026-03-01 10:00:00');

    $service = Service::factory()->create(['date' => '2026-03-10']);

    // Info slide with expire_date (should show)
    $withExpire = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-20',
        'uploaded_at' => '2026-02-01 10:00:00',
    ]);

    $withoutExpire = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => null,
        'uploaded_at' => '2026-02-15 10:00:00',
    ]);

    $response = $this->get(route('services.edit', $service));

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('informationSlides', 2)
            ->where('informationSlides.0.id', $withoutExpire->id)
            ->where('informationSlides.1.id', $withExpire->id)
    );
});

test('information slides uploaded after service date are not shown', function () {
    Carbon::setTestNow('2026-03-01 10:00:00');

    $service = Service::factory()->create(['date' => '2026-03-10']);

    $visibleSlide = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-20',
        'uploaded_at' => '2026-03-09 10:00:00',
    ]);

    Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-20',
        'uploaded_at' => '2026-03-12 10:00:00',
    ]);

    $response = $this->get(route('services.edit', $service));

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('informationSlides', 1)
            ->where('informationSlides.0.id', $visibleSlide->id)
    );
});

test('information slides ordered by uploaded_at descending', function () {
    Carbon::setTestNow('2026-03-01 10:00:00');

    $service = Service::factory()->create(['date' => '2026-03-10']);

    $older = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-20',
        'uploaded_at' => '2026-02-01 10:00:00',
    ]);

    $newer = Slide::factory()->create([
        'type' => 'information',
        'service_id' => null,
        'expire_date' => '2026-03-20',
        'uploaded_at' => '2026-02-15 10:00:00',
    ]);

    $response = $this->get(route('services.edit', $service));

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('informationSlides', 2)
            ->where('informationSlides.0.id', $newer->id)
            ->where('informationSlides.1.id', $older->id)
    );
});
