import { useApi } from './useApi'
import { DEFAULT_PAGE_SIZE } from '@/types/pagination'
import type { ApiNotificationList, UnreadCountResult } from '@/types/notification'

// 알림 도메인 API Composable — 인터셉터가 success/data 를 언래핑
// (실시간 스트림 SSE 는 store 에서 EventSource 로 직접 다룬다 — axios 비대상)
export function useNotification() {
  const api = useApi()

  // 내 알림 목록(페이지네이션 + 미읽음 수)
  async function list(
    page = 1,
    size = DEFAULT_PAGE_SIZE,
  ): Promise<ApiNotificationList> {
    return (await api.get('/notifications', {
      params: { page, size },
    })) as unknown as ApiNotificationList
  }

  // 단건 읽음 처리
  async function markRead(id: string): Promise<UnreadCountResult> {
    return (await api.post(
      `/notifications/${id}/read`,
    )) as unknown as UnreadCountResult
  }

  // 전체 읽음 처리
  async function markAllRead(): Promise<UnreadCountResult> {
    return (await api.post(
      '/notifications/read-all',
    )) as unknown as UnreadCountResult
  }

  return { list, markRead, markAllRead }
}
