import { apiClient } from './apiClient';

export const projectService = {
  /**
   * Fetch all active projects (includes dynamic member_count & task_count)
   */
  getProjects: async () => {
    return apiClient('/projects/list.php');
  },

  /**
   * Fetch single project details with section columns
   */
  getProjectDetail: async (projectId) => {
    return apiClient(`/projects/detail.php?id=${projectId}`);
  },

  /**
   * Create a new project and register owner in project_members
   */
  createProject: async (projectData) => {
    return apiClient('/projects/create.php', {
      method: 'POST',
      body: projectData,
    });
  },

  /**
   * Update existing project metadata (Name, Description, Due Date, Status)
   */
  updateProject: async (projectId, projectData) => {
    return apiClient('/projects/update.php', {
      method: 'POST',
      body: { id: projectId, ...projectData },
    });
  },

  /**
   * Delete a project and cascade tasks/sections
   */
  deleteProject: async (projectId) => {
    return apiClient('/projects/delete.php', {
      method: 'POST',
      body: { project_id: projectId },
    });
  },

  /**
   * Fetch dynamic member list for a given project
   */
  getProjectMembers: async (projectId) => {
    return apiClient(`/projects/members.php?project_id=${projectId}`);
  },
};

export default projectService;