/**
 * 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;
  priority?: string | null;
  startDate: string | null;
  dueDate?: string | null;
  endDate: string | null;
  creatorId: string;
  domainId: string | null;
  color: string | null;
  metadata: Record<string, unknown>;
  createdAt: string;
  updatedAt: string;
}

/**
 * Minimal creator details included on project detail response
 */
export interface ProjectCreator {
  id: string;
  name: string | null;
  email: string;
}

/**
 * Task row included on project detail response
 */
export interface ProjectTaskSummary {
  id: string;
  title: string;
  status: string;
  priority: string;
  dueDate: string | null;
}

/**
 * Event row included on project detail response
 */
export interface ProjectEventSummary {
  id: string;
  title: string;
  startTime: string;
  endTime: string | null;
}

/**
 * Counts included on project detail response
 */
export interface ProjectDetailCounts {
  tasks: number;
  events: number;
}

/**
 * Single-project response with related details
 */
export interface ProjectDetail extends Project {
  creator: ProjectCreator;
  tasks: ProjectTaskSummary[];
  events: ProjectEventSummary[];
  _count: ProjectDetailCounts;
}

/**
 * DTO for creating a new project
 */
export interface CreateProjectDto {
  name: string;
  description?: string;
  status?: ProjectStatus;
  startDate?: string;
  endDate?: string;
  color?: string;
  domainId?: 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;
  domainId?: string | null;
  metadata?: Record<string, unknown>;
}

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

/**
 * Fetch a single project by ID
 */
export async function fetchProject(id: string, workspaceId?: string): Promise<ProjectDetail> {
  return apiGet<ProjectDetail>(`/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);
}
