/**
 * Notification hooks: list, unread count, mark read, delete, types, preferences.
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { apiGet, apiPatch, apiDelete } from './client';
import { PaginatedResponse } from './types';

// ── Types matching backend schemas ──

export interface NotificationItem {
  id: string;
  type: string;
  title: string;
  body?: string | null;
  read_at?: string | null;
  created_at?: string | null;
}

export interface NotificationTypeItem {
  type_key: string;
  plugin_name: string;
  category: string;
  label: string;
  description: string | null;
  is_enabled_by_default: boolean;
  is_enabled: boolean;
}

export interface UnreadCountResponse {
  count: number;
}

export interface NotificationPreferenceItem {
  type_key: string;
  is_enabled: boolean;
}

export function useNotifications() {
  return useQuery({
    queryKey: ['notifications'],
    queryFn: () => apiGet<PaginatedResponse<NotificationItem>>('/notifications'),
  });
}

export function useUnreadNotificationCount() {
  return useQuery({
    queryKey: ['notifications', 'unread-count'],
    queryFn: async () => {
      const data = await apiGet<UnreadCountResponse>('/notifications/unread-count');
      return data.count;
    },
  });
}

export function useMarkNotificationRead() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: string) => apiPatch(`/notifications/${id}/read`),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['notifications'] });
    },
  });
}

export function useDeleteNotification() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: string) => apiDelete(`/notifications/${id}`),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['notifications'] });
    },
  });
}

export function useNotificationTypes() {
  return useQuery({
    queryKey: ['notification-types'],
    queryFn: () => apiGet<{ items: NotificationTypeItem[] }>('/notifications/types'),
  });
}

export function useNotificationPreferences() {
  return useQuery({
    queryKey: ['notification-preferences'],
    queryFn: () => apiGet<{ items: NotificationPreferenceItem[] }>('/notifications/preferences'),
  });
}

export function useUpdateNotificationPreference() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ typeKey, isEnabled }: { typeKey: string; isEnabled: boolean }) =>
      apiPatch(`/notifications/preferences/${typeKey}`, { is_enabled: isEnabled }),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['notification-preferences'] });
      queryClient.invalidateQueries({ queryKey: ['notification-types'] });
    },
  });
}
