<?php

namespace Tests\Feature;

use App\Models\SystemSetting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SystemSettingsTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        
        // 创建测试用户
        $this->actingAs(User::factory()->create());
    }

    /** @test */
    public function it_has_all_required_system_global_settings()
    {
        // 创建系统全局配置
        $requiredSettings = [
            'system.name' => ['name' => '知识库管理系统'],
            'system.timeout' => ['timeout' => 60],
            'system.max_retries' => ['max_retries' => 3],
            'system.max_upload_size' => ['max_upload_size' => 10485760],
            'system.allowed_file_types' => ['allowed_file_types' => ['pdf', 'docx', 'txt', 'md']],
        ];

        foreach ($requiredSettings as $key => $value) {
            SystemSetting::create([
                'key' => $key,
                'value' => $value,
                'group' => 'system',
                'description' => '测试配置',
            ]);
        }

        // 验证所有必需的系统全局配置都存在
        $this->assertEquals(5, SystemSetting::where('group', 'system')->count());
        
        // 验证每个配置都能正确读取
        $this->assertNotNull(SystemSetting::where('key', 'system.name')->first());
        $this->assertNotNull(SystemSetting::where('key', 'system.timeout')->first());
        $this->assertNotNull(SystemSetting::where('key', 'system.max_retries')->first());
        $this->assertNotNull(SystemSetting::where('key', 'system.max_upload_size')->first());
        $this->assertNotNull(SystemSetting::where('key', 'system.allowed_file_types')->first());
    }

    /** @test */
    public function it_can_read_system_settings_values()
    {
        // 创建测试配置
        SystemSetting::create([
            'key' => 'system.name',
            'value' => ['name' => '测试系统'],
            'group' => 'system',
            'description' => '系统名称',
        ]);

        SystemSetting::create([
            'key' => 'system.timeout',
            'value' => ['timeout' => 120],
            'group' => 'system',
            'description' => '超时时间',
        ]);

        // 验证能正确读取配置值
        $nameSetting = SystemSetting::where('key', 'system.name')->first();
        $this->assertEquals('测试系统', $nameSetting->value['name']);

        $timeoutSetting = SystemSetting::where('key', 'system.timeout')->first();
        $this->assertEquals(120, $timeoutSetting->value['timeout']);
    }

    /** @test */
    public function it_validates_system_settings_structure()
    {
        // 验证系统全局配置的结构
        $settings = [
            'system.name' => 'string',
            'system.timeout' => 'integer',
            'system.max_retries' => 'integer',
            'system.max_upload_size' => 'integer',
            'system.allowed_file_types' => 'array',
        ];

        foreach ($settings as $key => $expectedType) {
            $valueKey = explode('.', $key)[1];
            
            SystemSetting::create([
                'key' => $key,
                'value' => [$valueKey => $this->getTestValue($expectedType)],
                'group' => 'system',
                'description' => '测试',
            ]);

            $setting = SystemSetting::where('key', $key)->first();
            $actualValue = $setting->value[$valueKey];
            
            $this->assertEquals(
                $expectedType,
                gettype($actualValue),
                "配置 {$key} 的类型应该是 {$expectedType}"
            );
        }
    }

    private function getTestValue(string $type)
    {
        return match($type) {
            'string' => '测试值',
            'integer' => 100,
            'array' => ['test'],
            default => null,
        };
    }
}
