import { root } from '@repo/core'
import { AnnouncementController, type UnreadAnnouncementCountResult } from '@repo/sdk'
import { useState } from 'react'

export const useAnnouncementUnreadCount = () => {
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState<Error | null>(null)
  const [data, setData] = useState<UnreadAnnouncementCountResult>()

  const refetch = async () => {
    setLoading(true)
    setError(null)
    try {
      const announcementController = root.get(AnnouncementController)
      const result = await announcementController.getUnreadCount()
      setData(result)
    } catch (err) {
      setError(err as Error)
    } finally {
      setLoading(false)
    }
  }

  return {
    data,
    loading,
    error,
    refetch,
  }
}
