import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { createVerbandApi } from '@kit/verbandsverwaltung/api';
import {
  VerbandTabNavigation,
  SharedTemplates,
} from '@kit/verbandsverwaltung/components';

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

interface PageProps {
  params: Promise<{ account: string }>;
}

export default async function TemplatesPage({ params }: PageProps) {
  const { account } = await params;
  const client = getSupabaseServerClient();

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

  if (!acct) return <AccountNotFound />;

  const api = createVerbandApi(client);
  const templates = await api.listSharedTemplates(acct.id);

  return (
    <CmsPageShell
      account={account}
      title="Verbandsverwaltung - Vorlagen"
      description="Geteilte Vorlagen aus der Verbandshierarchie klonen und verwenden"
    >
      <VerbandTabNavigation account={account} activeTab="templates" />
      <SharedTemplates accountId={acct.id} templates={templates} />
    </CmsPageShell>
  );
}
