import { useState, useEffect, useCallback } from "react";
import {
  type Order,
  type OrderStatus,
  type OrderType,
  type OrderPriority,
  type CreateOrderRequest,
  type UpdateOrderRequest,
  listOrders,
  createOrder,
  getOrder,
  updateOrder,
  deleteOrder,
  linkOrderToDirective,
  linkOrderToContract,
  convertOrderToStep,
} from "../lib/api";

export function useOrders(
  statusFilter?: OrderStatus,
  typeFilter?: OrderType,
  priorityFilter?: OrderPriority,
) {
  const [orders, setOrders] = useState<Order[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const refresh = useCallback(async () => {
    try {
      setLoading(true);
      setError(null);
      const res = await listOrders(statusFilter, typeFilter, priorityFilter);
      setOrders(res.orders);
    } catch (e) {
      setError(e instanceof Error ? e.message : "Failed to load orders");
    } finally {
      setLoading(false);
    }
  }, [statusFilter, typeFilter, priorityFilter]);

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

  const create = useCallback(async (req: CreateOrderRequest) => {
    const o = await createOrder(req);
    await refresh();
    return o;
  }, [refresh]);

  const remove = useCallback(async (id: string) => {
    await deleteOrder(id);
    await refresh();
  }, [refresh]);

  return { orders, loading, error, refresh, create, remove };
}

export function useOrder(id: string | undefined) {
  const [order, setOrder] = useState<Order | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const refresh = useCallback(async () => {
    if (!id) return;
    try {
      setLoading(true);
      setError(null);
      const o = await getOrder(id);
      setOrder(o);
    } catch (e) {
      setError(e instanceof Error ? e.message : "Failed to load order");
    } finally {
      setLoading(false);
    }
  }, [id]);

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

  const update = useCallback(async (req: UpdateOrderRequest) => {
    if (!id) return;
    const o = await updateOrder(id, req);
    setOrder(o);
    return o;
  }, [id]);

  const remove = useCallback(async () => {
    if (!id) return;
    await deleteOrder(id);
  }, [id]);

  const linkDirective = useCallback(async (directiveId: string) => {
    if (!id) return;
    const o = await linkOrderToDirective(id, directiveId);
    setOrder(o);
    return o;
  }, [id]);

  const linkContract = useCallback(async (contractId: string) => {
    if (!id) return;
    const o = await linkOrderToContract(id, contractId);
    setOrder(o);
    return o;
  }, [id]);

  const convertToStep = useCallback(async (directiveId: string) => {
    if (!id) return;
    const step = await convertOrderToStep(id, directiveId);
    await refresh();
    return step;
  }, [id, refresh]);

  return {
    order, loading, error, refresh,
    update, remove,
    linkDirective, linkContract, convertToStep,
  };
}
