// frontend/lib/hooks/useAutomation.ts
"use client";
import useSWR from "swr";
import type { AutomationPage, RuleCreate } from "@/lib/types";
import { automationApi } from "@/lib/automation/api";

export function useAutomation() {
  const { data, error, isLoading, mutate } = useSWR<AutomationPage>(
    "automation",
    () => automationApi.page(),
    { revalidateOnFocus: false },
  );

  const parse = (text: string) => automationApi.parse(text);
  const createRule = async (b: RuleCreate) => {
    await automationApi.createRule(b);
    await mutate();
  };
  const toggle = async (id: string) => {
    await automationApi.toggle(id);
    await mutate();
  };
  const remove = async (id: string) => {
    await automationApi.remove(id);
    await mutate();
  };
  const accept = async (id: string) => {
    await automationApi.accept(id);
    await mutate();
  };
  const dismiss = async (id: string) => {
    await automationApi.dismiss(id);
    await mutate();
  };
  return { page: data, error, isLoading, parse, createRule, toggle, remove, accept, dismiss, reload: mutate };
}
