/**
 * Projects API Client
 * Handles project-related API requests
 */

import { apiGet, apiPost, apiPatch, apiDelete } from "./client";

/**
 * Project status enum (matches backend ProjectStatus)
 */
export enum ProjectStatus {
  PLANNING = "PLANNING",
  ACTIVE = "ACTIVE",
  PAUSED = "PAUSED",
  COMPLETED = "COMPLETED",
  ARCHIVED = "ARCHIVED",
}

/**
 * Project response interface (matches Prisma Project model)
 */
export interface Project {
  id: string;
  workspaceId: string;
  name: string;
  description: string | null;
  status: ProjectStatus;
  startDate: string | null;
  endDate: string | null;
  creatorId: string;
  domainId: string | null;
  color: string | null;
  metadata: Record<string, unknown>;
  createdAt: string;
  updatedAt: string;
}

/**
 * DTO for creating a new project
 */
export interface CreateProjectDto {
  name: string;
  description?: string;
  status?: ProjectStatus;
  startDate?: string;
  endDate?: string;
  color?: string;
  metadata?: Record<string, unknown>;
}

/**
 * DTO for updating an existing project
 */
export interface UpdateProjectDto {
  name?: string;
  description?: string | null;
  status?: ProjectStatus;
  startDate?: string | null;
  endDate?: string | null;
  color?: string | null;
  metadata?: Record<string, unknown>;
}

/**
 * Fetch all projects for a workspace
 */
export async function fetchProjects(workspaceId?: string): Promise<Project[]> {
  return apiGet<Project[]>("/api/projects", workspaceId);
}

/**
 * Fetch a single project by ID
 */
export async function fetchProject(id: string, workspaceId?: string): Promise<Project> {
  return apiGet<Project>(`/api/projects/${id}`, workspaceId);
}

/**
 * Create a new project
 */
export async function createProject(
  data: CreateProjectDto,
  workspaceId?: string
): Promise<Project> {
  return apiPost<Project>("/api/projects", data, workspaceId);
}

/**
 * Update an existing project
 */
export async function updateProject(
  id: string,
  data: UpdateProjectDto,
  workspaceId?: string
): Promise<Project> {
  return apiPatch<Project>(`/api/projects/${id}`, data, workspaceId);
}

/**
 * Delete a project
 */
export async function deleteProject(id: string, workspaceId?: string): Promise<void> {
  await apiDelete<Record<string, never>>(`/api/projects/${id}`, workspaceId);
}
