'use client';

import { useEffect } from 'react';
import { X } from 'lucide-react';

interface KeyboardShortcutsModalProps {
  open: boolean;
  onClose: () => void;
}

const shortcuts = [
  { key: 'R', description: 'Rectangle tool' },
  { key: 'S', description: 'Span tool' },
  { key: 'L', description: 'Line tool' },
  { key: 'D', description: 'Delete tool' },
  { key: 'T', description: 'Toggle theme (light/dark)' },
  { key: 'Esc', description: 'Deselect tool / cancel action' },
  { key: 'Del / ⌫', description: 'Delete selected annotation' },
  { key: 'Enter', description: 'Edit selected span' },
  { key: '1–6', description: 'Quick-assign span label (during span creation)' },
  { key: '?', description: 'Show this help' },
];

export default function KeyboardShortcutsModal({ open, onClose }: KeyboardShortcutsModalProps) {
  useEffect(() => {
    if (!open) return;
    const handleKey = (e: KeyboardEvent) => {
      if (e.key === 'Escape' || e.key === '?') {
        onClose();
      }
    };
    window.addEventListener('keydown', handleKey);
    return () => window.removeEventListener('keydown', handleKey);
  }, [open, onClose]);

  if (!open) return null;

  return (
    <div
      className="fixed inset-0 z-50 flex items-center justify-center bg-black/50"
      onClick={onClose}
    >
      <div
        className="bg-background border border-border rounded-lg shadow-xl w-96 p-5"
        onClick={(e) => e.stopPropagation()}
      >
        <div className="flex items-center justify-between mb-4">
          <h2 className="text-sm font-semibold text-foreground">Keyboard Shortcuts</h2>
          <button
            onClick={onClose}
            className="p-1 rounded text-muted-foreground hover:text-foreground hover:bg-secondary transition-colors"
          >
            <X className="w-4 h-4" />
          </button>
        </div>

        <table className="w-full text-xs">
          <tbody>
            {shortcuts.map(({ key, description }) => (
              <tr key={key} className="border-b border-border last:border-0">
                <td className="py-1.5 pr-4 font-mono">
                  <kbd className="px-1.5 py-0.5 bg-secondary rounded text-foreground font-mono text-[11px]">
                    {key}
                  </kbd>
                </td>
                <td className="py-1.5 text-muted-foreground">{description}</td>
              </tr>
            ))}
          </tbody>
        </table>

        <p className="mt-4 text-[10px] text-muted-foreground text-center">
          Press <kbd className="px-1 py-0.5 bg-secondary rounded font-mono">?</kbd> or <kbd className="px-1 py-0.5 bg-secondary rounded font-mono">Esc</kbd> to close
        </p>
      </div>
    </div>
  );
}
