<?php

namespace Tests\Unit\Models;

use App\Models\Setting;
use Tests\TestCase;

class SettingTest extends TestCase
{
    /** @test */
    public function it_can_create_a_setting_with_factory(): void
    {
        $setting = Setting::factory()->create();

        $this->assertInstanceOf(Setting::class, $setting);
        $this->assertIsString($setting->app_name);
        $this->assertIsString($setting->app_version);
    }

    /** @test */
    public function it_has_correct_fillable_attributes(): void
    {
        $settingData = [
            'app_name' => 'ZEmailnator',
            'app_version' => '1.0.0',
            'app_base_url' => 'https://example.com',
            'app_admin' => 'admin@example.com',
            'app_title' => 'Test Title',
        ];

        $setting = Setting::query()->create($settingData);

        foreach ($settingData as $key => $value) {
            $this->assertEquals($value, $setting->$key);
        }
    }

    /** @test */
    public function it_stores_configuration_values(): void
    {
        $setting = Setting::factory()->create([
            'app_name' => 'Test App',
            'configuration_settings' => json_encode([
                'max_emails_per_user' => 100,
                'enable_registrations' => true,
                'default_language' => 'en',
            ]),
        ]);

        $this->assertEquals('Test App', $setting->app_name);
        $this->assertIsString($setting->configuration_settings);
        $config = json_decode($setting->configuration_settings, true);
        $this->assertEquals(100, $config['max_emails_per_user']);
    }

    /** @test */
    public function it_can_query_public_settings(): void
    {
        Setting::factory()->create(['app_name' => 'Public App']);
        Setting::factory()->create(['app_name' => 'Private App']);

        $allSettings = Setting::all();

        $this->assertCount(2, $allSettings);
        $this->assertIsString($allSettings->first()->app_name);
    }
}
