<?php

use App\Models\Service;
use App\Models\ServiceAgendaItem;
use App\Models\Setting;
use App\Services\NameTagResolver;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('moderator override wins', function () {
    $service = Service::factory()->create([
        'moderator_name' => 'Override Mod',
    ]);

    $name = app(NameTagResolver::class)->moderatorFor($service);

    expect($name)->toBe('Override Mod');
});

test('moderator falls back to first visible agenda item responsibles', function () {
    $service = Service::factory()->create([
        'moderator_name' => null,
    ]);

    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Vorprogramm',
        'is_before_event' => true,
        'responsible' => [['name' => 'Ignored Person']],
        'sort_order' => 1,
    ]);
    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Begrüßung',
        'is_before_event' => false,
        'responsible' => [
            ['name' => 'Anna Müller'],
            ['name' => 'Tom Klein'],
        ],
        'sort_order' => 2,
    ]);
    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Später',
        'is_before_event' => false,
        'responsible' => [['name' => 'Späte Person']],
        'sort_order' => 3,
    ]);

    $name = app(NameTagResolver::class)->moderatorFor($service);

    expect($name)->toBe('Anna Müller, Tom Klein');
});

test('moderator returns null without override or visible agenda item', function () {
    $service = Service::factory()->create([
        'moderator_name' => null,
    ]);

    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'is_before_event' => true,
        'responsible' => [['name' => 'Ignored Person']],
        'sort_order' => 1,
    ]);

    $name = app(NameTagResolver::class)->moderatorFor($service);

    expect($name)->toBeNull();
});

test('preacher override wins', function () {
    $service = Service::factory()->create([
        'preacher_name_override' => 'Gast Sprecher',
        'preacher_name' => 'Pfr. Lang',
    ]);

    $name = app(NameTagResolver::class)->preacherFor($service);

    expect($name)->toBe('Gast Sprecher');
});

test('preacher falls back to cts preacher name', function () {
    $service = Service::factory()->create([
        'preacher_name_override' => null,
        'preacher_name' => 'Pfr. Lang',
    ]);

    $name = app(NameTagResolver::class)->preacherFor($service);

    expect($name)->toBe('Pfr. Lang');
});

test('preacher falls back to sermon agenda item responsibles', function () {
    Setting::set('agenda_sermon_matching', 'Predigt*,Sermon*');
    $service = Service::factory()->create([
        'preacher_name_override' => null,
        'preacher_name' => null,
    ]);

    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Lied vor der Predigt',
        'service_song_id' => null,
        'responsible' => [['name' => 'Ignored Person']],
        'sort_order' => 1,
    ]);
    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Predigt',
        'service_song_id' => null,
        'responsible' => [['name' => 'Diakon Bauer']],
        'sort_order' => 2,
    ]);

    $name = app(NameTagResolver::class)->preacherFor($service);

    expect($name)->toBe('Diakon Bauer');
});

test('preacher returns null without override cts name or sermon responsibles', function () {
    Setting::set('agenda_sermon_matching', 'Predigt*');
    $service = Service::factory()->create([
        'preacher_name_override' => null,
        'preacher_name' => null,
    ]);

    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Begrüßung',
        'service_song_id' => null,
        'responsible' => [['name' => 'Moderation']],
        'sort_order' => 1,
    ]);

    $name = app(NameTagResolver::class)->preacherFor($service);

    expect($name)->toBeNull();
});

test('worship leader resolves from lobpreis role in responsible persons', function () {
    $service = Service::factory()->create();

    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Begrüßung',
        'is_before_event' => false,
        'responsible' => [
            'text' => '[Moderation]',
            'persons' => [
                ['service' => '[Moderation]', 'accepted' => true, 'person' => ['title' => 'Moderator Max']],
            ],
        ],
        'sort_order' => 1,
    ]);
    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Lobpreis',
        'is_before_event' => false,
        'responsible' => [
            'text' => '[Lobpreis]',
            'persons' => [
                ['service' => '[Lobpreis]', 'accepted' => true, 'person' => ['title' => 'Lea Leiter']],
            ],
        ],
        'sort_order' => 2,
    ]);

    $name = app(NameTagResolver::class)->worshipLeaderFor($service);

    expect($name)->toBe('Lea Leiter');
});

test('worship leader returns null without a lobpreis agenda item', function () {
    $service = Service::factory()->create();

    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Begrüßung',
        'is_before_event' => false,
        'responsible' => [['name' => 'Moderator Max']],
        'sort_order' => 1,
    ]);

    $name = app(NameTagResolver::class)->worshipLeaderFor($service);

    expect($name)->toBeNull();
});

test('name extracted from real cts persons shape via person.title', function () {
    $service = Service::factory()->create(['moderator_name' => null]);

    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Begrüßung',
        'is_before_event' => false,
        'sort_order' => 1,
        'responsible' => [
            'text' => '[Moderation]',
            'persons' => [
                [
                    'service' => '[Moderation]',
                    'accepted' => true,
                    'person' => [
                        'title' => 'Kornelius Weiß',
                        'domainAttributes' => ['firstName' => 'Kornelius', 'lastName' => 'Weiß'],
                    ],
                ],
            ],
        ],
    ]);

    $name = app(NameTagResolver::class)->moderatorFor($service);

    expect($name)->toBe('Kornelius Weiß');
});

test('name falls back to domainAttributes when person.title is absent', function () {
    $service = Service::factory()->create(['moderator_name' => null]);

    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Begrüßung',
        'is_before_event' => false,
        'sort_order' => 1,
        'responsible' => [
            'text' => '[Moderation]',
            'persons' => [
                [
                    'service' => '[Moderation]',
                    'accepted' => true,
                    'person' => [
                        'domainAttributes' => ['firstName' => 'Anna', 'lastName' => 'Müller'],
                    ],
                ],
            ],
        ],
    ]);

    $name = app(NameTagResolver::class)->moderatorFor($service);

    expect($name)->toBe('Anna Müller');
});

test('worship leader resolves from persons service role lobpreis in real cts shape', function () {
    $service = Service::factory()->create();

    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Begrüßung',
        'is_before_event' => false,
        'sort_order' => 1,
        'responsible' => [
            'text' => '[Moderation]',
            'persons' => [
                [
                    'service' => '[Moderation]',
                    'accepted' => true,
                    'person' => ['title' => 'Moderator Max'],
                ],
            ],
        ],
    ]);
    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Lobpreis',
        'is_before_event' => false,
        'sort_order' => 2,
        'responsible' => [
            'text' => '[Lobpreis]',
            'persons' => [
                [
                    'service' => '[Lobpreis]',
                    'accepted' => true,
                    'person' => ['title' => 'Kornelius Weiß'],
                ],
            ],
        ],
    ]);

    $name = app(NameTagResolver::class)->worshipLeaderFor($service);

    expect($name)->toBe('Kornelius Weiß');
});

test('worship leader returns null when no person has lobpreis role in real cts shape', function () {
    $service = Service::factory()->create();

    ServiceAgendaItem::factory()->create([
        'service_id' => $service->id,
        'title' => 'Lobpreis',
        'is_before_event' => false,
        'sort_order' => 1,
        'responsible' => [
            'text' => '[Moderation]',
            'persons' => [
                [
                    'service' => '[Moderation]',
                    'accepted' => true,
                    'person' => ['title' => 'Moderator Max'],
                ],
            ],
        ],
    ]);

    $name = app(NameTagResolver::class)->worshipLeaderFor($service);

    expect($name)->toBeNull();
});
