import { apiClient } from './api-client';
import type { z } from 'zod/v4';
import type {
  RegimenResponseSchema,
  RegimenListResponseSchema,
  BurnRateResponseSchema,
  CreateRegimenSchema,
  UpdateRegimenSchema,
} from '@meshitrack/shared';

type RegimenResponse = z.infer<typeof RegimenResponseSchema>;
type RegimenListResponse = z.infer<typeof RegimenListResponseSchema>;
type BurnRateResponse = z.infer<typeof BurnRateResponseSchema>;
type CreateRegimenInput = z.infer<typeof CreateRegimenSchema>;
type UpdateRegimenInput = z.infer<typeof UpdateRegimenSchema>;

export async function listRegimens(
  householdId: string,
  query?: { isActive?: boolean; cursor?: string; limit?: number },
): Promise<RegimenListResponse> {
  const params = new URLSearchParams();
  if (query?.isActive !== undefined) params.set('isActive', String(query.isActive));
  if (query?.cursor) params.set('cursor', query.cursor);
  if (query?.limit) params.set('limit', String(query.limit));
  const qs = params.toString();
  return apiClient.get<RegimenListResponse>(
    `/households/${householdId}/regimens${qs ? `?${qs}` : ''}`,
  );
}

export async function getRegimen(householdId: string, id: string): Promise<RegimenResponse> {
  return apiClient.get<RegimenResponse>(`/households/${householdId}/regimens/${id}`);
}

export async function getBurnRates(householdId: string): Promise<BurnRateResponse> {
  return apiClient.get<BurnRateResponse>(`/households/${householdId}/regimens/burn-rate`);
}

export async function createRegimen(
  householdId: string,
  data: CreateRegimenInput,
): Promise<RegimenResponse> {
  return apiClient.post<RegimenResponse>(`/households/${householdId}/regimens`, data);
}

export async function updateRegimen(
  householdId: string,
  id: string,
  data: UpdateRegimenInput,
): Promise<RegimenResponse> {
  return apiClient.patch<RegimenResponse>(`/households/${householdId}/regimens/${id}`, data);
}

export async function deleteRegimen(householdId: string, id: string): Promise<void> {
  return apiClient.delete<void>(`/households/${householdId}/regimens/${id}`);
}
