import {
  ADD_PROJECT,
  SELECT_PROJECT,
  ARCHIVE_PROJECT,
} from "../actionTypes";

const initialState = {
  projects: [],
  selectedProjectId: "0",
};

export default function projectReducer(state = initialState, action) {
  switch (action.type) {
    // Projects are added only during initialization of the program
    // New projects and resources it can use are defined outside of the client program
    // Project initiation and closure are considered a separate endeavors 
    // to project planning, execution, and control in this application. 
    case ADD_PROJECT: {
      return {
        ...state,
        projects: [
          ...state.projects,
          {
            projectId: action.project.projectId,
            projectName: action.project.projectName,
            description: action.project.projectDescription,
          },
        ],
        selectedProjectId: action.project.projectId,
      };
    }
    // This is an important switch as everything user does in the session is tied to
    // selected project. Selected project must always be prominently displayed in every
    // view.
    case SELECT_PROJECT: {
      return {
        ...state,
        projects: [...state.projects],
        selectedProjectId: action.id,
      };
    }
    // Default action when project is closed. Not yet implemented. Archived projects are just
    // moved to another view. Project deletion can only be done through admin program and will
    // only become noticeable to user after client program restart.
    case ARCHIVE_PROJECT: {
      return {
        ...state,
        projects: state.projects.map((project) => project.id === action.id),
        selectedProjectId: 0,
      };
    }
    default:
      return state;
  }
}
