import { customerApi } from "@/lib/api/customer";
import { requireServerToken } from "@/lib/auth/server";
import type { ApiAddress } from "@/lib/api/types";
import AddressesManager from "./AddressesManager";

async function loadAddresses(token: string): Promise<ApiAddress[]> {
  try {
    const res = await customerApi.addresses(token);
    return res.data;
  } catch {
    return [];
  }
}

/**
 * Thin server shell over the interactive address book.
 *
 * Only the first read moves to the server — that is what kills the
 * hydrate-then-fetch waterfall and the "Loading addresses…" spinner. The form,
 * inline edit and pincode lookup below share too much state to be worth
 * splitting, so they stay client-side and receive the list already resolved.
 */
export default async function AddressesPage() {
  const token = await requireServerToken("/profile/addresses");
  const addresses = await loadAddresses(token);

  return <AddressesManager initialList={addresses} />;
}
