'use client';

import { useEffect, useState } from 'react';
import dynamic from 'next/dynamic';
import { ApexOptions } from 'apexcharts';
import { useTheme } from 'next-themes';
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";

// Dynamically import ApexCharts to avoid SSR issues
const Chart = dynamic(() => import('react-apexcharts'), { ssr: false });

interface JenisPendaftaranData {
  tahun_angkatan: number;
  jenis_pendaftaran: string;
  jumlah: number;
}

interface Props {
  tahunAngkatan: string;
}

export default function JenisPendaftaranPerAngkatanChart({ tahunAngkatan }: Props) {
  const { theme } = useTheme();
  const [mounted, setMounted] = useState(false);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [data, setData] = useState<JenisPendaftaranData[]>([]);
  const [series, setSeries] = useState<number[]>([]);
  const [options, setOptions] = useState<ApexOptions>({
    chart: {
      type: 'pie',
      toolbar: {
        show: true,
        tools: {
          download: true,
          selection: true,
          zoom: true,
          zoomin: true,
          zoomout: true,
          pan: true,
          reset: true
        }
      }
    },
    labels: [],
    colors: ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6', '#EC4899', '#06B6D4', '#F97316'],
    legend: {
      position: 'bottom',
      fontSize: '14px',
      markers: {
        size: 12,
        strokeWidth: 0
      },
      itemMargin: {
        horizontal: 10
      },
      labels: {
        colors: theme === 'dark' ? '#fff' : '#000'
      }
    },
    dataLabels: {
      enabled: true,
      formatter: function (val: number, opts: any) {
        return `${val.toFixed(0)}%`;
      },
      style: {
        fontSize: '14px',
        fontFamily: 'Inter, sans-serif',
        fontWeight: '500'
      },
      offsetY: 0,
      dropShadow: {
        enabled: false
      }
    },
    tooltip: {
      theme: theme === 'dark' ? 'dark' : 'light',
      y: {
        formatter: function (val: number) {
          return val + " mahasiswa"
        }
      }
    }
  });

  // Update theme when it changes
  useEffect(() => {
    setOptions(prev => ({
      ...prev,
      chart: {
        ...prev.chart,
        background: theme === 'dark' ? '#0F172B' : '#fff',
      },
      legend: {
        ...prev.legend,
        labels: {
          ...prev.legend?.labels,
          colors: theme === 'dark' ? '#fff' : '#000'
        }
      },
      tooltip: {
        ...prev.tooltip,
        theme: theme === 'dark' ? 'dark' : 'light'
      }
    }));
  }, [theme]);
  
  // Update dataLabels formatter when data changes
  useEffect(() => {
    if (data.length > 0) {
      setOptions(prev => ({
        ...prev,
        dataLabels: {
          ...prev.dataLabels,
          formatter: function(val: number, opts: any) {
            // Calculate the percentage based on the current data
            const total = data.reduce((sum, item) => sum + item.jumlah, 0);
            const seriesIndex = opts.seriesIndex;
            const jenisPendaftaran = prev.labels?.[seriesIndex];
            
            if (jenisPendaftaran) {
              const item = data.find(d => d.jenis_pendaftaran === jenisPendaftaran);
              const jumlah = item ? item.jumlah : 0;
              const percentage = (jumlah / total) * 100;
              return `${percentage.toFixed(0)}%`;
            }
            return `${val.toFixed(0)}%`;
          }
        }
      }));
    }
  }, [data]);

  useEffect(() => {
    setMounted(true);
  }, []);

  useEffect(() => {
    const fetchData = async () => {
      try {
        setLoading(true);
        setError(null);
        
        const response = await fetch(`/api/mahasiswa/jenis-pendaftaran?tahun_angkatan=${tahunAngkatan}`);
        
        if (!response.ok) {
          throw new Error(`Failed to fetch data: ${response.status} ${response.statusText}`);
        }
        
        const result = await response.json();
        
        if (!Array.isArray(result)) {
          throw new Error('Invalid data format received from server');
        }

        // Process data for pie chart
        const jenisPendaftaran = [...new Set(result.map(item => item.jenis_pendaftaran))].sort();
        const jumlahData = jenisPendaftaran.map(jenis => {
          const item = result.find(d => d.jenis_pendaftaran === jenis);
          return item ? item.jumlah : 0;
        });

        setSeries(jumlahData);
        setOptions(prev => ({
          ...prev,
          labels: jenisPendaftaran,
        }));
        
        // Store processed data
        setData(result);
      } catch (err) {
        console.error('Error in fetchData:', err);
        setError(err instanceof Error ? err.message : 'An error occurred while fetching data');
      } finally {
        setLoading(false);
      }
    };

    if (tahunAngkatan) {
      fetchData();
    }
  }, [tahunAngkatan]);

  if (!mounted) {
    return null;
  }

  if (loading) {
    return (
      <Card className="bg-white dark:bg-slate-900 shadow-lg">
        <CardHeader>
          <CardTitle className="text-xl font-bold dark:text-white">
            Loading...
          </CardTitle>
        </CardHeader>
      </Card>
    );
  }

  if (error) {
    return (
      <Card className="bg-white dark:bg-slate-900 shadow-lg">
        <CardHeader>
          <CardTitle className="text-xl font-bold text-red-500 dark:text-red-400">
            Error: {error}
          </CardTitle>
        </CardHeader>
      </Card>
    );
  }

  if (data.length === 0) {
    return (
      <Card className="bg-white dark:bg-slate-900 shadow-lg">
        <CardHeader>
          <CardTitle className="text-xl font-bold dark:text-white">
            Tidak ada data yang tersedia
          </CardTitle>
        </CardHeader>
      </Card>
    );
  }

  const totalMahasiswa = data.reduce((sum, item) => sum + item.jumlah, 0);

  return (
    <Card className="bg-white dark:bg-slate-900 shadow-lg">
      <CardHeader>
        <CardTitle className="text-xl font-bold dark:text-white">
          Jenis Pendaftaran Angkatan {tahunAngkatan}
        </CardTitle>
      </CardHeader>
      <CardContent>
        <div className="h-[300px] w-full max-w-5xl mx-auto">
          <Chart
            options={options}
            series={series}
            type="pie"
            height="100%"
            width="100%"
          />
        </div>
      </CardContent>
    </Card>
  );
} 