import type {
  Training,
  TrainingSearchParams,
  CreateTrainingDto,
  UpdateTrainingDto,
  CancelTrainingDto,
  RescheduleTrainingDto,
  TrainingsByDateResponse,
  TimeSlot,
} from '../types/training';
import type { PaginatedResponse } from '../types/client';
import { api } from './api';

export const scheduleService = {
  async getTrainings(
    params?: TrainingSearchParams,
  ): Promise<PaginatedResponse<Training>> {
    return api.get<PaginatedResponse<Training>>('/trainings', {
      params: {
        date: params?.date,
        trainerId: params?.trainerId,
        clientId: params?.clientId,
        status: params?.status,
        cursor: params?.cursor,
        limit: params?.limit,
      },
    });
  },

  async getTrainingsByDate(
    date: string,
    trainerId?: string,
  ): Promise<TrainingsByDateResponse> {
    const params: Record<string, string | undefined> = {};
    if (trainerId) params.trainerId = trainerId;
    const data = await api.get<Training[]>(`/trainings/date/${date}`, {
      params,
    });
    return { data, date };
  },

  async getTrainerSlots(
    trainerId: string,
    date: string,
  ): Promise<TimeSlot[]> {
    return api.get<TimeSlot[]>(`/trainings/slots/${trainerId}/${date}`);
  },

  async getTraining(id: string): Promise<Training> {
    return api.get<Training>(`/trainings/${id}`);
  },

  async createTraining(dto: CreateTrainingDto): Promise<Training> {
    return api.post<Training>('/trainings', dto);
  },

  async updateTraining(
    id: string,
    dto: UpdateTrainingDto,
  ): Promise<Training> {
    return api.patch<Training>(`/trainings/${id}`, dto);
  },

  async cancelTraining(
    id: string,
    dto: CancelTrainingDto,
  ): Promise<Training> {
    return api.post<Training>(`/trainings/${id}/cancel`, dto);
  },

  async rescheduleTraining(
    id: string,
    dto: RescheduleTrainingDto,
  ): Promise<Training> {
    return api.post<Training>(`/trainings/${id}/reschedule`, dto);
  },

  async completeTraining(id: string): Promise<Training> {
    return api.post<Training>(`/trainings/${id}/complete`);
  },

  async confirmTraining(id: string): Promise<Training> {
    return api.post<Training>(`/trainings/${id}/confirm`);
  },
};
