<?php

use App\Models\Setting;
use App\Models\User;

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

test('settings index includes all agenda keys in props', function () {
    $response = $this->actingAs($this->user)
        ->withoutVite()
        ->get(route('settings.index'));

    $response->assertInertia(
        fn ($page) => $page
            ->component('Settings')
            ->has('settings.agenda_start_title')
            ->has('settings.agenda_end_title')
            ->has('settings.agenda_announcement_position')
            ->has('settings.agenda_sermon_matching')
    );
});

test('patch agenda_start_title setting returns 200', function () {
    $response = $this->actingAs($this->user)
        ->patchJson(route('settings.update'), [
            'key' => 'agenda_start_title',
            'value' => 'Test Title',
        ]);

    $response->assertOk()
        ->assertJson(['success' => true]);

    expect(Setting::get('agenda_start_title'))->toBe('Test Title');
});

test('patch agenda_end_title setting returns 200', function () {
    $response = $this->actingAs($this->user)
        ->patchJson(route('settings.update'), [
            'key' => 'agenda_end_title',
            'value' => 'End Title',
        ]);

    $response->assertOk()
        ->assertJson(['success' => true]);

    expect(Setting::get('agenda_end_title'))->toBe('End Title');
});

test('patch agenda_announcement_position setting returns 200', function () {
    $response = $this->actingAs($this->user)
        ->patchJson(route('settings.update'), [
            'key' => 'agenda_announcement_position',
            'value' => 'pattern1,pattern2',
        ]);

    $response->assertOk()
        ->assertJson(['success' => true]);

    expect(Setting::get('agenda_announcement_position'))->toBe('pattern1,pattern2');
});

test('patch agenda_sermon_matching setting returns 200', function () {
    $response = $this->actingAs($this->user)
        ->patchJson(route('settings.update'), [
            'key' => 'agenda_sermon_matching',
            'value' => 'sermon,predigt',
        ]);

    $response->assertOk()
        ->assertJson(['success' => true]);

    expect(Setting::get('agenda_sermon_matching'))->toBe('sermon,predigt');
});

test('patch with unknown key returns 422', function () {
    $response = $this->actingAs($this->user)
        ->patchJson(route('settings.update'), [
            'key' => 'unknown_setting_key',
            'value' => 'some value',
        ]);

    $response->assertUnprocessable();
});
