import { useState, useEffect, useCallback } from "react";
import {
  type DirectiveOrderGroup,
  type CreateDOGRequest,
  type UpdateDOGRequest,
  listDogs,
  createDog,
  getDog,
  updateDog as updateDogApi,
  deleteDog as deleteDogApi,
  pickUpDogOrders as pickUpDogOrdersApi,
} from "../lib/api";

export function useDogs(directiveId: string | undefined) {
  const [dogs, setDogs] = useState<DirectiveOrderGroup[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const refresh = useCallback(async () => {
    if (!directiveId) return;
    try {
      setLoading(true);
      setError(null);
      const res = await listDogs(directiveId);
      setDogs(res.dogs);
    } catch (e) {
      setError(e instanceof Error ? e.message : "Failed to load DOGs");
    } finally {
      setLoading(false);
    }
  }, [directiveId]);

  useEffect(() => {
    refresh();
  }, [refresh]);

  const create = useCallback(async (req: CreateDOGRequest) => {
    if (!directiveId) return null;
    const dog = await createDog(directiveId, req);
    await refresh();
    return dog;
  }, [directiveId, refresh]);

  const update = useCallback(async (dogId: string, req: UpdateDOGRequest) => {
    if (!directiveId) return;
    await updateDogApi(directiveId, dogId, req);
    await refresh();
  }, [directiveId, refresh]);

  const remove = useCallback(async (dogId: string) => {
    if (!directiveId) return;
    await deleteDogApi(directiveId, dogId);
    await refresh();
  }, [directiveId, refresh]);

  const pickUpOrders = useCallback(async (dogId: string) => {
    if (!directiveId) return null;
    const result = await pickUpDogOrdersApi(directiveId, dogId);
    await refresh();
    return result;
  }, [directiveId, refresh]);

  return { dogs, loading, error, refresh, create, update, remove, pickUpOrders };
}

export function useDog(directiveId: string | undefined, dogId: string | undefined) {
  const [dog, setDog] = useState<DirectiveOrderGroup | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const refresh = useCallback(async () => {
    if (!directiveId || !dogId) return;
    try {
      setLoading(true);
      setError(null);
      const d = await getDog(directiveId, dogId);
      setDog(d);
    } catch (e) {
      setError(e instanceof Error ? e.message : "Failed to load DOG");
    } finally {
      setLoading(false);
    }
  }, [directiveId, dogId]);

  useEffect(() => {
    setDog(null);
    setError(null);
    setLoading(true);
    refresh();
  }, [directiveId, dogId]); // eslint-disable-line react-hooks/exhaustive-deps

  const update = useCallback(async (req: UpdateDOGRequest) => {
    if (!directiveId || !dogId) return;
    const d = await updateDogApi(directiveId, dogId, req);
    setDog(d);
    return d;
  }, [directiveId, dogId]);

  const remove = useCallback(async () => {
    if (!directiveId || !dogId) return;
    await deleteDogApi(directiveId, dogId);
  }, [directiveId, dogId]);

  const pickUpOrders = useCallback(async () => {
    if (!directiveId || !dogId) return null;
    const result = await pickUpDogOrdersApi(directiveId, dogId);
    await refresh();
    return result;
  }, [directiveId, dogId, refresh]);

  return { dog, loading, error, refresh, update, remove, pickUpOrders };
}
