import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { createFischereiApi } from '@kit/fischerei/api';
import { FischereiTabNavigation, StockingDataTable } from '@kit/fischerei/components';

import { CmsPageShell } from '~/components/cms-page-shell';
import { AccountNotFound } from '~/components/account-not-found';

interface Props {
  params: Promise<{ account: string }>;
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}

export default async function StockingPage({ params, searchParams }: Props) {
  const { account } = await params;
  const search = await searchParams;
  const client = getSupabaseServerClient();

  const { data: acct } = await client
    .from('accounts')
    .select('id')
    .eq('slug', account)
    .single();

  if (!acct) return <AccountNotFound />;

  const api = createFischereiApi(client);
  const page = Number(search.page) || 1;
  const result = await api.listStocking(acct.id, {
    page,
    pageSize: 25,
  });

  return (
    <CmsPageShell account={account} title="Fischerei - Besatz">
      <FischereiTabNavigation account={account} activeTab="stocking" />
      <StockingDataTable
        data={result.data}
        total={result.total}
        page={page}
        pageSize={25}
        account={account}
      />
    </CmsPageShell>
  );
}
