<?php

namespace Tests\Feature\Livewire;

use App\Livewire\Settings;
use App\Models\Setting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class SettingsTest extends TestCase
{
    use RefreshDatabase;

    public function test_mount_loads_feed_staleness_threshold(): void
    {
        Setting::setFeedStalenessThreshold(72);

        Livewire::test(Settings::class)
            ->assertSet('feedStalenessThreshold', 72);
    }

    public function test_mount_loads_default_feed_staleness_threshold(): void
    {
        Livewire::test(Settings::class)
            ->assertSet('feedStalenessThreshold', 48);
    }

    public function test_update_feed_staleness_threshold_saves_value(): void
    {
        Livewire::test(Settings::class)
            ->set('feedStalenessThreshold', 24)
            ->call('updateFeedStalenessThreshold')
            ->assertHasNoErrors();

        $this->assertSame(24, Setting::getFeedStalenessThreshold());
    }

    public function test_update_feed_staleness_threshold_validates_minimum(): void
    {
        Livewire::test(Settings::class)
            ->set('feedStalenessThreshold', -1)
            ->call('updateFeedStalenessThreshold')
            ->assertHasErrors(['feedStalenessThreshold' => 'min']);
    }

    public function test_update_feed_staleness_threshold_shows_success_message(): void
    {
        Livewire::test(Settings::class)
            ->set('feedStalenessThreshold', 24)
            ->call('updateFeedStalenessThreshold')
            ->assertSet('successMessage', 'Settings updated successfully!');
    }

    public function test_mount_loads_daily_publish_cap(): void
    {
        Setting::setDailyPublishCap(25);

        Livewire::test(Settings::class)
            ->assertSet('dailyPublishCap', 25);
    }

    public function test_mount_defaults_daily_publish_cap_to_unlimited(): void
    {
        Livewire::test(Settings::class)
            ->assertSet('dailyPublishCap', 0);
    }

    public function test_update_daily_publish_cap_saves_value(): void
    {
        Livewire::test(Settings::class)
            ->set('dailyPublishCap', 40)
            ->call('updateDailyPublishCap')
            ->assertHasNoErrors();

        $this->assertSame(40, Setting::getDailyPublishCap());
    }

    public function test_update_daily_publish_cap_validates_minimum(): void
    {
        Livewire::test(Settings::class)
            ->set('dailyPublishCap', -1)
            ->call('updateDailyPublishCap')
            ->assertHasErrors(['dailyPublishCap' => 'min']);
    }
}
