import useSWR from 'swr';
import { useState, useCallback } from 'react';
import { getAsset, getAssetSummaryMetrics, getAvailableAssetFacts } from '@alga-psa/assets/actions/assetActions';
import { getAssetRmmData, refreshAssetRmmData } from '@alga-psa/assets/actions/rmmActions';
import { toast } from 'react-hot-toast';

export function useAssetDetail(assetId: string) {
  const {
    data: asset,
    error: assetError,
    isLoading: assetLoading,
    mutate: mutateAsset,
  } = useSWR(assetId ? ['asset', assetId] : null, ([_, id]) => getAsset(id));

  const {
    data: metrics,
    error: metricsError,
    isLoading: metricsLoading,
  } = useSWR(assetId ? ['asset', assetId, 'summary'] : null, ([_, id]) => getAssetSummaryMetrics(id));

  const {
    data: rmmData,
    error: rmmError,
    isLoading: rmmLoading,
    mutate: mutateRmmData,
  } = useSWR(assetId ? ['asset', assetId, 'rmm'] : null, ([_, id]) => getAssetRmmData(id));
  const {
    data: assetFacts,
    error: assetFactsError,
    isLoading: assetFactsLoading,
  } = useSWR(assetId ? ['asset', assetId, 'facts'] : null, ([_, id]) => getAvailableAssetFacts(id));

  const [isRefreshing, setIsRefreshing] = useState(false);

  const refreshRmmData = useCallback(
    async () => {
      if (!assetId) return;

      try {
        setIsRefreshing(true);
        const [updatedData, updatedAsset] = await Promise.all([
          refreshAssetRmmData(assetId),
          getAsset(assetId),
        ]);

        await mutateRmmData(updatedData, false);
        await mutateAsset(updatedAsset, false);

        toast.success('RMM data refreshed');
      } catch (error) {
        console.error('Error refreshing RMM data:', error);
        toast.error('Failed to refresh RMM data');
      } finally {
        setIsRefreshing(false);
      }
    },
    [assetId, mutateAsset, mutateRmmData]
  );

  return {
    asset,
    metrics,
    rmmData,
    assetFacts,
    isLoading: assetLoading || metricsLoading || rmmLoading || assetFactsLoading,
    isRefreshing,
    refreshRmmData,
    errors: {
      asset: assetError,
      metrics: metricsError,
      rmm: rmmError,
      facts: assetFactsError,
    },
    mutateAsset,
  };
}
