import React from 'react';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { AlertCircle } from 'lucide-react';
import { useToast } from '@/components/ui/use-toast';
import { deleteApplication } from '../service';
import type { Application, DevelopmentLanguageTypeEnum } from '../types';

interface DeleteDialogProps {
  open: boolean;
  record?: Application;
  onOpenChange: (open: boolean) => void;
  onSuccess: () => void;
}

const getLanguageLabel = (language: DevelopmentLanguageTypeEnum) => {
  const languageMap = {
    JAVA: 'Java',
    NODE_JS: 'NodeJS',
    PYTHON: 'Python',
    GO: 'Go',
  };
  return languageMap[language] || language;
};

/**
 * 应用删除确认对话框
 */
const DeleteDialog: React.FC<DeleteDialogProps> = ({
  open,
  record,
  onOpenChange,
  onSuccess,
}) => {
  const { toast } = useToast();

  const handleConfirm = async () => {
    if (!record) return;

    try {
      await deleteApplication(record.id);
      toast({ title: '删除成功', description: `应用 "${record.appName}" 已删除` });
      onSuccess();
      onOpenChange(false);
    } catch (error) {
      // 错误提示统一由全局 request 拦截器处理，这里仅记录日志
      console.error('删除失败:', error);
    }
  };

  if (!record) return null;

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-[500px]">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2 text-destructive">
            <AlertCircle className="h-5 w-5" />
            确认删除
          </DialogTitle>
          <DialogDescription className="space-y-3 pt-4">
            <p>您确定要删除以下应用吗？此操作不可撤销。</p>
            <div className="bg-muted p-3 rounded-md space-y-2 text-sm">
              <div className="flex items-center justify-between">
                <span className="text-muted-foreground">应用编码:</span>
                <span className="font-medium">{record.appCode}</span>
              </div>
              <div className="flex items-center justify-between">
                <span className="text-muted-foreground">应用名称:</span>
                <span className="font-medium">{record.appName}</span>
              </div>
              {record.category && (
                <div className="flex items-center justify-between">
                  <span className="text-muted-foreground">应用分类:</span>
                  <span className="font-medium">{record.category.name}</span>
                </div>
              )}
              <div className="flex items-center justify-between">
                <span className="text-muted-foreground">开发语言:</span>
                <Badge variant="outline">{getLanguageLabel(record.language)}</Badge>
              </div>
              {record.appDesc && (
                <div className="flex items-center justify-between">
                  <span className="text-muted-foreground">应用描述:</span>
                  <span className="font-medium">{record.appDesc}</span>
                </div>
              )}
            </div>
          </DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <Button variant="outline" onClick={() => onOpenChange(false)}>
            取消
          </Button>
          <Button variant="destructive" onClick={handleConfirm}>
            确认删除
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
};

export default DeleteDialog;

