import { getSupabaseClient } from "@/lib/supabase";

const StudentAttendanceCard = async ({ id }: { id: string }) => {
  const supabase = await getSupabaseClient();
  const { data: attendanceData, error } = await supabase
    .from("Attendance")
    .select("present")
    .eq("studentId", id)
    .gte("date", new Date(new Date().getFullYear(), 0, 1).toISOString());

  if (error) {
    console.error("Error fetching student attendance:", error);
  }

  const attendance = attendanceData || [];

  const totalDays = attendance.length;
  const presentDays = attendance.filter((day) => day.present).length;
  const percentage = (presentDays / totalDays) * 100;
  return (
    <div className="">
      <h1 className="text-xl font-semibold">{percentage || "-"}%</h1>
      <span className="text-sm text-gray-400">Attendance</span>
    </div>
  );
};

export default StudentAttendanceCard;
