'use client';

import { useEffect } from 'react';
import type { GenImage } from '@/lib/types';
import { HoverImagePreview } from './HoverImagePreview';

export type ResultGridProps = {
  images: GenImage[];
  onAction: (imageId: string, action: 'select' | 'reject' | 'reset') => void;
};

export default function ResultGrid({ images, onAction }: ResultGridProps) {
  useEffect(() => {
    function handler(e: KeyboardEvent) {
      const target = e.target as HTMLElement;
      if (target && (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA')) return;
      const n = parseInt(e.key, 10);
      if (isNaN(n) || n < 1 || n > 9 || n > images.length) return;
      const img = images[n - 1];
      if (!img) return;
      const action = e.shiftKey
        ? (img.status === 'rejected' ? 'reset' : 'reject')
        : (img.status === 'selected' ? 'reset' : 'select');
      onAction(img.id, action);
    }
    window.addEventListener('keydown', handler);
    return () => window.removeEventListener('keydown', handler);
  }, [images, onAction]);

  const cols = images.length <= 4 ? 'grid-cols-2' : images.length <= 9 ? 'grid-cols-3' : 'grid-cols-4';
  const selectedCount = images.filter(i => i.status === 'selected').length;

  return (
    <div className="space-y-4">
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-2 text-[11px] text-white/55">
          <kbd className="kbd">1</kbd>
          <span className="text-white/40">–</span>
          <kbd className="kbd">{Math.min(9, images.length)}</kbd>
          <span>选中</span>
          <span className="text-white/20 mx-1">/</span>
          <kbd className="kbd">⇧</kbd>
          <span className="text-white/40">+</span>
          <kbd className="kbd">1</kbd>
          <span>打叉</span>
        </div>
        <div className="text-xs text-white/55">
          已选
          <span className="ml-1 font-semibold bg-gradient-to-r from-[#e6f578] to-[#d6b36a] bg-clip-text text-transparent">{selectedCount}</span>
          <span className="text-white/30"> / {images.length}</span>
        </div>
      </div>

      <div className={`grid ${cols} gap-3`}>
        {images.map((img, i) => (
          <div
            key={img.id}
            className={`tile group ${img.status === 'selected' ? 'tile-selected' : ''} ${img.status === 'rejected' ? 'tile-rejected' : ''}`}
          >
            <HoverImagePreview src={img.url} alt={`gen ${i + 1}`} imageClassName="w-full h-full object-contain bg-white" />
            <div className="tile-keynum">{i + 1}</div>

            {img.status === 'selected' && (
              <div className="tile-badge tile-badge-selected">✓</div>
            )}
            {img.status === 'rejected' && (
              <div className="tile-badge tile-badge-rejected">✕</div>
            )}

            <div className="absolute inset-x-0 bottom-0 p-2 flex gap-2 opacity-0 group-hover:opacity-100 transition-opacity bg-gradient-to-t from-black/80 via-black/40 to-transparent">
              <button
                onClick={() => onAction(img.id, img.status === 'selected' ? 'reset' : 'select')}
                className="flex-1 px-3 py-1.5 rounded-[8px] bg-gradient-to-r from-[#e6f578] to-[#d6b36a] text-[#081006] text-xs font-semibold hover:brightness-110 transition shadow-glow-violet"
              >
                {img.status === 'selected' ? '✓ 已选' : '选中'}
              </button>
              <button
                onClick={() => onAction(img.id, img.status === 'rejected' ? 'reset' : 'reject')}
                className="px-3 py-1.5 rounded-lg bg-white/[0.10] text-white/85 text-xs font-medium hover:bg-rose-500/60 hover:text-white transition-colors backdrop-blur"
              >✕</button>
            </div>
          </div>
        ))}
      </div>
    </div>
  );
}
