import { Badge } from "@/components/ui/badge";
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Separator } from "@/components/ui/separator";
import type { Audits } from "@/types/audit";

function JsonDisplay({ value }: { value: string | null | undefined }) {
  if (!value) return <span className="text-muted-foreground">—</span>;
  try {
    return (
      <pre className="text-xs bg-muted/60 p-2.5 rounded-md overflow-auto whitespace-pre-wrap break-all leading-relaxed max-h-48 font-mono">
        {JSON.stringify(JSON.parse(value), null, 2)}
      </pre>
    );
  } catch {
    return <span className="text-xs break-all font-mono">{value}</span>;
  }
}

interface AuditDetailDialogProps {
  audit: Audits | null;
  open: boolean;
  onClose: () => void;
}

export function AuditDetailDialog({
  audit,
  open,
  onClose,
}: AuditDetailDialogProps) {
  if (!audit) return null;

  

  return (
    <Dialog open={open} onOpenChange={(o) => !o && onClose()}>
      <DialogContent className="max-w-2xl w-full max-h-[85vh] overflow-y-auto">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            Chi tiết audit
            <span className="text-muted-foreground font-normal text-sm">
              #{audit.id}
            </span>
          </DialogTitle>
        </DialogHeader>

        <Separator />

        <div className="grid grid-cols-2 gap-x-6 gap-y-3 pt-1">
          <div className="space-y-0.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              Thời gian
            </p>
            <p className="text-sm font-medium">
              {audit.dateTime
                ? new Date(audit.dateTime).toLocaleString("vi-VN")
                : "—"}
            </p>
          </div>

          <div className="space-y-0.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              User
            </p>
            <p className="text-sm font-medium">{audit.username}</p>
          </div>

          <div className="space-y-0.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              API Call
            </p>
            <code className="text-xs bg-muted px-1.5 py-0.5 rounded">
              {audit.apiCall ?? "—"}
            </code>
          </div>

          <div className="space-y-0.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              Kết quả
            </p>
            <div>
              {audit.isSuccess == null ? (
                <span className="text-muted-foreground text-sm">—</span>
              ) : audit.isSuccess ? (
                <Badge
                  variant="outline"
                  className="text-green-600 border-green-600"
                >
                  Thành công
                </Badge>
              ) : (
                <Badge
                  variant="outline"
                  className="text-red-600 border-red-600"
                >
                  Thất bại
                </Badge>
              )}
            </div>
          </div>

          <div className="space-y-0.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              Hành động
            </p>
            <code className="text-xs bg-muted px-1.5 py-0.5 rounded">
              {audit.action}
            </code>
          </div>

          <div className="space-y-0.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              URL
            </p>
            <code className="text-xs text-muted-foreground break-all">
              {audit.url ?? "—"}
            </code>
          </div>

          <div className="space-y-0.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              Bảng
            </p>
            <p className="text-sm">{audit.tableName ?? "—"}</p>
          </div>

          <div className="space-y-0.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              Entity ID
            </p>
            <p className="text-sm">{audit.entityId ?? "—"}</p>
          </div>

          <div className="col-span-2 space-y-0.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              Lỗi
            </p>
            <p className="text-sm text-red-600">{audit.errorMessage ?? "—"}</p>
          </div>
        </div>

        <Separator />

        <div className="space-y-4">
          <div className="space-y-1.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              Nội dung request 
            </p>
            <JsonDisplay value={audit.requestPayload} />
          </div>

          <div className="space-y-1.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              Giá trị cũ
            </p>
            <JsonDisplay value={audit.oldValues} />
          </div>

          <div className="space-y-1.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              Giá trị mới 
            </p>
            <JsonDisplay value={audit.newValues} />
          </div>

          <div className="space-y-1.5">
            <p className="text-xs text-muted-foreground uppercase tracking-wide">
              Kết quả
            </p>
            <p className="text-sm">{audit.isSuccess == null ? "—" : audit.isSuccess ? "Thành công" : "Thất bại"}</p>
          </div>
        </div>
      </DialogContent>
    </Dialog>
  );
}