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

export type DocCategory = 'Policy' | 'Certificate' | 'Evidence' | 'Contract' | 'Exit Interview' | 'Other';
export type DocStatus = 'Active' | 'Expired' | 'Expiring Soon' | 'Archived';

export interface Document {
  id: string;
  name: string;
  category: DocCategory;
  uploadedBy: string;
  uploadDate: string;
  fileSize: string;
  fileType: string;
  linkedTo?: string;
  linkedModule?: string;
  expiryDate?: string;
  status: DocStatus;
  confidential: boolean;
  description?: string;
}

interface DocumentContextType {
  documents: Document[];
  addDocument: (doc: Omit<Document, 'id' | 'uploadDate' | 'status'>) => void;
  deleteDocument: (id: string) => void;
  updateDocument: (id: string, updates: Partial<Document>) => void;
  getByModule: (module: string) => Document[];
  getByEmployee: (employeeId: string) => Document[];
}

const INITIAL_DOCS: Document[] = [
  { id: 'doc-001', name: 'Uwezo Fund HR Policy Manual v3.0', category: 'Policy', uploadedBy: 'System Administrator', uploadDate: '2026-01-15', fileSize: '2.4 MB', fileType: 'PDF', linkedModule: 'Setup', status: 'Active', confidential: false, description: 'Comprehensive HR policies and procedures manual' },
  { id: 'doc-002', name: 'Data Protection Policy 2026', category: 'Policy', uploadedBy: 'System Administrator', uploadDate: '2026-02-01', fileSize: '1.1 MB', fileType: 'PDF', linkedModule: 'Setup', status: 'Active', confidential: false },
  { id: 'doc-003', name: 'Jane Mwangi - CPAK Certificate', category: 'Certificate', uploadedBy: 'HR Manager', uploadDate: '2025-06-20', fileSize: '450 KB', fileType: 'PDF', linkedTo: 'UF001', linkedModule: 'Training', expiryDate: '2026-06-20', status: 'Active', confidential: false },
  { id: 'doc-004', name: 'Samuel Kipchoge - Driving License', category: 'Certificate', uploadedBy: 'HR Manager', uploadDate: '2024-03-10', fileSize: '320 KB', fileType: 'JPG', linkedTo: 'UF006', linkedModule: 'Employees', expiryDate: '2026-03-10', status: 'Expiring Soon', confidential: false },
  { id: 'doc-005', name: 'DISC-2026-003 - Evidence Email Screenshots', category: 'Evidence', uploadedBy: 'HR Manager', uploadDate: '2026-03-22', fileSize: '1.8 MB', fileType: 'PDF', linkedTo: 'DISC-2026-003', linkedModule: 'Disciplinary', status: 'Active', confidential: true },
  { id: 'doc-006', name: 'Grace Wanjiku - Employment Contract', category: 'Contract', uploadedBy: 'System Administrator', uploadDate: '2023-07-01', fileSize: '890 KB', fileType: 'PDF', linkedTo: 'UF005', linkedModule: 'Employees', expiryDate: '2026-06-30', status: 'Expiring Soon', confidential: true },
  { id: 'doc-007', name: 'Robert Kariuki - Exit Interview Notes', category: 'Exit Interview', uploadedBy: 'HR Manager', uploadDate: '2026-03-15', fileSize: '280 KB', fileType: 'PDF', linkedTo: 'TERM-2026-001', linkedModule: 'Termination', status: 'Active', confidential: true },
  { id: 'doc-008', name: 'Leadership Workshop Completion Certificates - Batch', category: 'Certificate', uploadedBy: 'HR Manager', uploadDate: '2026-04-02', fileSize: '5.2 MB', fileType: 'PDF', linkedModule: 'Training', status: 'Active', confidential: false },
  { id: 'doc-009', name: 'Alice Odhiambo - AWS Solutions Architect', category: 'Certificate', uploadedBy: 'HR Manager', uploadDate: '2025-11-30', fileSize: '520 KB', fileType: 'PDF', linkedTo: 'UF009', linkedModule: 'Training', expiryDate: '2027-11-30', status: 'Active', confidential: false },
  { id: 'doc-010', name: 'DISC-2026-007 - Hearing Minutes', category: 'Evidence', uploadedBy: 'HR Manager', uploadDate: '2026-04-10', fileSize: '750 KB', fileType: 'DOCX', linkedTo: 'DISC-2026-007', linkedModule: 'Disciplinary', status: 'Active', confidential: true },
  { id: 'doc-011', name: 'Anti-Corruption Policy 2025', category: 'Policy', uploadedBy: 'System Administrator', uploadDate: '2025-01-10', fileSize: '980 KB', fileType: 'PDF', linkedModule: 'Setup', expiryDate: '2027-01-10', status: 'Active', confidential: false },
  { id: 'doc-012', name: 'Peter Otieno - CISCO CCNA Certificate', category: 'Certificate', uploadedBy: 'HR Manager', uploadDate: '2024-08-15', fileSize: '410 KB', fileType: 'PDF', linkedTo: 'UF002', linkedModule: 'Training', expiryDate: '2026-08-15', status: 'Active', confidential: false },
];

const DocContext = createContext<DocumentContextType | null>(null);

export function DocumentProvider({ children }: { children: ReactNode }) {
  const [documents, setDocuments] = useState<Document[]>(INITIAL_DOCS);

  const addDocument = (doc: Omit<Document, 'id' | 'uploadDate' | 'status'>) => {
    const today = new Date().toISOString().split('T')[0];
    let status: DocStatus = 'Active';
    if (doc.expiryDate) {
      const exp = new Date(doc.expiryDate);
      const diff = (exp.getTime() - Date.now()) / (1000 * 60 * 60 * 24);
      if (diff < 0) status = 'Expired';
      else if (diff <= 30) status = 'Expiring Soon';
    }
    setDocuments(prev => [{ ...doc, id: `doc-${Date.now()}`, uploadDate: today, status }, ...prev]);
  };

  const deleteDocument = (id: string) => setDocuments(prev => prev.filter(d => d.id !== id));

  const updateDocument = (id: string, updates: Partial<Document>) => {
    setDocuments(prev => prev.map(d => d.id === id ? { ...d, ...updates } : d));
  };

  const getByModule = (module: string) => documents.filter(d => d.linkedModule === module);
  const getByEmployee = (employeeId: string) => documents.filter(d => d.linkedTo === employeeId);

  return (
    <DocContext.Provider value={{ documents, addDocument, deleteDocument, updateDocument, getByModule, getByEmployee }}>
      {children}
    </DocContext.Provider>
  );
}

export function useDocuments() {
  const ctx = useContext(DocContext);
  if (!ctx) throw new Error('useDocuments must be used within DocumentProvider');
  return ctx;
}
