import {
  GraduationCap,
  Users,
  Calendar,
  TrendingUp,
  BarChart3,
} from 'lucide-react';
import { getTranslations } from 'next-intl/server';

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

import { AccountNotFound } from '~/components/account-not-found';
import { CmsPageShell } from '~/components/cms-page-shell';
import { StatsCard } from '~/components/stats-card';
import { StatsBarChart, StatsPieChart } from '~/components/stats-charts';

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

export default async function CourseStatisticsPage({ params }: PageProps) {
  const { account } = await params;
  const client = getSupabaseServerClient();
  const t = await getTranslations('courses');

  const { data: acct } = await client
    .from('accounts')
    .select('id')
    .eq('slug', account)
    .single();
  if (!acct) return <AccountNotFound />;

  const api = createCourseManagementApi(client);
  const stats = await api.getStatistics(acct.id);

  const statusChartData = [
    { name: t('stats.active'), value: stats.openCourses },
    { name: t('stats.completed'), value: stats.completedCourses },
    { name: t('stats.total'), value: stats.totalCourses },
  ];

  return (
    <CmsPageShell account={account} title={t('pages.statisticsTitle')}>
      <div className="flex w-full flex-col gap-6">
        <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
          <StatsCard
            title={t('stats.totalCourses')}
            value={stats.totalCourses}
            icon={<GraduationCap className="h-5 w-5" />}
          />
          <StatsCard
            title={t('stats.activeCourses')}
            value={stats.openCourses}
            icon={<Calendar className="h-5 w-5" />}
          />
          <StatsCard
            title={t('stats.participants')}
            value={stats.totalParticipants}
            icon={<Users className="h-5 w-5" />}
          />
          <StatsCard
            title={t('stats.completed')}
            value={stats.completedCourses}
            icon={<TrendingUp className="h-5 w-5" />}
          />
        </div>

        <div className="grid grid-cols-1 gap-6 lg:grid-cols-2">
          <Card>
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                <BarChart3 className="h-5 w-5" />
                {t('stats.utilization')}
              </CardTitle>
            </CardHeader>
            <CardContent>
              <StatsBarChart data={statusChartData} />
            </CardContent>
          </Card>

          <Card>
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                <TrendingUp className="h-5 w-5" />
                {t('stats.distribution')}
              </CardTitle>
            </CardHeader>
            <CardContent>
              <StatsPieChart data={statusChartData} />
            </CardContent>
          </Card>
        </div>
      </div>
    </CmsPageShell>
  );
}
