/**
 * 系统设置页
 *
 * 功能：
 * - 显示当前系统配置信息
 * - 提供系统配置的动态获取
 */

import { useState, useEffect } from 'react'
import { Col, Descriptions, Row, Space, Typography, Spin } from 'antd'
import { EnvironmentOutlined, SafetyOutlined, SettingOutlined } from '@ant-design/icons'
import { PageLayout, ContentCard } from '@/components/layout'
import { PageHeader } from '@/components/common'
import { getSettings, type SystemSettings } from '@/services/settings'

const { Text } = Typography

export function SettingsPage() {
  const [loading, setLoading] = useState(true)
  const [settings, setSettings] = useState<SystemSettings | null>(null)
  const [error, setError] = useState<string | null>(null)

  useEffect(() => {
    const fetchSettings = async () => {
      try {
        setLoading(true)
        setError(null)
        const data = await getSettings()
        setSettings(data)
      } catch (err) {
        setError(err instanceof Error ? err.message : '获取设置失败')
      } finally {
        setLoading(false)
      }
    }

    void fetchSettings()
  }, [])

  if (loading) {
    return (
      <PageLayout>
        <PageHeader
          title="系统设置"
          description="查看当前系统配置和功能开关状态"
        />
        <div style={{ textAlign: 'center', padding: '50px' }}>
          <Spin />
        </div>
      </PageLayout>
    )
  }

  if (error || !settings) {
    return (
      <PageLayout>
        <PageHeader
          title="系统设置"
          description="查看当前系统配置和功能开关状态"
          actions={
            <Space>
              <SettingOutlined />
              <Text type="secondary">加载失败</Text>
            </Space>
          }
        />
        <ContentCard>
          <Text type="danger">{error || '无法加载系统设置'}</Text>
        </ContentCard>
      </PageLayout>
    )
  }

  return (
    <PageLayout>
      <PageHeader
        title="系统设置"
        description="查看当前系统配置和功能开关状态"
        actions={
          <Space>
            <SettingOutlined />
            <Text type="secondary">配置更新请联系管理员</Text>
          </Space>
        }
      />

      <Row gutter={24}>
        <Col xs={24} lg={12}>
          <ContentCard
            title={
              <Space>
                <SafetyOutlined />
                安全设置
              </Space>
            }
          >
            <Descriptions column={1} bordered size="small">
              <Descriptions.Item label="密码最小长度">
                {settings.security.password_min_length} 位
              </Descriptions.Item>
              <Descriptions.Item label="密码必须包含大写字母">
                {settings.security.password_require_uppercase ? '是' : '否'}
              </Descriptions.Item>
              <Descriptions.Item label="密码必须包含小写字母">
                {settings.security.password_require_lowercase ? '是' : '否'}
              </Descriptions.Item>
              <Descriptions.Item label="密码必须包含数字">
                {settings.security.password_require_numbers ? '是' : '否'}
              </Descriptions.Item>
              <Descriptions.Item label="密码必须包含特殊字符">
                {settings.security.password_require_symbols ? '是' : '否'}
              </Descriptions.Item>
              <Descriptions.Item label="密码历史记录">
                最近 {settings.security.password_history} 次
              </Descriptions.Item>
              <Descriptions.Item label="TOTP 两步验证">
                {settings.security.totp_enabled ? '已启用' : '未启用'}
              </Descriptions.Item>
              <Descriptions.Item label="登录失败锁定">
                {settings.security.login_fail_lock
                  ? `锁定 ${settings.security.login_fail_threshold} 次后锁定 ${settings.security.login_fail_duration} 分钟`
                  : '未启用'}
              </Descriptions.Item>
              <Descriptions.Item label="会话超时">
                {settings.security.session_timeout / 86400} 天
              </Descriptions.Item>
              <Descriptions.Item label="设备信任有效期">
                {settings.security.device_trust_duration / 86400} 天
              </Descriptions.Item>
            </Descriptions>
          </ContentCard>
        </Col>

        <Col xs={24} lg={12}>
          <ContentCard
            title={
              <Space>
                <SettingOutlined />
                功能开关
              </Space>
            }
          >
            <Descriptions column={1} bordered size="small">
              <Descriptions.Item label="邮箱验证">
                {settings.features.email_verification ? '已启用' : '未启用'}
              </Descriptions.Item>
              <Descriptions.Item label="手机验证">
                {settings.features.phone_verification ? '已启用' : '未启用'}
              </Descriptions.Item>
              <Descriptions.Item label="OAuth 提供商">
                {settings.features.oauth_providers?.join(', ') || '无'}
              </Descriptions.Item>
              <Descriptions.Item label="SSO 单点登录">
                {settings.features.sso_enabled ? '已启用' : '未启用'}
              </Descriptions.Item>
              <Descriptions.Item label="操作日志">
                {settings.features.operation_log_enabled ? '已启用' : '未启用'}
              </Descriptions.Item>
              <Descriptions.Item label="登录日志">
                {settings.features.login_log_enabled ? '已启用' : '未启用'}
              </Descriptions.Item>
              <Descriptions.Item label="数据导出">
                {settings.features.data_export_enabled ? '已启用' : '未启用'}
              </Descriptions.Item>
              <Descriptions.Item label="数据导入">
                {settings.features.data_import_enabled ? '已启用' : '未启用'}
              </Descriptions.Item>
            </Descriptions>
          </ContentCard>
        </Col>
      </Row>

      <Row gutter={24} style={{ marginTop: 24 }}>
        <Col xs={24}>
          <ContentCard
            title={
              <Space>
                <EnvironmentOutlined />
                系统信息
              </Space>
            }
          >
            <Descriptions column={1} bordered size="small">
              <Descriptions.Item label="系统名称">
                {settings.system.name}
              </Descriptions.Item>
              <Descriptions.Item label="版本号">
                {settings.system.version}
              </Descriptions.Item>
              <Descriptions.Item label="运行环境">
                {settings.system.environment}
              </Descriptions.Item>
              <Descriptions.Item label="系统描述">
                {settings.system.description}
              </Descriptions.Item>
            </Descriptions>
          </ContentCard>
        </Col>
      </Row>
    </PageLayout>
  )
}
