import { httpClient } from './base/http-client';
import {
  Task,
  TaskStatus,
  TaskPriority,
  TaskStats,
  DailyCheckbox,
} from '@/lib/types';

export interface TaskFilters {
  status?: TaskStatus[];
  source?: string[];
  search?: string;
  limit?: number;
  offset?: number;
}

export interface TasksResponse {
  success: boolean;
  data: Task[];
  stats: TaskStats;
  count: number;
}

export interface CreateTaskData {
  title: string;
  description?: string;
  status?: TaskStatus;
  priority?: TaskPriority;
  tags?: string[];
  dueDate?: Date;
}

export interface UpdateTaskData {
  taskId: string;
  title?: string;
  description?: string;
  status?: TaskStatus;
  priority?: TaskPriority;
  tags?: string[];
  dueDate?: Date;
}

/**
 * Client pour la gestion des tâches
 */
export class TasksClient {
  /**
   * Récupère toutes les tâches avec filtres
   */
  async getTasks(filters?: TaskFilters): Promise<TasksResponse> {
    const params: Record<string, string> = {};

    if (filters?.status) {
      params.status = filters.status.join(',');
    }
    if (filters?.source) {
      params.source = filters.source.join(',');
    }
    if (filters?.search) {
      params.search = filters.search;
    }
    if (filters?.limit) {
      params.limit = filters.limit.toString();
    }
    if (filters?.offset) {
      params.offset = filters.offset.toString();
    }

    return httpClient.get<TasksResponse>('/tasks', params);
  }

  /**
   * Récupère les daily checkboxes liées à une tâche
   */
  async getTaskCheckboxes(taskId: string): Promise<DailyCheckbox[]> {
    const response = await httpClient.get<{ data: DailyCheckbox[] }>(
      `/tasks/${taskId}/checkboxes`
    );
    return response.data;
  }

  // Note: Les méthodes createTask, updateTask et deleteTask ont été migrées vers Server Actions
  // Voir /src/actions/tasks.ts pour createTask, updateTask, updateTaskTitle, updateTaskStatus, deleteTask
}

// Instance singleton
export const tasksClient = new TasksClient();
