<?php

namespace Database\Seeders;

use App\Models\SystemSetting;
use Illuminate\Database\Seeder;

class SystemSettingSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->command->info('开始创建系统设置...');

        // 1. 嵌入模型配置
        $this->command->info('创建嵌入模型配置...');
        SystemSetting::create([
            'key' => 'embedding.model_name',
            'value' => [
                'model_name' => 'text-embedding-3-small',
            ],
            'group' => 'embedding',
            'description' => '嵌入模型名称',
            'is_public' => false,
        ]);

        SystemSetting::create([
            'key' => 'embedding.api_key',
            'value' => [
                'api_key' => '',
            ],
            'group' => 'embedding',
            'description' => 'OpenAI API 密钥（敏感信息）',
            'is_public' => false,
        ]);

        SystemSetting::create([
            'key' => 'embedding.endpoint_url',
            'value' => [
                'endpoint_url' => 'https://api.openai.com/v1/embeddings',
            ],
            'group' => 'embedding',
            'description' => '嵌入模型 API 端点 URL',
            'is_public' => false,
        ]);

        SystemSetting::create([
            'key' => 'embedding.dimensions',
            'value' => [
                'dimensions' => 1536,
            ],
            'group' => 'embedding',
            'description' => '嵌入向量维度',
            'is_public' => false,
        ]);

        SystemSetting::create([
            'key' => 'embedding.batch_size',
            'value' => [
                'batch_size' => 100,
            ],
            'group' => 'embedding',
            'description' => '批量处理大小',
            'is_public' => false,
        ]);

        // 2. 分块参数配置
        $this->command->info('创建分块参数配置...');
        SystemSetting::create([
            'key' => 'chunking.chunk_size',
            'value' => [
                'chunk_size' => 1000,
            ],
            'group' => 'chunking',
            'description' => '文档分块大小（字符数）',
            'is_public' => true,
        ]);

        SystemSetting::create([
            'key' => 'chunking.chunk_overlap',
            'value' => [
                'chunk_overlap' => 200,
            ],
            'group' => 'chunking',
            'description' => '分块重叠大小（字符数）',
            'is_public' => true,
        ]);

        SystemSetting::create([
            'key' => 'chunking.separator',
            'value' => [
                'separator' => "\n\n",
            ],
            'group' => 'chunking',
            'description' => '分块分隔符',
            'is_public' => true,
        ]);

        SystemSetting::create([
            'key' => 'chunking.min_chunk_size',
            'value' => [
                'min_chunk_size' => 100,
            ],
            'group' => 'chunking',
            'description' => '最小分块大小（字符数）',
            'is_public' => true,
        ]);

        // 3. 系统全局配置
        $this->command->info('创建系统全局配置...');
        SystemSetting::create([
            'key' => 'system.name',
            'value' => [
                'name' => '知识库管理系统',
            ],
            'group' => 'system',
            'description' => '系统名称',
            'is_public' => true,
        ]);

        SystemSetting::create([
            'key' => 'system.timeout',
            'value' => [
                'timeout' => 60,
            ],
            'group' => 'system',
            'description' => 'API 请求超时时间（秒）',
            'is_public' => false,
        ]);

        SystemSetting::create([
            'key' => 'system.max_retries',
            'value' => [
                'max_retries' => 3,
            ],
            'group' => 'system',
            'description' => 'API 请求最大重试次数',
            'is_public' => false,
        ]);

        SystemSetting::create([
            'key' => 'system.max_upload_size',
            'value' => [
                'max_upload_size' => 10485760, // 10MB
            ],
            'group' => 'system',
            'description' => '最大文件上传大小（字节）',
            'is_public' => true,
        ]);

        SystemSetting::create([
            'key' => 'system.allowed_file_types',
            'value' => [
                'allowed_file_types' => ['docx', 'pdf', 'txt', 'md'],
            ],
            'group' => 'system',
            'description' => '允许上传的文件类型',
            'is_public' => true,
        ]);

        // 4. 搜索配置
        $this->command->info('创建搜索配置...');
        SystemSetting::create([
            'key' => 'search.top_k',
            'value' => [
                'top_k' => 5,
            ],
            'group' => 'search',
            'description' => '搜索返回的最大结果数',
            'is_public' => true,
        ]);

        SystemSetting::create([
            'key' => 'search.similarity_threshold',
            'value' => [
                'similarity_threshold' => 0.7,
            ],
            'group' => 'search',
            'description' => '相似度阈值（0-1）',
            'is_public' => true,
        ]);

        SystemSetting::create([
            'key' => 'search.enable_rerank',
            'value' => [
                'enable_rerank' => false,
            ],
            'group' => 'search',
            'description' => '是否启用重排序',
            'is_public' => false,
        ]);

        $this->command->info('系统设置创建完成！');
        $this->command->newLine();
        $this->command->info('=== 生成的配置摘要 ===');
        $this->command->info('总配置数量: ' . SystemSetting::count());
        $this->command->info('  - 嵌入模型配置: ' . SystemSetting::where('group', 'embedding')->count());
        $this->command->info('  - 分块参数配置: ' . SystemSetting::where('group', 'chunking')->count());
        $this->command->info('  - 系统全局配置: ' . SystemSetting::where('group', 'system')->count());
        $this->command->info('  - 搜索配置: ' . SystemSetting::where('group', 'search')->count());
        $this->command->info('  - 公开配置: ' . SystemSetting::where('is_public', true)->count());
        $this->command->info('  - 私有配置: ' . SystemSetting::where('is_public', false)->count());
    }
}
