'use client';

import { useEffect, useRef } from 'react';
import { Note } from '@/lib/types';
import { toast } from 'sonner';

const STORAGE_KEY = 'memento-notified-reminders';

function getNotifiedFromStorage(): Set<string> {
  try {
    const raw = sessionStorage.getItem(STORAGE_KEY);
    return raw ? new Set(JSON.parse(raw)) : new Set();
  } catch {
    return new Set();
  }
}

function persistNotified(ids: Set<string>) {
  try {
    sessionStorage.setItem(STORAGE_KEY, JSON.stringify([...ids]));
  } catch { /* quota exceeded — non-critical */ }
}

export function useReminderCheck(notes: Note[]) {
  const notifiedRef = useRef<Set<string>>(getNotifiedFromStorage());

  useEffect(() => {
    const checkReminders = () => {
      const now = new Date();
      const newIds: string[] = [];

      for (const note of notes) {
        if (!note.reminder || note.isReminderDone) continue;
        if (notifiedRef.current.has(note.id)) continue;

        const reminderDate = new Date(note.reminder);
        if (reminderDate <= now) {
          newIds.push(note.id);
          toast.info(`🔔 ${note.title || 'Untitled Note'}`, { id: `reminder-${note.id}` });
        }
      }

      if (newIds.length > 0) {
        for (const id of newIds) notifiedRef.current.add(id);
        persistNotified(notifiedRef.current);
      }
    };

    checkReminders();
    const interval = setInterval(checkReminders, 30_000);
    return () => clearInterval(interval);
  }, [notes]);
}
