'use client';
import { useEffect, useState } from 'react';
import dynamic from 'next/dynamic';
import { useLanguage } from '@/lib/i18n/language-context';

const ReportsCharts = dynamic(() => import('./reports-charts'), { ssr: false });

export default function ReportsPage() {
  const [stats, setStats] = useState<any>(null);
  const [loading, setLoading] = useState(true);
  const { t } = useLanguage();

  useEffect(() => {
    fetch('/api/stats')
      .then((r: any) => r?.json?.())
      .then((d: any) => setStats(d ?? {}))
      .catch(() => {})
      .finally(() => setLoading(false));
  }, []);

  if (loading) {
    return (
      <div className="flex items-center justify-center py-20">
        <div className="animate-spin w-8 h-8 border-4 border-red-600 border-t-transparent rounded-full" />
      </div>
    );
  }

  return (
    <div>
      <h1 className="font-display text-2xl font-bold tracking-tight mb-6">{t.admin.reports.title}</h1>
      {stats && <ReportsCharts stats={stats} />}
    </div>
  );
}
