"use client"

import { Wallet } from "lucide-react"

import { ListPageCard } from "@/app/dashboard/_components"
import { CashBalanceLogTable } from "@/components/xml-apis/cash-balance-log/components/cash-balance-log-table"
import { CashBalanceLogToolbar } from "@/components/xml-apis/cash-balance-log/components/cash-balance-log-toolbar"
import { useCashBalanceLog } from "@/components/xml-apis/cash-balance-log/hooks/use-cash-balance-log"
import { XmlApiErrorBanner } from "@/components/xml-apis/shared/xml-api-error-banner"
import type { CashBalanceLogRow } from "@/components/xml-apis/cash-balance-log/types"

type CashBalanceLogListPageProps = {
  initialData?: CashBalanceLogRow[]
  initialTotalCount?: number
  initialPage?: number
  initialPageSize?: number
  initialPageCount?: number
  initialErrorMessage?: string | null
}

export function CashBalanceLogListPage({
  initialData = [],
  initialTotalCount = 0,
  initialPage = 1,
  initialPageSize = 10,
  initialPageCount = 1,
  initialErrorMessage = null,
}: CashBalanceLogListPageProps) {
  const {
    data,
    totalCount,
    page,
    pageSize,
    pageCount,
    isLoading,
    isRefreshing,
    refresh,
    goToPage,
    changePageSize,
  } = useCashBalanceLog({
    initialData,
    initialTotalCount,
    initialPage,
    initialPageSize,
    initialPageCount,
  })

  return (
    <ListPageCard
      icon={Wallet}
      title="Cash Balance Log"
      description="Review cash balance fetch results by API, bank, and process."
      breadcrumb={[{ label: "XML API" }, { label: "Cash Balance Log" }]}
      actions={
        <CashBalanceLogToolbar
          isRefreshing={isRefreshing}
          onRefresh={() => void refresh()}
        />
      }
    >
      <XmlApiErrorBanner message={initialErrorMessage} />
      <CashBalanceLogTable
        data={data}
        totalCount={totalCount}
        page={page}
        pageSize={pageSize}
        pageCount={pageCount}
        isLoading={isLoading || isRefreshing}
        onPageChange={goToPage}
        onPageSizeChange={changePageSize}
      />
    </ListPageCard>
  )
}
