import { useLiveQuery } from 'dexie-react-hooks';
import { campaignsRepo } from '@/lib/db/repositories';
import { useUiStore } from '@/stores/uiStore';
import type { Campaign } from '@/lib/schemas';

export function useCampaigns(): Campaign[] {
  return useLiveQuery(() => campaignsRepo.list(), [], []);
}

export function useActiveCampaign(): Campaign | undefined {
  const id = useUiStore((s) => s.activeCampaignId);
  return useLiveQuery(
    () => (id ? campaignsRepo.get(id) : Promise.resolve(undefined)),
    [id],
    undefined,
  );
}
