import { supabase } from "@/lib/supabase";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Etape } from "@/types/tasks.types";
import { Alert } from "react-native";

export const useTabloEtapes = (tabloId: string | undefined) => {
  return useQuery({
    queryKey: ["tablo-etapes", tabloId],
    queryFn: async () => {
      const { data, error } = await supabase
        .from("tasks")
        .select("*")
        .eq("tablo_id", tabloId!)
        .eq("is_parent", true)
        .is("deleted_at", null)
        .order("position", { ascending: true });

      if (error) throw error;
      return data as Etape[];
    },
    enabled: !!tabloId,
  });
};

export const useCreateEtape = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async ({ tabloId, title, description, position, due_date }: {
      tabloId: string;
      title: string;
      description?: string | null;
      position?: number;
      due_date?: string | null;
    }) => {
      const { data, error } = await supabase
        .from("tasks")
        .insert({
          tablo_id: tabloId,
          title,
          description: description ?? null,
          position: position ?? 0,
          is_parent: true,
          status: "todo",
          due_date: due_date ?? null,
        })
        .select()
        .single();

      if (error) throw error;
      return data;
    },
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["tablo-etapes", variables.tabloId] });
      queryClient.invalidateQueries({ queryKey: ["tasks", "tablo", variables.tabloId] });
    },
    onError: () => {
      Alert.alert("Erreur", "Impossible de créer l'étape.");
    },
  });
};

export const useUpdateEtape = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async ({ id, tabloId, ...updates }: {
      id: string;
      tabloId: string;
      title?: string;
      description?: string | null;
      position?: number;
      due_date?: string | null;
    }) => {
      const { data, error } = await supabase
        .from("tasks")
        .update(updates)
        .eq("id", id)
        .select()
        .single();

      if (error) throw error;
      return data;
    },
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["tablo-etapes", variables.tabloId] });
      queryClient.invalidateQueries({ queryKey: ["tasks", "tablo", variables.tabloId] });
    },
    onError: () => {
      Alert.alert("Erreur", "Impossible de modifier l'étape.");
    },
  });
};

export const useDeleteEtape = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async ({ id, tabloId }: { id: string; tabloId: string }) => {
      const { error } = await supabase.from("tasks").delete().eq("id", id);
      if (error) throw error;
    },
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["tablo-etapes", variables.tabloId] });
      queryClient.invalidateQueries({ queryKey: ["tasks", "tablo", variables.tabloId] });
    },
    onError: () => {
      Alert.alert("Erreur", "Impossible de supprimer l'étape.");
    },
  });
};
