import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { createMemberManagementApi } from '@kit/member-management/api';
import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card';
import { Button } from '@kit/ui/button';
import { Badge } from '@kit/ui/badge';
import { CreditCard, Download } from 'lucide-react';
import { CmsPageShell } from '~/components/cms-page-shell';
import { EmptyState } from '~/components/empty-state';

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

export default async function MemberCardsPage({ params }: Props) {
  const { account } = await params;
  const client = getSupabaseServerClient();
  const { data: acct } = await client.from('accounts').select('id').eq('slug', account).single();
  if (!acct) return <div>Konto nicht gefunden</div>;

  const api = createMemberManagementApi(client);
  const result = await api.listMembers(acct.id, { status: 'active', pageSize: 100 });
  const members = result.data;

  return (
    <CmsPageShell account={account} title="Mitgliedsausweise" description="Ausweise erstellen und verwalten">
      <div className="space-y-6">
        <div className="flex items-center justify-between">
          <p className="text-sm text-muted-foreground">{members.length} aktive Mitglieder</p>
          <Button disabled>
            <Download className="mr-2 h-4 w-4" />
            Alle Ausweise generieren (PDF)
          </Button>
        </div>

        {members.length === 0 ? (
          <EmptyState
            icon={<CreditCard className="h-8 w-8" />}
            title="Keine aktiven Mitglieder"
            description="Erstellen Sie zuerst Mitglieder, um Ausweise zu generieren."
            actionLabel="Mitglieder verwalten"
            actionHref={`/home/${account}/members-cms`}
          />
        ) : (
          <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
            {members.map((m: Record<string, unknown>) => (
              <Card key={String(m.id)}>
                <CardContent className="p-4">
                  <div className="flex items-center justify-between">
                    <div>
                      <p className="font-semibold">{String(m.last_name)}, {String(m.first_name)}</p>
                      <p className="text-xs text-muted-foreground">Nr. {String(m.member_number ?? '—')}</p>
                    </div>
                    <Badge variant="default">Aktiv</Badge>
                  </div>
                  <div className="mt-3 flex gap-2">
                    <Button size="sm" variant="outline" disabled>
                      <CreditCard className="mr-1 h-3 w-3" />
                      Ausweis
                    </Button>
                  </div>
                </CardContent>
              </Card>
            ))}
          </div>
        )}

        <Card>
          <CardHeader>
            <CardTitle>PDF-Generierung</CardTitle>
          </CardHeader>
          <CardContent>
            <p className="text-sm text-muted-foreground">
              Die PDF-Generierung erfordert die Installation von <code>@react-pdf/renderer</code>.
              Nach der Installation können Mitgliedsausweise einzeln oder als Stapel erstellt werden.
            </p>
          </CardContent>
        </Card>
      </div>
    </CmsPageShell>
  );
}
