"use client"
import { useEffect } from 'react';

export default function ReminderSetup() {
  useEffect(() => {
    const checkReminder = () => {
      const enabled = localStorage.getItem('reminderEnabled') === 'true';
      if (!enabled) return;
      
      const timeStr = localStorage.getItem('reminderTime') || '23:00';
      const [targetH, targetM] = timeStr.split(':').map(Number);
      
      const now = new Date();
      const currentH = now.getHours();
      const currentM = now.getMinutes();

      // Simple check: if it's currently the same hour and minute, and we haven't reminded today
      if (currentH === targetH && currentM === targetM) {
        const lastReminded = localStorage.getItem('lastRemindedDate');
        const todayStr = now.toDateString();
        
        if (lastReminded !== todayStr && 'Notification' in window && Notification.permission === 'granted') {
          new Notification('අද tasks check කරන්න! 📋', {
            body: 'It is time to check off your daily tasks on the Weekly Planner.',
          });
          localStorage.setItem('lastRemindedDate', todayStr);
        }
      }
    };

    const interval = setInterval(checkReminder, 60000); // Check every minute
    checkReminder(); // Check on mount
    return () => clearInterval(interval);
  }, []);

  return null;
}
