/**
 * React Query hooks for Sustainability API
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
  getSustainabilityMetrics,
  getSustainabilityWidgetData,
  getSDGCompliance,
  getEnvironmentalImpact,
  exportGrantReport
} from '../services/sustainability';
import type {
  SustainabilityMetrics,
  SustainabilityWidgetData,
  SDGCompliance,
  EnvironmentalImpact,
  GrantReport
} from '../types/sustainability';

// Query keys
export const sustainabilityKeys = {
  all: ['sustainability'] as const,
  metrics: (tenantId: string, startDate?: string, endDate?: string) =>
    ['sustainability', 'metrics', tenantId, startDate, endDate] as const,
  widget: (tenantId: string, days: number) =>
    ['sustainability', 'widget', tenantId, days] as const,
  sdg: (tenantId: string) =>
    ['sustainability', 'sdg', tenantId] as const,
  environmental: (tenantId: string, days: number) =>
    ['sustainability', 'environmental', tenantId, days] as const,
};

/**
 * Hook to get comprehensive sustainability metrics
 */
export function useSustainabilityMetrics(
  tenantId: string,
  startDate?: string,
  endDate?: string,
  options?: { enabled?: boolean }
) {
  return useQuery({
    queryKey: sustainabilityKeys.metrics(tenantId, startDate, endDate),
    queryFn: () => getSustainabilityMetrics(tenantId, startDate, endDate),
    enabled: options?.enabled !== false && !!tenantId,
    staleTime: 5 * 60 * 1000, // 5 minutes
    refetchInterval: 10 * 60 * 1000, // Refetch every 10 minutes
  });
}

/**
 * Hook to get sustainability widget data (simplified metrics)
 */
export function useSustainabilityWidget(
  tenantId: string,
  days: number = 30,
  options?: { enabled?: boolean }
) {
  return useQuery({
    queryKey: sustainabilityKeys.widget(tenantId, days),
    queryFn: () => getSustainabilityWidgetData(tenantId, days),
    enabled: options?.enabled !== false && !!tenantId,
    staleTime: 5 * 60 * 1000, // 5 minutes
    refetchInterval: 10 * 60 * 1000, // Refetch every 10 minutes
  });
}

/**
 * Hook to get SDG 12.3 compliance status
 */
export function useSDGCompliance(
  tenantId: string,
  options?: { enabled?: boolean }
) {
  return useQuery({
    queryKey: sustainabilityKeys.sdg(tenantId),
    queryFn: () => getSDGCompliance(tenantId),
    enabled: options?.enabled !== false && !!tenantId,
    staleTime: 10 * 60 * 1000, // 10 minutes
  });
}

/**
 * Hook to get environmental impact data
 */
export function useEnvironmentalImpact(
  tenantId: string,
  days: number = 30,
  options?: { enabled?: boolean }
) {
  return useQuery({
    queryKey: sustainabilityKeys.environmental(tenantId, days),
    queryFn: () => getEnvironmentalImpact(tenantId, days),
    enabled: options?.enabled !== false && !!tenantId,
    staleTime: 5 * 60 * 1000, // 5 minutes
  });
}

/**
 * Hook to export grant report
 */
export function useExportGrantReport() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({
      tenantId,
      grantType,
      startDate,
      endDate
    }: {
      tenantId: string;
      grantType?: string;
      startDate?: string;
      endDate?: string;
    }) => exportGrantReport(tenantId, grantType, startDate, endDate),
    onSuccess: () => {
      // Optionally invalidate related queries
      queryClient.invalidateQueries({ queryKey: sustainabilityKeys.all });
    },
  });
}
