import { apiRequest } from '../lib/api'

export interface UserAddress {
  id: string
  label: string | null
  province: string
  city: string
  address: string
  postalCode: string | null
  landline: string | null
  createdAt: string
  updatedAt: string
}

export interface UserAddressInput {
  label?: string
  province: string
  city: string
  address: string
  postalCode?: string
  landline?: string
}

export async function listAddresses(signal?: AbortSignal) {
  return apiRequest<{ items: UserAddress[] }>('/auth/addresses', { auth: true, signal })
}

export async function createAddress(input: UserAddressInput) {
  return apiRequest<{ address: UserAddress }>('/auth/addresses', {
    method: 'POST',
    auth: true,
    body: input,
  })
}

export async function updateAddress(addressId: string, input: UserAddressInput) {
  return apiRequest<{ address: UserAddress }>(`/auth/addresses/${addressId}`, {
    method: 'PATCH',
    auth: true,
    body: input,
  })
}

export async function removeAddress(addressId: string) {
  return apiRequest<{ message: string }>(`/auth/addresses/${addressId}`, {
    method: 'DELETE',
    auth: true,
  })
}
