"use client";

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useDataProvider, useQueryKeyPrefix } from '@/lib/demo/data-provider-context';
import { trackEvent, AnalyticsEvents } from '@/lib/analytics';
import type { CreateContractorRequest, UpdateContractorRequest } from '@/lib/api/contractors';

// ---------------------------------------------------------------------------
// Query hooks
// ---------------------------------------------------------------------------

export function useContractors() {
  const { contractors } = useDataProvider();
  const qk = useQueryKeyPrefix();
  return useQuery({
    queryKey: qk('contractors'),
    queryFn: () => contractors.list(),
    select: (data) => (Array.isArray(data) ? data : []),
  });
}

export function useContractor(id: number) {
  const { contractors } = useDataProvider();
  const qk = useQueryKeyPrefix();
  return useQuery({
    queryKey: qk('contractors', id),
    queryFn: () => contractors.get(id),
    enabled: !!id,
  });
}

export function useContractorTasks(id: number) {
  const { contractors } = useDataProvider();
  const qk = useQueryKeyPrefix();
  return useQuery({
    queryKey: qk('contractors', id, 'tasks'),
    queryFn: () => contractors.getTasks(id),
    enabled: !!id,
    select: (data) => (Array.isArray(data) ? data : []),
  });
}

// ---------------------------------------------------------------------------
// Mutation hooks
// ---------------------------------------------------------------------------

export function useCreateContractor() {
  const queryClient = useQueryClient();
  const { contractors } = useDataProvider();
  const qk = useQueryKeyPrefix();
  return useMutation({
    mutationFn: (data: CreateContractorRequest) =>
      contractors.create(data),
    onSuccess: () => {
      trackEvent(AnalyticsEvents.CONTRACTOR_CREATED, { platform: 'web' });
      queryClient.invalidateQueries({ queryKey: qk('contractors') });
    },
  });
}

export function useUpdateContractor(id: number) {
  const queryClient = useQueryClient();
  const { contractors } = useDataProvider();
  const qk = useQueryKeyPrefix();
  return useMutation({
    mutationFn: (data: UpdateContractorRequest) =>
      contractors.update(id, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: qk('contractors') });
      queryClient.invalidateQueries({ queryKey: qk('contractors', id) });
    },
  });
}

export function useDeleteContractor() {
  const queryClient = useQueryClient();
  const { contractors } = useDataProvider();
  const qk = useQueryKeyPrefix();
  return useMutation({
    mutationFn: (id: number) => contractors.delete(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: qk('contractors') });
    },
  });
}

export function useToggleFavorite() {
  const queryClient = useQueryClient();
  const { contractors } = useDataProvider();
  const qk = useQueryKeyPrefix();
  return useMutation({
    mutationFn: (id: number) => contractors.toggleFavorite(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: qk('contractors') });
    },
  });
}
