<?php

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

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

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

    $response->assertInertia(
        fn ($page) => $page
            ->component('Settings')
            ->has('settings.add_countdown_slide')
    );
});

test('patch add_countdown_slide to 1 persists', function () {
    $response = $this->actingAs($this->user)
        ->patchJson(route('settings.update'), [
            'key' => 'add_countdown_slide',
            'value' => '1',
        ]);

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

    expect(Setting::get('add_countdown_slide'))->toBe('1');
});

test('patch add_countdown_slide to 0 persists', function () {
    Setting::set('add_countdown_slide', '1');

    $response = $this->actingAs($this->user)
        ->patchJson(route('settings.update'), [
            'key' => 'add_countdown_slide',
            'value' => '0',
        ]);

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

    expect(Setting::get('add_countdown_slide'))->toBe('0');
});

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