import { queryOptions } from '@tanstack/react-query';

import {
  getBootstrapSetupStatus,
  getCurrentBootstrapSetupRun,
  getCurrentSetupRun,
  getSetupProfiles,
  getSetupReadiness,
  getSetupRunReport,
  getSetupTemplates,
  verifySetup
} from './service';
import type { SetupVerificationPayload } from './types';

export const setupKeys = {
  all: ['setup'] as const,
  readiness: (profileId?: string) => [...setupKeys.all, 'readiness', profileId ?? 'crm_demo'] as const,
  templates: (profileId?: string) => [...setupKeys.all, 'templates', profileId ?? 'crm_demo'] as const,
  profiles: (profileId?: string) => [...setupKeys.all, 'profiles', profileId ?? 'crm_demo'] as const,
  bootstrapStatus: () => [...setupKeys.all, 'bootstrap', 'status'] as const,
  bootstrapRun: (bootstrapToken?: string) =>
    [...setupKeys.all, 'bootstrap', 'run', bootstrapToken ? 'token-provided' : 'no-token'] as const,
  run: () => [...setupKeys.all, 'run'] as const,
  report: () => [...setupKeys.all, 'run', 'report'] as const,
  verification: (payload: SetupVerificationPayload = {}) => [...setupKeys.all, 'verification', payload] as const
};

export const setupReadinessQueryOptions = (profileId?: string) =>
  queryOptions({
    queryKey: setupKeys.readiness(profileId),
    queryFn: () => getSetupReadiness(profileId)
  });

export const setupTemplatesQueryOptions = (profileId?: string) =>
  queryOptions({
    queryKey: setupKeys.templates(profileId),
    queryFn: () => getSetupTemplates(profileId)
  });

export const setupProfilesQueryOptions = (profileId?: string) =>
  queryOptions({
    queryKey: setupKeys.profiles(profileId),
    queryFn: () => getSetupProfiles(profileId)
  });

export const bootstrapSetupStatusQueryOptions = () =>
  queryOptions({
    queryKey: setupKeys.bootstrapStatus(),
    queryFn: () => getBootstrapSetupStatus()
  });

export const setupRunQueryOptions = () =>
  queryOptions({
    queryKey: setupKeys.run(),
    queryFn: () => getCurrentSetupRun()
  });

export const bootstrapSetupRunQueryOptions = (bootstrapToken?: string) =>
  queryOptions({
    queryKey: setupKeys.bootstrapRun(bootstrapToken),
    queryFn: () => getCurrentBootstrapSetupRun(bootstrapToken)
  });

export const setupRunReportQueryOptions = () =>
  queryOptions({
    queryKey: setupKeys.report(),
    queryFn: () => getSetupRunReport()
  });

export const setupVerificationQueryOptions = (payload: SetupVerificationPayload = {}) =>
  queryOptions({
    queryKey: setupKeys.verification(payload),
    queryFn: () => verifySetup(payload)
  });
