import { apiClient } from './api-client';
import type { z } from 'zod/v4';
import type {
  StoreResponseSchema,
  StoreListResponseSchema,
  CreateStoreSchema,
  UpdateStoreSchema,
} from '@meshitrack/shared';

type StoreResponse = z.infer<typeof StoreResponseSchema>;
type StoreListResponse = z.infer<typeof StoreListResponseSchema>;
type CreateStoreInput = z.infer<typeof CreateStoreSchema>;
type UpdateStoreInput = z.infer<typeof UpdateStoreSchema>;

export async function listStores(
  householdId: string,
  query?: { tags?: string; search?: string; cursor?: string; limit?: number },
): Promise<StoreListResponse> {
  const params = new URLSearchParams();
  if (query?.tags) params.set('tags', query.tags);
  if (query?.search) params.set('search', query.search);
  if (query?.cursor) params.set('cursor', query.cursor);
  if (query?.limit) params.set('limit', String(query.limit));
  const qs = params.toString();
  return apiClient.get<StoreListResponse>(`/households/${householdId}/stores${qs ? `?${qs}` : ''}`);
}

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

export async function createStore(
  householdId: string,
  data: CreateStoreInput,
): Promise<StoreResponse> {
  return apiClient.post<StoreResponse>(`/households/${householdId}/stores`, data);
}

export async function updateStore(
  householdId: string,
  id: string,
  data: UpdateStoreInput,
): Promise<StoreResponse> {
  return apiClient.patch<StoreResponse>(`/households/${householdId}/stores/${id}`, data);
}

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