import React, { createContext, useContext, useState } from "react";

// 1. Create Context
const ProjectContext = createContext();

// 2. Custom hook to use the context
export const useProjects = () => useContext(ProjectContext);

// 3. Provider Component
export const ProjectProvider = ({ children }) => {
  const [projects, setProjects] = useState([]);

  // Add new project
  const addProject = (project) => {
    setProjects((prev) => [...prev, project]);
  };

  // Edit existing project by ID
  const editProject = (updatedProject) => {
    setProjects((prev) =>
      prev.map((proj) =>
        proj.id === updatedProject.id ? updatedProject : proj
      )
    );
  };

  // Delete project by ID
  const deleteProject = (id) => {
    setProjects((prev) => prev.filter((proj) => proj.id !== id));
  };

  return (
    <ProjectContext.Provider
      value={{ projects, addProject, editProject, deleteProject }}
    >
      {children}
    </ProjectContext.Provider>
  );
};