import { Users, UserCheck, UserMinus, Clock, BarChart3, TrendingUp } from 'lucide-react';

import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card';

import { createMemberManagementApi } from '@kit/member-management/api';

import { CmsPageShell } from '~/components/cms-page-shell';
import { StatsCard } from '~/components/stats-card';

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

export default async function MemberStatisticsPage({ 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 <div>Konto nicht gefunden</div>;

  const api = createMemberManagementApi(client);
  const stats = await api.getMemberStatistics(acct.id);

  return (
    <CmsPageShell account={account} title="Mitglieder-Statistiken">
      <div className="flex w-full flex-col gap-6">
        <div>
          <h1 className="text-2xl font-bold">Mitglieder-Statistiken</h1>
          <p className="text-muted-foreground">Übersicht über Ihre Mitglieder</p>
        </div>

        <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
          <StatsCard
            title="Gesamt"
            value={stats.total ?? 0}
            icon={<Users className="h-5 w-5" />}
          />
          <StatsCard
            title="Aktiv"
            value={stats.active ?? 0}
            icon={<UserCheck className="h-5 w-5" />}
          />
          <StatsCard
            title="Inaktiv"
            value={stats.inactive ?? 0}
            icon={<UserMinus className="h-5 w-5" />}
          />
          <StatsCard
            title="Ausstehend"
            value={stats.pending ?? 0}
            icon={<Clock className="h-5 w-5" />}
          />
        </div>

        {/* Chart Placeholders */}
        <Card>
          <CardHeader>
            <CardTitle className="flex items-center gap-2">
              <BarChart3 className="h-5 w-5" />
              Mitgliederentwicklung
            </CardTitle>
          </CardHeader>
          <CardContent>
            <div className="flex h-64 items-center justify-center rounded-md border border-dashed text-muted-foreground">
              Diagramm wird hier angezeigt
            </div>
          </CardContent>
        </Card>

        <Card>
          <CardHeader>
            <CardTitle className="flex items-center gap-2">
              <TrendingUp className="h-5 w-5" />
              Eintritte / Austritte pro Monat
            </CardTitle>
          </CardHeader>
          <CardContent>
            <div className="flex h-64 items-center justify-center rounded-md border border-dashed text-muted-foreground">
              Diagramm wird hier angezeigt
            </div>
          </CardContent>
        </Card>
      </div>
    </CmsPageShell>
  );
}
