import { apiClient } from '@/lib/api-client';
import type {
  CourseByIdResponse,
  CourseFilters,
  CourseMutationPayload,
  CoursesResponse
} from './types';

export async function getCourses(filters: CourseFilters): Promise<CoursesResponse> {
  const searchParams = new URLSearchParams();

  if (filters.page) searchParams.set('page', String(filters.page));
  if (filters.limit) searchParams.set('limit', String(filters.limit));
  if (filters.search) searchParams.set('search', filters.search);
  if (filters.category) searchParams.set('category', filters.category);
  if (filters.status) searchParams.set('status', filters.status);
  if (filters.sort) searchParams.set('sort', filters.sort);

  return apiClient<CoursesResponse>(`/courses?${searchParams.toString()}`);
}

export async function getCourseById(id: number): Promise<CourseByIdResponse> {
  return apiClient<CourseByIdResponse>(`/courses/${id}`);
}

export async function createCourse(data: CourseMutationPayload) {
  return apiClient<CourseByIdResponse>('/courses', {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateCourse(id: number, data: CourseMutationPayload) {
  return apiClient<CourseByIdResponse>(`/courses/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteCourse(id: number) {
  return apiClient<{ success: boolean; message: string }>(`/courses/${id}`, {
    method: 'DELETE'
  });
}
