<?php

use App\Models\Macro;
use App\Models\Setting;
use App\Services\NameTagSlideBuilder;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('build returns null when namenseinblender macro is not configured', function () {
    expect(app(NameTagSlideBuilder::class)->build('Max Mustermann', 'Moderation'))->toBeNull();
});

test('build returns text lines and macro when configured', function () {
    Setting::set('namenseinblender_macro_name', 'Namenseinblender');
    Setting::set('namenseinblender_macro_uuid', '11111111-1111-1111-1111-111111111111');
    Setting::set('namenseinblender_macro_collection_name', 'Service Macros');
    Setting::set('namenseinblender_macro_collection_uuid', '22222222-2222-2222-2222-222222222222');

    $slide = app(NameTagSlideBuilder::class)->build('Anna Müller', 'Moderation');

    expect($slide)->toBe([
        'text' => 'Anna Müller',
        'macro' => [
            'name' => 'Namenseinblender',
            'uuid' => '11111111-1111-1111-1111-111111111111',
            'collectionName' => 'Service Macros',
            'collectionUuid' => '22222222-2222-2222-2222-222222222222',
        ],
        'subtitle' => 'Moderation',
    ]);
});

test('convenience methods use moderator and preacher titles', function () {
    Setting::set('namenseinblender_macro_name', 'Namenseinblender');
    Setting::set('namenseinblender_macro_uuid', '11111111-1111-1111-1111-111111111111');
    Setting::set('namenseinblender_macro_collection_name', 'Service Macros');
    Setting::set('namenseinblender_macro_collection_uuid', '22222222-2222-2222-2222-222222222222');

    $builder = app(NameTagSlideBuilder::class);

    $moderatorSlide = $builder->buildModeratorSlide('Max Mustermann');
    $preacherSlide = $builder->buildPreacherSlide('Erika Beispiel');

    expect($moderatorSlide['text'])->toBe('Max Mustermann')
        ->and($moderatorSlide['subtitle'])->toBe('Moderation')
        ->and($preacherSlide['text'])->toBe('Erika Beispiel')
        ->and($preacherSlide['subtitle'])->toBe('Predigt');
});

test('build returns slide with macro when macro_id points to existing macro', function () {
    $macro = Macro::factory()->create([
        'name' => 'Namenseinblender',
        'uuid' => 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
    ]);
    Setting::set('namenseinblender_macro_id', (string) $macro->id);

    $slide = app(NameTagSlideBuilder::class)->build('Lea Leiter', 'Moderation');

    expect($slide)->toHaveKey('text', 'Lea Leiter')
        ->and($slide)->toHaveKey('subtitle', 'Moderation')
        ->and($slide)->toHaveKey('macro')
        ->and($slide['macro']['name'])->toBe('Namenseinblender')
        ->and($slide['macro']['uuid'])->toBe('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa');
});

test('build returns slide without macro when macro_id points to missing macro', function () {
    Setting::set('namenseinblender_macro_id', '99999');

    $slide = app(NameTagSlideBuilder::class)->build('Lea Leiter', 'Moderation');

    expect($slide)->toBe(['text' => 'Lea Leiter', 'subtitle' => 'Moderation']);
});
