'use client';
import { createContext, useContext, useState, ReactNode } from 'react';

export type NotifType = 'leave' | 'appraisal' | 'payroll' | 'training' | 'employee' | 'system';

export interface Notification {
  id: string;
  type: NotifType;
  title: string;
  message: string;
  timestamp: string;
  read: boolean;
  alertType: 'info' | 'warning' | 'success' | 'error';
}

interface NotifContextType {
  notifications: Notification[];
  addNotification: (n: Omit<Notification, 'id' | 'timestamp' | 'read'>) => void;
  markRead: (id: string) => void;
  markAllRead: () => void;
  dismiss: (id: string) => void;
  dismissAll: () => void;
  unreadCount: number;
}

const NotifContext = createContext<NotifContextType | null>(null);

const INITIAL_NOTIFS: Notification[] = [
  { id: 'n1', type: 'leave', title: 'Leave Request Submitted', message: 'Jane Mwangi submitted an Annual Leave request for April 14–18, 2026.', timestamp: '2026-04-08 09:15', read: false, alertType: 'info' },
  { id: 'n2', type: 'appraisal', title: 'Appraisal Self-Evaluation Submitted', message: 'Fatuma Hassan submitted her Q1 2026 self-evaluation. Awaiting supervisor review.', timestamp: '2026-04-07 16:30', read: false, alertType: 'success' },
  { id: 'n3', type: 'appraisal', title: 'Appraisal Self-Evaluation Submitted', message: 'Alice Odhiambo submitted her Q1 2026 self-evaluation. Awaiting supervisor review.', timestamp: '2026-04-07 14:22', read: false, alertType: 'success' },
  { id: 'n4', type: 'payroll', title: 'Payroll Deadline Approaching', message: 'April 2026 payroll must be processed by April 25th. 2 employees still have pending status.', timestamp: '2026-04-08 08:00', read: false, alertType: 'warning' },
  { id: 'n5', type: 'employee', title: 'New Employee Added', message: 'Brian Omondi (UF012) has been successfully added to the ICT department.', timestamp: '2026-04-06 11:05', read: true, alertType: 'success' },
  { id: 'n6', type: 'training', title: 'Training Enrollment Closing', message: 'Leadership Skills Workshop enrollment closes April 12th. 8 slots remaining.', timestamp: '2026-04-05 09:00', read: true, alertType: 'warning' },
  { id: 'n7', type: 'system', title: 'Leave Quota Alert', message: 'Operations dept has 12 employees on leave simultaneously — above 10% threshold.', timestamp: '2026-04-04 14:00', read: true, alertType: 'error' },
  { id: 'n8', type: 'appraisal', title: 'Appraisal Cycle Completed', message: 'Q1 2026 performance appraisals for Finance dept are fully submitted and reviewed.', timestamp: '2026-04-03 17:00', read: true, alertType: 'success' },
  { id: 'n9', type: 'leave', title: 'Leave Request Approved', message: 'David Njoroge\'s Sick Leave request for April 1–3, 2026 has been approved by HR Manager.', timestamp: '2026-04-02 10:20', read: true, alertType: 'success' },
  { id: 'n10', type: 'employee', title: 'Contract Expiry Alert', message: '5 employee contracts expire within the next 30 days. Please review and renew as necessary.', timestamp: '2026-04-01 08:00', read: true, alertType: 'warning' },
];

export function NotifProvider({ children }: { children: ReactNode }) {
  const [notifications, setNotifications] = useState<Notification[]>(INITIAL_NOTIFS);

  const addNotification = (n: Omit<Notification, 'id' | 'timestamp' | 'read'>) => {
    const now = new Date();
    const ts = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
    const newNotif: Notification = { ...n, id: `n${Date.now()}`, timestamp: ts, read: false };
    setNotifications(prev => [newNotif, ...prev]);
  };

  const markRead = (id: string) => setNotifications(prev => prev.map(n => n.id === id ? { ...n, read: true } : n));
  const markAllRead = () => setNotifications(prev => prev.map(n => ({ ...n, read: true })));
  const dismiss = (id: string) => setNotifications(prev => prev.filter(n => n.id !== id));
  const dismissAll = () => setNotifications([]);
  const unreadCount = notifications.filter(n => !n.read).length;

  return (
    <NotifContext.Provider value={{ notifications, addNotification, markRead, markAllRead, dismiss, dismissAll, unreadCount }}>
      {children}
    </NotifContext.Provider>
  );
}

export function useNotifications() {
  const ctx = useContext(NotifContext);
  if (!ctx) throw new Error('useNotifications must be used within NotifProvider');
  return ctx;
}
