/**
 * Task API Client
 * Handles task-related API requests
 */

import type { Task } from "@mosaic/shared";
import { TaskStatus, TaskPriority } from "@mosaic/shared";
import { apiGet, type ApiResponse } from "./client";

export interface TaskFilters {
  status?: TaskStatus;
  priority?: TaskPriority;
  workspaceId?: string;
}

/**
 * Fetch tasks with optional filters
 */
export async function fetchTasks(filters?: TaskFilters): Promise<Task[]> {
  const params = new URLSearchParams();

  if (filters?.status) {
    params.append("status", filters.status);
  }
  if (filters?.priority) {
    params.append("priority", filters.priority);
  }
  if (filters?.workspaceId) {
    params.append("workspaceId", filters.workspaceId);
  }

  const queryString = params.toString();
  const endpoint = queryString ? `/api/tasks?${queryString}` : "/api/tasks";

  const response = await apiGet<ApiResponse<Task[]>>(endpoint);
  return response.data;
}

/**
 * Mock tasks for development (until backend endpoints are ready)
 */
export const mockTasks: Task[] = [
  {
    id: "task-1",
    title: "Review pull request",
    description: "Review and provide feedback on frontend PR",
    status: TaskStatus.IN_PROGRESS,
    priority: TaskPriority.HIGH,
    dueDate: new Date("2026-01-29"),
    creatorId: "user-1",
    assigneeId: "user-1",
    workspaceId: "workspace-1",
    projectId: null,
    parentId: null,
    sortOrder: 0,
    metadata: {},
    completedAt: null,
    createdAt: new Date("2026-01-28"),
    updatedAt: new Date("2026-01-28"),
  },
  {
    id: "task-2",
    title: "Update documentation",
    description: "Add setup instructions for new developers",
    status: TaskStatus.IN_PROGRESS,
    priority: TaskPriority.MEDIUM,
    dueDate: new Date("2026-01-30"),
    creatorId: "user-1",
    assigneeId: "user-1",
    workspaceId: "workspace-1",
    projectId: null,
    parentId: null,
    sortOrder: 1,
    metadata: {},
    completedAt: null,
    createdAt: new Date("2026-01-28"),
    updatedAt: new Date("2026-01-28"),
  },
  {
    id: "task-3",
    title: "Plan Q1 roadmap",
    description: "Define priorities for Q1 2026",
    status: TaskStatus.NOT_STARTED,
    priority: TaskPriority.HIGH,
    dueDate: new Date("2026-02-03"),
    creatorId: "user-1",
    assigneeId: "user-1",
    workspaceId: "workspace-1",
    projectId: null,
    parentId: null,
    sortOrder: 2,
    metadata: {},
    completedAt: null,
    createdAt: new Date("2026-01-28"),
    updatedAt: new Date("2026-01-28"),
  },
  {
    id: "task-4",
    title: "Research new libraries",
    description: "Evaluate options for state management",
    status: TaskStatus.PAUSED,
    priority: TaskPriority.LOW,
    dueDate: new Date("2026-02-10"),
    creatorId: "user-1",
    assigneeId: "user-1",
    workspaceId: "workspace-1",
    projectId: null,
    parentId: null,
    sortOrder: 3,
    metadata: {},
    completedAt: null,
    createdAt: new Date("2026-01-28"),
    updatedAt: new Date("2026-01-28"),
  },
];
