import Styles from "@/constants/Styles";
import { apiGetDataHome } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { useTheme } from "@/providers/ThemeProvider";
import { Feather } from "@expo/vector-icons";
import { useQuery } from "@tanstack/react-query";
import { router } from "expo-router";
import React from "react";
import { Dimensions, Pressable, View } from "react-native";
import { ICarouselInstance } from "react-native-reanimated-carousel";
import Skeleton from "../skeleton";
import Text from "../Text";

type Props = {
   id: string
   name: string
   jumlah: number
}

export default function DivisionHome({ refreshing }: { refreshing: boolean }) {
   const { decryptToken, token } = useAuthSession()
   const { colors } = useTheme();
   const ref = React.useRef<ICarouselInstance>(null)
   const width = Dimensions.get("window").width
   const arrSkeleton = Array.from({ length: 2 }, (_, index) => index)

   // TanStack Query for Division data
   const { data: homeDivisions = [], isLoading } = useQuery({
      queryKey: ['homeData', 'division'],
      queryFn: async () => {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetDataHome({ cat: "division", user: hasil })
         return response.data as Props[]
      },
      enabled: !!token?.current,
      staleTime: 0,
   })

   return (
      <View style={[Styles.mb15]}>
         <Text style={[Styles.textDefaultSemiBold, Styles.mb10]}>Divisi Teraktif</Text>
         {
            isLoading ?
               arrSkeleton.map((item, index) => (
                  <Skeleton key={index} width={100} height={80} borderRadius={10} widthType="percent" />
               ))
               :
               homeDivisions.length > 0 ?
                  homeDivisions.map((item, index) => (
                     <Pressable style={[Styles.wrapPaper, Styles.mb05, { backgroundColor: colors.card, borderColor: colors.icon + '20' }]} key={index} onPress={() => { router.push(`/division/${item.id}`) }}>
                        <View style={[Styles.rowSpaceBetween, { alignItems: 'center' }]}>
                           <View>
                              <Text style={[Styles.textDefaultSemiBold]}>{item.name?.charAt(0).toUpperCase() + item.name?.slice(1)}</Text>
                              <Text style={[Styles.textDefault]}>{item.jumlah} Kegiatan</Text>
                           </View>
                           <Feather name="chevron-right" size={20} color={colors.text} />
                        </View>
                     </Pressable>
                  ))
                  // <Carousel
                  //    ref={ref}
                  //    style={{ width: "100%" }}
                  //    width={width * 0.8}
                  //    height={235}
                  //    data={data}
                  //    loop={true}
                  //    autoPlay={false}
                  //    autoPlayReverse={false}
                  //    pagingEnabled={true}
                  //    snapEnabled={true}
                  //    vertical={false}
                  //    renderItem={({ index }) => (
                  //       <PaperGridContent titleTail={1} onPress={() => { router.push(`/division/${data[index].id}`) }} content="carousel" title={data[index].name} headerColor="warning">
                  //          <View>
                  //             <Text style={{ fontSize: 50, textAlign: "center", fontWeight: 'bold' }}>{data[index].jumlah}</Text>
                  //             <Text style={[Styles.textSubtitle, { textAlign: "center" }]}>KEGIATAN</Text>
                  //          </View>
                  //       </PaperGridContent>
                  //    )}
                  // />
                  :
                  <Text style={[Styles.textDefault, { textAlign: 'center', color: colors.dimmed }]}>Tidak ada data</Text>
         }
      </View>
   )
}