import { apiFetch } from './client'

export interface Household {
  id: string
  name: string
  invite_code: string
  created_at: string
  members: HouseholdMember[]
}

export interface HouseholdMember {
  user_id: string
  email: string
  display_name: string
  avatar_url?: string
  role: 'owner' | 'member'
  joined_at: string
}

export function getMyHousehold() {
  return apiFetch<{ success: boolean; data: Household }>('/households/mine')
}

export function createHousehold(name: string) {
  return apiFetch<{ success: boolean; data: Household }>('/households', {
    method: 'POST',
    body: JSON.stringify({ name }),
  })
}

export function joinHousehold(inviteCode: string) {
  return apiFetch<{ success: boolean; data: Household }>('/households/join', {
    method: 'POST',
    body: JSON.stringify({ inviteCode }),
  })
}

export function leaveHousehold(id: string) {
  return apiFetch<{ success: boolean }>(`/households/${id}/leave`, {
    method: 'DELETE',
  })
}

export function regenerateInviteCode(id: string) {
  return apiFetch<{ success: boolean; data: { invite_code: string } }>(
    `/households/${id}/invite`,
    { method: 'POST' }
  )
}
