import { useState, useEffect, useCallback } from 'react'
import {
    Globe, Plus, Trash2, ToggleLeft, ToggleRight,
    Loader2, Save, RefreshCcw, CheckCircle, XCircle,
    AlertTriangle, Clock, MapPin, Play
} from 'lucide-react'
import { Card, CardHeader, CardTitle, CardContent, Button, Input } from '../components/common'

interface CodexProxy {
    id: number
    proxy_url: string
    description: string
    is_enabled: boolean
    last_used_at: string | null
    success_count: number
    fail_count: number
    location: string
    last_test_at: string | null
    created_at: string
}

interface ProxyStats {
    total: number
    enabled: number
    disabled: number
}

export default function CodexProxyConfig() {
    const [proxies, setProxies] = useState<CodexProxy[]>([])
    const [stats, setStats] = useState<ProxyStats>({ total: 0, enabled: 0, disabled: 0 })
    const [loading, setLoading] = useState(true)
    const [saving, setSaving] = useState(false)
    const [testingIds, setTestingIds] = useState<Set<number>>(new Set())
    const [message, setMessage] = useState<{ type: 'success' | 'error', text: string } | null>(null)

    // 单个添加
    const [newProxyUrl, setNewProxyUrl] = useState('')
    const [newDescription, setNewDescription] = useState('')

    // 批量添加
    const [batchMode, setBatchMode] = useState(false)
    const [batchInput, setBatchInput] = useState('')

    // 获取代理列表
    const fetchProxies = useCallback(async () => {
        setLoading(true)
        try {
            const res = await fetch('/api/codex-proxy')
            const data = await res.json()
            if (data.code === 0 && data.data) {
                setProxies(data.data.proxies || [])
                setStats(data.data.stats || { total: 0, enabled: 0, disabled: 0 })
            }
        } catch (error) {
            console.error('获取代理列表失败:', error)
        } finally {
            setLoading(false)
        }
    }, [])

    useEffect(() => {
        fetchProxies()
    }, [fetchProxies])

    // 添加代理
    const handleAddProxy = async () => {
        if (!newProxyUrl.trim()) return
        setSaving(true)
        setMessage(null)
        try {
            const res = await fetch('/api/codex-proxy', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    proxy_url: newProxyUrl.trim(),
                    description: newDescription.trim(),
                }),
            })
            const data = await res.json()
            if (data.code === 0) {
                setMessage({ type: 'success', text: '代理添加成功' })
                setNewProxyUrl('')
                setNewDescription('')
                fetchProxies()
            } else {
                setMessage({ type: 'error', text: data.message || '添加失败' })
            }
        } catch {
            setMessage({ type: 'error', text: '网络错误' })
        } finally {
            setSaving(false)
        }
    }

    // 批量添加
    const handleBatchAdd = async () => {
        const lines = batchInput.split('\n').filter(line => line.trim())
        if (lines.length === 0) return
        setSaving(true)
        setMessage(null)
        try {
            const res = await fetch('/api/codex-proxy', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ proxies: lines }),
            })
            const data = await res.json()
            if (data.code === 0) {
                setMessage({ type: 'success', text: `成功添加 ${data.data.added}/${data.data.total} 个代理` })
                setBatchInput('')
                fetchProxies()
            } else {
                setMessage({ type: 'error', text: data.message || '添加失败' })
            }
        } catch {
            setMessage({ type: 'error', text: '网络错误' })
        } finally {
            setSaving(false)
        }
    }

    // 切换启用状态
    const handleToggle = async (id: number) => {
        try {
            const res = await fetch(`/api/codex-proxy?id=${id}`, { method: 'PUT' })
            const data = await res.json()
            if (data.code === 0) {
                fetchProxies()
            }
        } catch (error) {
            console.error('切换状态失败:', error)
        }
    }

    // 删除代理
    const handleDelete = async (id: number) => {
        if (!confirm('确定要删除这个代理吗？')) return
        try {
            const res = await fetch(`/api/codex-proxy?id=${id}`, { method: 'DELETE' })
            const data = await res.json()
            if (data.code === 0) {
                fetchProxies()
            }
        } catch (error) {
            console.error('删除失败:', error)
        }
    }

    // 测试代理
    const handleTestProxy = async (id: number) => {
        if (testingIds.has(id)) return
        setTestingIds(prev => new Set(prev).add(id))
        try {
            const res = await fetch(`/api/codex-proxy/test?id=${id}`, { method: 'POST' })
            const data = await res.json()
            if (data.code === 0) {
                // 局部更新代理信息
                setProxies(prev => prev.map(p => 
                    p.id === id 
                        ? { ...p, location: data.data.location, last_test_at: new Date().toISOString(), success_count: p.success_count + 1 } 
                        : p
                ))
            } else {
                alert(`测试失败: ${data.message}`)
                // 虽然失败也需要刷新列表以获取最新的统计数据
                fetchProxies()
            }
        } catch (error) {
            console.error('测试代理出错:', error)
            alert('网络错误，测试失败')
        } finally {
            setTestingIds(prev => {
                const next = new Set(prev)
                next.delete(id)
                return next
            })
        }
    }

    // 清空所有
    const handleClearAll = async () => {
        if (!confirm('确定要清空所有代理吗？此操作不可恢复！')) return
        try {
            const res = await fetch('/api/codex-proxy?all=true', { method: 'DELETE' })
            const data = await res.json()
            if (data.code === 0) {
                setMessage({ type: 'success', text: '已清空所有代理' })
                fetchProxies()
            }
        } catch (error) {
            console.error('清空失败:', error)
        }
    }

    // 格式化代理显示
    const formatProxyDisplay = (url: string) => {
        if (url.includes('@')) {
            const parts = url.split('@')
            return parts[parts.length - 1]
        }
        return url.replace(/^https?:\/\//, '').replace(/^socks5:\/\//, '')
    }

    // 格式化时间
    const formatTime = (timeStr: string | null) => {
        if (!timeStr) return '从未'
        const date = new Date(timeStr)
        return date.toLocaleString('zh-CN', {
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit'
        })
    }

    // 计算成功率
    const getSuccessRate = (proxy: CodexProxy) => {
        const total = proxy.success_count + proxy.fail_count
        if (total === 0) return null
        return Math.round((proxy.success_count / total) * 100)
    }

    if (loading) {
        return (
            <div className="flex items-center justify-center h-64">
                <Loader2 className="h-8 w-8 animate-spin text-blue-500" />
            </div>
        )
    }

    return (
        <div className="space-y-6">
            {/* Header */}
            <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
                <div>
                    <h1 className="text-2xl font-bold text-slate-900 dark:text-slate-100 flex items-center gap-2">
                        <Globe className="h-7 w-7 text-purple-500" />
                        CodexAuth 代理池
                    </h1>
                    <p className="text-sm text-slate-500 dark:text-slate-400">
                        管理 CodexAuth API 授权使用的代理池，支持归属地自动识别
                    </p>
                </div>
                <div className="flex gap-2">
                    <Button
                        variant="outline"
                        onClick={fetchProxies}
                        icon={<RefreshCcw className="h-4 w-4" />}
                    >
                        刷新
                    </Button>
                    {proxies.length > 0 && (
                        <Button
                            variant="danger"
                            onClick={handleClearAll}
                            icon={<Trash2 className="h-4 w-4" />}
                        >
                            清空
                        </Button>
                    )}
                </div>
            </div>

            {/* Message */}
            {message && (
                <div className={`p-3 rounded-lg text-sm ${message.type === 'success'
                    ? 'bg-green-50 text-green-700 dark:bg-green-900/30 dark:text-green-400'
                    : 'bg-red-50 text-red-700 dark:bg-red-900/30 dark:text-red-400'
                    }`}>
                    {message.text}
                </div>
            )}

            {/* Stats */}
            <div className="grid grid-cols-3 gap-4">
                <Card>
                    <CardContent className="py-4">
                        <div className="text-center">
                            <div className="text-2xl font-bold text-slate-900 dark:text-slate-100">
                                {stats.total}
                            </div>
                            <div className="text-xs text-slate-500 dark:text-slate-400">
                                总数
                            </div>
                        </div>
                    </CardContent>
                </Card>
                <Card>
                    <CardContent className="py-4">
                        <div className="text-center">
                            <div className="text-2xl font-bold text-green-600 dark:text-green-400">
                                {stats.enabled}
                            </div>
                            <div className="text-xs text-slate-500 dark:text-slate-400">
                                已启用
                            </div>
                        </div>
                    </CardContent>
                </Card>
                <Card>
                    <CardContent className="py-4">
                        <div className="text-center">
                            <div className="text-2xl font-bold text-slate-400">
                                {stats.disabled}
                            </div>
                            <div className="text-xs text-slate-500 dark:text-slate-400">
                                已禁用
                            </div>
                        </div>
                    </CardContent>
                </Card>
            </div>

            {/* Add Proxy */}
            <Card>
                <CardHeader>
                    <CardTitle className="flex items-center gap-2">
                        <Plus className="h-5 w-5 text-blue-500" />
                        添加代理
                    </CardTitle>
                    <button
                        onClick={() => setBatchMode(!batchMode)}
                        className="text-sm text-blue-600 hover:text-blue-700 dark:text-blue-400"
                    >
                        {batchMode ? '单个添加' : '批量添加'}
                    </button>
                </CardHeader>
                <CardContent className="space-y-4">
                    {batchMode ? (
                        <>
                            <textarea
                                value={batchInput}
                                onChange={(e) => setBatchInput(e.target.value)}
                                placeholder="每行一个代理地址，格式：&#10;http://user:pass@host:port&#10;http://host:port&#10;socks5://host:port"
                                rows={6}
                                className="w-full px-3 py-2 border border-slate-300 dark:border-slate-600 rounded-lg bg-white dark:bg-slate-800 text-slate-900 dark:text-slate-100 placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm font-mono"
                            />
                            <Button
                                onClick={handleBatchAdd}
                                disabled={saving || !batchInput.trim()}
                                icon={saving ? <Loader2 className="h-4 w-4 animate-spin" /> : <Save className="h-4 w-4" />}
                            >
                                {saving ? '添加中...' : '批量添加'}
                            </Button>
                        </>
                    ) : (
                        <>
                            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                                <Input
                                    label="代理地址"
                                    value={newProxyUrl}
                                    onChange={(e) => setNewProxyUrl(e.target.value)}
                                    placeholder="http://user:pass@host:port"
                                    onKeyDown={(e) => e.key === 'Enter' && handleAddProxy()}
                                />
                                <Input
                                    label="备注（可选）"
                                    value={newDescription}
                                    onChange={(e) => setNewDescription(e.target.value)}
                                    placeholder="代理描述"
                                />
                            </div>
                            <Button
                                onClick={handleAddProxy}
                                disabled={saving || !newProxyUrl.trim()}
                                icon={saving ? <Loader2 className="h-4 w-4 animate-spin" /> : <Plus className="h-4 w-4" />}
                            >
                                {saving ? '添加中...' : '添加代理'}
                            </Button>
                        </>
                    )}
                </CardContent>
            </Card>

            {/* Proxy List */}
            <Card>
                <CardHeader>
                    <CardTitle>代理列表</CardTitle>
                    <span className="text-sm text-slate-500">
                        共 {proxies.length} 个代理
                    </span>
                </CardHeader>
                <CardContent>
                    {proxies.length === 0 ? (
                        <div className="text-center py-8 text-slate-500 dark:text-slate-400">
                            <Globe className="h-12 w-12 mx-auto mb-3 opacity-50" />
                            <p>暂无代理</p>
                            <p className="text-sm">添加代理后，CodexAuth 授权时将随机选择使用</p>
                        </div>
                    ) : (
                        <div className="space-y-3">
                            {proxies.map((proxy) => {
                                const successRate = getSuccessRate(proxy)
                                const istesting = testingIds.has(proxy.id)
                                
                                return (
                                    <div
                                        key={proxy.id}
                                        className={`p-4 rounded-lg border transition-all ${proxy.is_enabled
                                            ? 'bg-white dark:bg-slate-800 border-slate-200 dark:border-slate-700'
                                            : 'bg-slate-50 dark:bg-slate-800/50 border-slate-200 dark:border-slate-700 opacity-60'
                                            }`}
                                    >
                                        <div className="flex items-center justify-between gap-4">
                                            <div className="flex-1 min-w-0">
                                                <div className="flex items-center gap-2">
                                                    <span className="font-mono text-sm text-slate-900 dark:text-slate-100 truncate">
                                                        {formatProxyDisplay(proxy.proxy_url)}
                                                    </span>
                                                    {proxy.location && (
                                                        <span className="px-1.5 py-0.5 rounded bg-blue-100 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400 text-[10px] font-bold flex items-center gap-0.5">
                                                            <MapPin className="h-2.5 w-2.5" />
                                                            {proxy.location}
                                                        </span>
                                                    )}
                                                    {proxy.description && (
                                                        <span className="text-xs text-slate-500 dark:text-slate-400">
                                                            ({proxy.description})
                                                        </span>
                                                    )}
                                                </div>
                                                <div className="flex flex-wrap items-center gap-x-4 gap-y-2 mt-1 text-xs text-slate-500 dark:text-slate-400">
                                                    <span className="flex items-center gap-1" title="最后测试时间">
                                                        <Clock className="h-3 w-3" />
                                                        测试: {formatTime(proxy.last_test_at)}
                                                    </span>
                                                    <span className="flex items-center gap-1">
                                                        <CheckCircle className="h-3 w-3 text-green-500" />
                                                        {proxy.success_count}
                                                    </span>
                                                    <span className="flex items-center gap-1">
                                                        <XCircle className="h-3 w-3 text-red-500" />
                                                        {proxy.fail_count}
                                                    </span>
                                                    {successRate !== null && (
                                                        <span className={`flex items-center gap-1 ${successRate >= 80 ? 'text-green-600' : successRate >= 50 ? 'text-yellow-600' : 'text-red-600'
                                                            }`}>
                                                            <AlertTriangle className="h-3 w-3" />
                                                            {successRate}%
                                                        </span>
                                                    )}
                                                </div>
                                            </div>
                                            <div className="flex items-center gap-2">
                                                <button
                                                    onClick={() => handleTestProxy(proxy.id)}
                                                    disabled={istesting}
                                                    className={`p-1.5 hover:bg-blue-50 dark:hover:bg-blue-900/30 rounded text-blue-500 transition-colors ${istesting ? 'animate-pulse opacity-50' : ''}`}
                                                    title="立即测试"
                                                >
                                                    {istesting ? (
                                                        <Loader2 className="h-5 w-5 animate-spin" />
                                                    ) : (
                                                        <Play className="h-5 w-5" />
                                                    )}
                                                </button>
                                                <button
                                                    onClick={() => handleToggle(proxy.id)}
                                                    className="p-1 hover:bg-slate-100 dark:hover:bg-slate-700 rounded transition-colors"
                                                    title={proxy.is_enabled ? '禁用' : '启用'}
                                                >
                                                    {proxy.is_enabled ? (
                                                        <ToggleRight className="h-6 w-6 text-green-500" />
                                                    ) : (
                                                        <ToggleLeft className="h-6 w-6 text-slate-400" />
                                                    )}
                                                </button>
                                                <button
                                                    onClick={() => handleDelete(proxy.id)}
                                                    className="p-1 hover:bg-red-50 dark:hover:bg-red-900/30 rounded text-red-500 transition-colors"
                                                    title="删除"
                                                >
                                                    <Trash2 className="h-5 w-5" />
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                )
                            })}
                        </div>
                    )}
                </CardContent>
            </Card>

            {/* Info */}
            <Card>
                <CardContent>
                    <div className="text-sm text-slate-500 dark:text-slate-400">
                        <p className="font-medium mb-2">使用说明：</p>
                        <ul className="list-disc list-inside space-y-1">
                            <li>代理池中的代理将在 CodexAuth API 授权时随机选择使用</li>
                            <li>支持 HTTP / HTTPS / SOCKS5 代理协议</li>
                            <li>支持带认证的代理格式：<code className="px-1 bg-slate-100 dark:bg-slate-800 rounded">http://user:pass@host:port</code></li>
                            <li>点击“测试”按钮可检查联通性并识别出口归属地（使用 ip-api.com）</li>
                            <li>系统会自动统计每个代理的成功/失败次数，禁用的代理不会被选中</li>
                        </ul>
                    </div>
                </CardContent>
            </Card>
        </div>
    )
}
