<?php

namespace Database\Seeders;

use App\Models\ConfigGroup;
use App\Models\ConfigItem;
use Illuminate\Database\Seeder;

class ConfigItemSeeder extends Seeder
{
    public function run(): void
    {
        $groupMap = ConfigGroup::whereIn('code', ['basic', 'upload', 'security'])
            ->pluck('id', 'code');

        $items = [
            ['group_code' => 'basic', 'name' => '站点名称', 'code' => 'site_name', 'value' => 'CmsPro', 'type' => 'text', 'options' => null, 'tips' => null, 'sort' => 0],
            ['group_code' => 'basic', 'name' => '站点描述', 'code' => 'site_description', 'value' => 'CmsPro 内容管理系统', 'type' => 'text', 'options' => null, 'tips' => null, 'sort' => 1],
            ['group_code' => 'basic', 'name' => '站点关键词', 'code' => 'site_keywords', 'value' => 'CMS,内容管理', 'type' => 'text', 'options' => null, 'tips' => null, 'sort' => 2],
            ['group_code' => 'basic', 'name' => '站点关闭', 'code' => 'site_closed', 'value' => '0', 'type' => 'switch', 'options' => null, 'tips' => '站点前端关闭，不影响后台操作。', 'sort' => 3],
            ['group_code' => 'basic', 'name' => '非WWW跳转', 'code' => 'force_www_redirect', 'value' => '0', 'type' => 'switch', 'options' => null, 'tips' => '开启后，未绑定应用的子域名与根域名将自动301跳转到www域名下，已绑定应用的子域名不受影响。', 'sort' => 4],
            ['group_code' => 'basic', 'name' => '站点Logo', 'code' => 'site_logo', 'value' => '/Admin/images/logo.png', 'type' => 'image', 'options' => null, 'tips' => '建议尺寸：200x50像素', 'sort' => 5],
            ['group_code' => 'basic', 'name' => '版权信息', 'code' => 'site_copyright', 'value' => 'Copyright © 2026 CmsPro. All Rights Reserved.', 'type' => 'textarea', 'options' => null, 'tips' => '显示在页面底部', 'sort' => 6],
            ['group_code' => 'basic', 'name' => '备案号', 'code' => 'site_icp', 'value' => '', 'type' => 'text', 'options' => null, 'tips' => 'ICP备案号，留空则不显示', 'sort' => 7],
            ['group_code' => 'basic', 'name' => '统计代码', 'code' => 'site_analytics', 'value' => '', 'type' => 'textarea', 'options' => null, 'tips' => '第三方统计代码（如百度统计、Google Analytics等），将自动嵌入前台页面', 'sort' => 8],
            ['group_code' => 'basic', 'name' => '默认时区', 'code' => 'default_timezone', 'value' => 'Asia/Shanghai', 'type' => 'select', 'options' => [
                ['label' => 'Asia/Shanghai (北京时间)', 'value' => 'Asia/Shanghai'],
                ['label' => 'Asia/Hong_Kong (香港时间)', 'value' => 'Asia/Hong_Kong'],
                ['label' => 'Asia/Taipei (台北时间)', 'value' => 'Asia/Taipei'],
                ['label' => 'Asia/Tokyo (东京时间)', 'value' => 'Asia/Tokyo'],
                ['label' => 'Asia/Seoul (首尔时间)', 'value' => 'Asia/Seoul'],
                ['label' => 'Asia/Singapore (新加坡时间)', 'value' => 'Asia/Singapore'],
                ['label' => 'America/New_York (纽约时间)', 'value' => 'America/New_York'],
                ['label' => 'America/Los_Angeles (洛杉矶时间)', 'value' => 'America/Los_Angeles'],
                ['label' => 'Europe/London (伦敦时间)', 'value' => 'Europe/London'],
                ['label' => 'Europe/Paris (巴黎时间)', 'value' => 'Europe/Paris'],
                ['label' => 'UTC (协调世界时)', 'value' => 'UTC'],
            ], 'tips' => '系统默认时区设置', 'sort' => 8],
            ['group_code' => 'upload', 'name' => '图片大小限制(KB)', 'code' => 'upload_image_max_size', 'value' => '5120', 'type' => 'number', 'options' => null, 'tips' => null, 'sort' => 1],
            ['group_code' => 'upload', 'name' => '图片允许扩展名', 'code' => 'upload_image_extensions', 'value' => 'jpg,jpeg,png,gif,bmp,webp', 'type' => 'text', 'options' => null, 'tips' => '多个扩展名用英文逗号分隔', 'sort' => 2],
            ['group_code' => 'upload', 'name' => '文件大小限制(KB)', 'code' => 'upload_file_max_size', 'value' => '20480', 'type' => 'number', 'options' => null, 'tips' => null, 'sort' => 3],
            ['group_code' => 'upload', 'name' => '文件允许扩展名', 'code' => 'upload_file_extensions', 'value' => 'pdf,doc,docx,xls,xlsx,ppt,pptx,zip,rar', 'type' => 'text', 'options' => null, 'tips' => '多个扩展名用英文逗号分隔', 'sort' => 4],
            ['group_code' => 'upload', 'name' => '视频大小限制(KB)', 'code' => 'upload_video_max_size', 'value' => '102400', 'type' => 'number', 'options' => null, 'tips' => '0表示不限制', 'sort' => 5],
            ['group_code' => 'upload', 'name' => '视频允许扩展名', 'code' => 'upload_video_extensions', 'value' => 'mp4,mp3', 'type' => 'text', 'options' => null, 'tips' => '多个扩展名用英文逗号分隔', 'sort' => 6],
            ['group_code' => 'upload', 'name' => '哈希去重', 'code' => 'upload_hash_check', 'value' => '1', 'type' => 'switch', 'options' => null, 'tips' => '开启后相同内容的文件不会重复上传', 'sort' => 7],
            ['group_code' => 'security', 'name' => '登录验证码', 'code' => 'login_captcha', 'value' => '1', 'type' => 'switch', 'options' => null, 'tips' => '开启后登录时需要输入验证码', 'sort' => 0],
            ['group_code' => 'security', 'name' => '登录失败锁定次数', 'code' => 'login_lock_attempts', 'value' => '5', 'type' => 'number', 'options' => null, 'tips' => '连续登录失败达到该次数后锁定账号', 'sort' => 1],
            ['group_code' => 'security', 'name' => '锁定时长(分钟)', 'code' => 'login_lock_duration', 'value' => '30', 'type' => 'number', 'options' => null, 'tips' => '账号锁定后的持续时间', 'sort' => 2],
            ['group_code' => 'security', 'name' => '会话超时(分钟)', 'code' => 'session_lifetime', 'value' => '0', 'type' => 'select', 'options' => [
                ['label' => '30分钟', 'value' => '30'],
                ['label' => '60分钟', 'value' => '60'],
                ['label' => '2小时', 'value' => '120'],
                ['label' => '4小时', 'value' => '240'],
                ['label' => '8小时', 'value' => '480'],
                ['label' => '永不超时', 'value' => '0'],
            ], 'tips' => '无操作超过该时间后自动退出登录', 'sort' => 3],
        ];

        foreach ($items as $item) {
            $groupCode = $item['group_code'];
            unset($item['group_code']);

            $groupId = $groupMap[$groupCode] ?? null;
            if (!$groupId) {
                continue;
            }

            ConfigItem::updateOrCreate(
                ['code' => $item['code']],
                array_merge($item, [
                    'group_id' => $groupId,
                    'status' => 1,
                ])
            );
        }
    }
}
