// src/app/features/courses/course.service.ts
import { inject, Injectable } from '@angular/core';
import { PaginatedHttpService } from '@shared/paging/paginated-http.service';
import { BackendConfig, ListParams, PagedResult } from '@shared/paging/paging';
import { Observable } from 'rxjs';

export interface Course {
  id: string;
  numero: number;
  nom: string;
  type_course: string;
  depart_at: string | null;
  statut: string;
}

@Injectable({ providedIn: 'root' })
export class CourseService {
  private http = inject(PaginatedHttpService);
  private base = '/api/courses';

  list(params: ListParams): Observable<PagedResult<Course>> {
    const cfg: BackendConfig = {
      zeroBasedPageIndex: true,
      buildSort: (key, dir) => (key && dir ? ['sort', `${key},${dir}`] : null),
      mapClientSortKey: (k) => {
        const map: Record<string, string> = {
          depart_at: 'departAt',
          type_course: 'type',
          numero: 'numero',
          nom: 'nom',
          statut: 'statut',
        };
        return k ? map[k] ?? k : undefined;
      },
    };
    return this.http.fetch<Course>(this.base, params, cfg);
  }
}
