import { api } from './api';

export interface CreateTrainingPlanDto {
  startDate: string;
  name?: string;
  mode: 'daysOfWeek' | 'sessionsPerWeek';
  daysOfWeek: number[];
  sessionsPerWeek: number;
  goal: string;
  startWeightKg?: number;
  preferredTimeOfDay?: 'morning' | 'noon' | 'evening' | '';
}

export interface TrainingPlanResponse {
  id: string;
  userId: string;
  name: string;
  createdAt: string;
  startDate: string;
  mode: 'daysOfWeek' | 'sessionsPerWeek';
  daysOfWeek: number[];
  sessionsPerWeek: number;
  goal: string;
  startWeightKg: number | null;
  preferredTimeOfDay: 'morning' | 'noon' | 'evening' | '';
  updatedAt: string;
  deleted: boolean;
}

export interface TrainingPlanSummary {
  id: string;
  createdAt: string;
  startDate: string;
  goal: string;
  mode: 'daysOfWeek' | 'sessionsPerWeek';
  daysOfWeek: number[];
  sessionsPerWeek: number;
  preferredTimeOfDay: 'morning' | 'noon' | 'evening' | '';
  startWeightKg: number | null;
}

export interface TrainingPlanListResponse {
  list: TrainingPlanSummary[];
  total: number;
  page: number;
  limit: number;
}

class TrainingPlanApi {
  async create(dto: CreateTrainingPlanDto): Promise<TrainingPlanResponse> {
    return api.post<TrainingPlanResponse>('/training-plans', dto);
  }

  async list(page: number = 1, limit: number = 10): Promise<TrainingPlanListResponse> {
    return api.get<TrainingPlanListResponse>(`/training-plans?page=${page}&limit=${limit}`);
  }

  async detail(id: string): Promise<TrainingPlanResponse> {
    return api.get<TrainingPlanResponse>(`/training-plans/${id}`);
  }

  async update(id: string, dto: CreateTrainingPlanDto): Promise<TrainingPlanResponse> {
    return api.post<TrainingPlanResponse>(`/training-plans/${id}/update`, dto);
  }

  async delete(id: string): Promise<{ success: boolean }> {
    return api.delete<{ success: boolean }>(`/training-plans/${id}`);
  }

  async activate(id: string): Promise<{ success: boolean }> {
    return api.post<{ success: boolean }>(`/training-plans/${id}/activate`);
  }
}

export const trainingPlanApi = new TrainingPlanApi();