'use client';

import React, { useState, useEffect } from 'react';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Loader2, Database, Trash2, Eye, Zap } from 'lucide-react';
import { clientCleanup } from '@/lib/client-connection-cleanup';

export default function TestIPCleanupPage() {
  const [status, setStatus] = useState<any>(null);
  const [loading, setLoading] = useState(false);
  const [message, setMessage] = useState<string>('');
  const [error, setError] = useState<string>('');

  // 獲取連線狀態
  const fetchStatus = async () => {
    try {
      setLoading(true);
      const status = await clientCleanup.getConnectionStatus();
      setStatus(status);
      setMessage('連線狀態更新成功');
      setError('');
    } catch (err) {
      setError('獲取狀態失敗: ' + (err instanceof Error ? err.message : '未知錯誤'));
    } finally {
      setLoading(false);
    }
  };

  // 手動清理
  const manualCleanup = async () => {
    try {
      setLoading(true);
      const success = await clientCleanup.manualCleanup();
      
      if (success) {
        setMessage('手動清理完成');
        setError('');
        await fetchStatus(); // 重新獲取狀態
      } else {
        setError('手動清理失敗');
      }
    } catch (err) {
      setError('手動清理錯誤: ' + (err instanceof Error ? err.message : '未知錯誤'));
    } finally {
      setLoading(false);
    }
  };

  // 模擬關閉頁面
  const simulatePageClose = () => {
    if (confirm('確定要模擬關閉頁面嗎？這會觸發自動清理機制。')) {
      // 觸發 beforeunload 事件
      window.dispatchEvent(new Event('beforeunload'));
      
      // 等待一下再觸發 unload 事件
      setTimeout(() => {
        window.dispatchEvent(new Event('unload'));
      }, 100);
    }
  };

  // 組件載入時獲取狀態
  useEffect(() => {
    fetchStatus();
  }, []);

  return (
    <div className="container mx-auto p-6 space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-3xl font-bold">IP 連線清理測試</h1>
          <p className="text-muted-foreground">
            測試基於 IP 的智能連線清理功能
          </p>
        </div>
        <Button 
          onClick={fetchStatus} 
          disabled={loading}
          variant="outline"
        >
          {loading ? <Loader2 className="h-4 w-4 animate-spin" /> : <Eye className="h-4 w-4" />}
          重新整理
        </Button>
      </div>

      {/* 客戶端信息 */}
      <Card>
        <CardHeader>
          <CardTitle className="flex items-center gap-2">
            <Database className="h-5 w-5" />
            客戶端信息
          </CardTitle>
        </CardHeader>
        <CardContent>
          <div className="space-y-2">
            <p><strong>客戶端 ID:</strong> {clientCleanup.getClientId()}</p>
            <p><strong>用戶代理:</strong> {navigator.userAgent}</p>
            <p><strong>當前時間:</strong> {new Date().toLocaleString()}</p>
          </div>
        </CardContent>
      </Card>

      {/* 連線狀態 */}
      {status && (
        <Card>
          <CardHeader>
            <CardTitle className="flex items-center gap-2">
              <Eye className="h-5 w-5" />
              連線狀態
            </CardTitle>
            <CardDescription>
              IP: {status.ip} | 連線數: {status.connectionCount}
            </CardDescription>
          </CardHeader>
          <CardContent className="space-y-4">
            <div className="flex items-center gap-4">
              <Badge variant="outline" className="text-lg px-3 py-1">
                連線數: {status.connectionCount}
              </Badge>
              <Badge 
                variant={status.connectionCount > 5 ? "destructive" : "default"}
                className="text-lg px-3 py-1"
              >
                狀態: {status.connectionCount > 5 ? "異常" : "正常"}
              </Badge>
            </div>
            
            {status.connections && status.connections.length > 0 && (
              <div className="space-y-2">
                <h4 className="font-medium">連線詳情:</h4>
                {status.connections.slice(0, 5).map((conn: any, index: number) => (
                  <div key={index} className="p-2 border rounded text-sm">
                    <p>ID: {conn.ID} | HOST: {conn.HOST} | 時間: {conn.TIME}s</p>
                  </div>
                ))}
                {status.connections.length > 5 && (
                  <p className="text-sm text-muted-foreground">
                    ... 還有 {status.connections.length - 5} 個連線
                  </p>
                )}
              </div>
            )}
          </CardContent>
        </Card>
      )}

      {/* 測試操作 */}
      <Card>
        <CardHeader>
          <CardTitle className="flex items-center gap-2">
            <Zap className="h-5 w-5" />
            測試操作
          </CardTitle>
          <CardDescription>
            測試各種連線清理功能
          </CardDescription>
        </CardHeader>
        <CardContent className="space-y-4">
          <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
            <Button 
              onClick={manualCleanup} 
              disabled={loading}
              variant="outline"
              className="w-full"
            >
              {loading ? <Loader2 className="h-4 w-4 animate-spin" /> : <Trash2 className="h-4 w-4" />}
              手動清理
            </Button>
            
            <Button 
              onClick={simulatePageClose} 
              disabled={loading}
              variant="destructive"
              className="w-full"
            >
              <Zap className="h-4 w-4" />
              模擬關閉頁面
            </Button>
            
            <Button 
              onClick={() => window.location.reload()} 
              disabled={loading}
              variant="secondary"
              className="w-full"
            >
              <Database className="h-4 w-4" />
              重新載入頁面
            </Button>
          </div>
        </CardContent>
      </Card>

      {/* 自動清理說明 */}
      <Card>
        <CardHeader>
          <CardTitle>自動清理機制</CardTitle>
        </CardHeader>
        <CardContent className="space-y-2 text-sm text-muted-foreground">
          <p>• <strong>頁面關閉前:</strong> 自動清理當前 IP 的所有連線</p>
          <p>• <strong>頁面隱藏時:</strong> 當切換到其他標籤頁時清理連線</p>
          <p>• <strong>定期清理:</strong> 每5分鐘檢查並清理多餘連線</p>
          <p>• <strong>手動清理:</strong> 可以隨時手動觸發清理</p>
          <p>• <strong>智能識別:</strong> 只清理當前 IP 的連線，不影響其他用戶</p>
        </CardContent>
      </Card>

      {/* 訊息顯示 */}
      {message && (
        <Alert>
          <Database className="h-4 w-4" />
          <AlertDescription>{message}</AlertDescription>
        </Alert>
      )}

      {error && (
        <Alert variant="destructive">
          <Trash2 className="h-4 w-4" />
          <AlertDescription>{error}</AlertDescription>
        </Alert>
      )}
    </div>
  );
}
