'use client'

import { useCallback } from 'react'
import { Note } from '@/types/note'
import { NoteCard } from './note-card'
import { useNoteListKeyboard } from '@/hooks/use-note-list-keyboard'
import { toast } from 'sonner'

interface KeyboardNavigableNoteListProps {
  notes: Note[]
  onEdit?: (noteId: string) => void
}

export function KeyboardNavigableNoteList({
  notes,
  onEdit,
}: KeyboardNavigableNoteListProps) {
  const handleFavorite = useCallback(async (noteId: string) => {
    try {
      const res = await fetch(`/api/notes/${noteId}`, {
        method: 'PATCH',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ isFavorite: true }),
      })
      if (res.ok) {
        toast.success('Añadido a favoritos')
        window.location.reload()
      }
    } catch {
      toast.error('Error al añadir a favoritos')
    }
  }, [])

  const handlePin = useCallback(async (noteId: string) => {
    try {
      const res = await fetch(`/api/notes/${noteId}`, {
        method: 'PATCH',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ isPinned: true }),
      })
      if (res.ok) {
        toast.success('Nota fijada')
        window.location.reload()
      }
    } catch {
      toast.error('Error al fijar nota')
    }
  }, [])

  const { selectedIndex } = useNoteListKeyboard({
    notes,
    onEdit,
    onFavorite: handleFavorite,
    onPin: handlePin,
  })

  if (notes.length === 0) {
    return (
      <div className="text-center py-12 text-gray-500">
        <p className="text-lg">No hay notas todavía</p>
        <p className="text-sm">Crea tu primera nota para comenzar</p>
      </div>
    )
  }

  return (
    <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
      {notes.map((note, index) => (
        <div
          key={note.id}
          className={`relative ${index === selectedIndex ? 'ring-2 ring-primary ring-offset-2 rounded-lg' : ''}`}
          data-selected={index === selectedIndex}
        >
          <NoteCard note={note} />
          {index === selectedIndex && (
            <div className="absolute bottom-2 right-2 flex gap-1">
              <span className="px-1.5 py-0.5 bg-muted text-xs rounded text-muted-foreground">
                Enter: abrir | E: editar | F: favoritar | P: fijar
              </span>
            </div>
          )}
        </div>
      ))}
    </div>
  )
}
