import { AlertTriangle, Bell, Shield, TrendingDown, AlertOctagon } from 'lucide-react';
import type { RiskAlert } from '@/types';

interface RiskAlertsProps {
  alerts: RiskAlert[];
}

function RiskLevelBadge({ level }: { level: 'high' | 'medium' | 'low' }) {
  const config = {
    high: { text: '高风险', color: 'text-semantic-red', bg: 'bg-semantic-red/10', icon: AlertOctagon },
    medium: { text: '中风险', color: 'text-binance-gold', bg: 'bg-binance-gold/10', icon: AlertTriangle },
    low: { text: '低风险', color: 'text-semantic-green', bg: 'bg-semantic-green/10', icon: Shield },
  };
  
  const { text, color, bg, icon: Icon } = config[level];
  
  return (
    <div className={`flex items-center gap-1 px-2 py-1 rounded-md ${bg}`}>
      <Icon className={`w-3 h-3 ${color}`} />
      <span className={`text-xs font-semibold ${color}`}>{text}</span>
    </div>
  );
}

export function RiskAlertsPanel({ alerts }: RiskAlertsProps) {
  if (alerts.length === 0) {
    return (
      <div className="p-4 rounded-card border border-border-light bg-white">
        <div className="flex items-center gap-2 mb-space-4">
          <div className="p-2 rounded-lg bg-semantic-green/10">
            <Shield className="w-4 h-4 text-semantic-green" />
          </div>
          <h3 className="font-bold text-text-ink">风险提醒</h3>
        </div>
        <div className="text-center py-6 text-text-slate">
          <Shield className="w-10 h-10 mx-auto mb-2 opacity-50" />
          <p className="text-sm">当前无重大风险事件</p>
        </div>
      </div>
    );
  }

  return (
    <div className="p-4 rounded-card border border-border-light bg-white">
      <div className="flex items-center justify-between mb-space-4">
        <div className="flex items-center gap-2">
          <div className="p-2 rounded-lg bg-semantic-red/10">
            <Bell className="w-4 h-4 text-semantic-red" />
          </div>
          <h3 className="font-bold text-text-ink">风险提醒</h3>
        </div>
        <span className="px-2 py-1 rounded-full bg-semantic-red/10 text-semantic-red text-xs font-semibold">
          {alerts.length} 条
        </span>
      </div>
      
      <div className="space-y-space-3">
        {alerts.map((alert) => (
          <div
            key={alert.id}
            className="p-3 rounded-lg border border-binanceBorder-light bg-surface-snow hover:border-semantic-red/30 transition-colors"
          >
            <div className="flex items-start justify-between gap-3 mb-2">
              <div className="flex-1">
                <h4 className="text-sm font-semibold text-text-ink mb-1">{alert.title}</h4>
                <p className="text-xs text-text-slate line-clamp-2">{alert.description}</p>
              </div>
              <RiskLevelBadge level={alert.riskLevel} />
            </div>
            
            <div className="flex items-center justify-between text-xs">
              <div className="flex items-center gap-2">
                <span className="text-text-slate">影响品种:</span>
                <div className="flex gap-1">
                  {alert.affectedProducts.slice(0, 2).map((product) => (
                    <span key={product} className="px-1.5 py-0.5 rounded bg-white text-text-secondary">
                      {product}
                    </span>
                  ))}
                  {alert.affectedProducts.length > 2 && (
                    <span className="text-text-slate">+{alert.affectedProducts.length - 2}</span>
                  )}
                </div>
              </div>
              <span className="text-text-slate">{alert.time}</span>
            </div>
            
            <div className="mt-2 pt-2 border-t border-binanceBorder-light/50">
              <div className="flex items-center gap-1 text-xs">
                <TrendingDown className="w-3 h-3 text-binance-gold" />
                <span className="text-binance-gold">建议: {alert.suggestion}</span>
              </div>
            </div>
          </div>
        ))}
      </div>
    </div>
  );
}