import Styles from "@/constants/Styles";
import { apiGetDataHome } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { useEffect, useState } from "react";
import { Text, View } from "react-native";
import DiscussionItem from "../discussionItem";

type Props = {
   id: string
   idDivision: string
   desc: string
   title: string
   date: string
   user: string
}

export default function DisccussionHome() {
   const { decryptToken, token } = useAuthSession()
   const [data, setData] = useState<Props[]>([])


   async function handleData() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetDataHome({ cat: "discussion", user: hasil })
         setData(response.data)
      } catch (error) {
         console.error(error)
      }
   }

   useEffect(() => {
      handleData()
   }, []);

   return (
      <View style={[Styles.mb15]}>
         <Text style={[Styles.textDefaultSemiBold, Styles.mv10]}>Diskusi</Text>
         <View style={[Styles.wrapPaper]}>
            {
               data.length > 0 ?
                  data.map((item, index) => {
                     return (
                        <DiscussionItem key={index} title={item.desc} user={item.user} date={item.date} />
                     )
                  })
                  : <Text style={[Styles.textDefault, Styles.cGray, { textAlign: 'center' }]}>Tidak ada acara hari ini</Text>
            }
         </View>
      </View>
   )
}