import { useCallback, useState } from "react";
import { api } from "@/lib/api";
import { ENDPOINTS } from "@/constants/endpoints";
import type {
  ApiResponse,
  MarketPriceActionResponse,
  MarketPriceLatestResponse,
  MarketPriceLiveModeStartPayload,
} from "@/types";

export function useMarketPrices() {
  const [marketPrices, setMarketPrices] = useState<MarketPriceLatestResponse | null>(null);
  const [isLoadingMarketPrices, setIsLoadingMarketPrices] = useState(false);
  const [marketPriceError, setMarketPriceError] = useState<string | null>(null);

  const fetchMarketPrices = useCallback(async () => {
    setIsLoadingMarketPrices(true);
    setMarketPriceError(null);
    try {
      const response: ApiResponse<MarketPriceLatestResponse> = await api.get(ENDPOINTS.MARKET_PRICES.LATEST);
      if (response.success && response.data) {
        setMarketPrices(response.data);
        return response.data;
      }
      throw new Error(response.message || "Failed to load market prices");
    } catch (err) {
      setMarketPriceError(err instanceof Error ? err.message : "Failed to load market prices");
      return null;
    } finally {
      setIsLoadingMarketPrices(false);
    }
  }, []);

  const refreshMarketPrices = useCallback(async () => {
    setIsLoadingMarketPrices(true);
    setMarketPriceError(null);
    try {
      const response: MarketPriceActionResponse = await api.post(ENDPOINTS.MARKET_PRICES.REFRESH);
      if (response.data) {
        setMarketPrices(response.data);
      }
      if (!response.success) {
        setMarketPriceError(response.detail || response.message);
      }
      return response;
    } catch (err) {
      const message = err instanceof Error ? err.message : "Failed to refresh market prices";
      setMarketPriceError(message);
      return null;
    } finally {
      setIsLoadingMarketPrices(false);
    }
  }, []);

  const startLiveMode = useCallback(async (payload: MarketPriceLiveModeStartPayload) => {
    setIsLoadingMarketPrices(true);
    setMarketPriceError(null);
    try {
      const response: MarketPriceActionResponse = await api.post(ENDPOINTS.MARKET_PRICES.LIVE_MODE_START, payload);
      if (response.data) setMarketPrices(response.data);
      if (!response.success) setMarketPriceError(response.detail || response.message);
      return response;
    } catch (err) {
      const message = err instanceof Error ? err.message : "Failed to start live mode";
      setMarketPriceError(message);
      return null;
    } finally {
      setIsLoadingMarketPrices(false);
    }
  }, []);

  const stopLiveMode = useCallback(async () => {
    setIsLoadingMarketPrices(true);
    setMarketPriceError(null);
    try {
      const response: MarketPriceActionResponse = await api.post(ENDPOINTS.MARKET_PRICES.LIVE_MODE_STOP);
      if (response.data) setMarketPrices(response.data);
      if (!response.success) setMarketPriceError(response.detail || response.message);
      return response;
    } catch (err) {
      const message = err instanceof Error ? err.message : "Failed to stop live mode";
      setMarketPriceError(message);
      return null;
    } finally {
      setIsLoadingMarketPrices(false);
    }
  }, []);

  return {
    marketPrices,
    isLoadingMarketPrices,
    marketPriceError,
    fetchMarketPrices,
    refreshMarketPrices,
    startLiveMode,
    stopLiveMode,
  };
}
