import { apiClient } from './api-client';

// Mirror of the sidecar SetupState shape — keep in sync with
// helix-engage-server/src/config/setup-state.defaults.ts. Any change to the
// step list there must be reflected here.

export type SetupStepName =
    | 'identity'
    | 'clinics'
    | 'doctors'
    | 'team'
    | 'telephony'
    | 'ai';

export const SETUP_STEP_NAMES: readonly SetupStepName[] = [
    'identity',
    'clinics',
    'doctors',
    'team',
    'telephony',
    'ai',
] as const;

export type SetupStepStatus = {
    completed: boolean;
    completedAt: string | null;
    completedBy: string | null;
};

export type SetupState = {
    version?: number;
    updatedAt?: string;
    wizardDismissed: boolean;
    steps: Record<SetupStepName, SetupStepStatus>;
    wizardRequired: boolean;
};

// Human-friendly labels for the wizard UI + settings hub badges. Kept here
// next to the type so adding a new step touches one file.
export const SETUP_STEP_LABELS: Record<SetupStepName, { title: string; description: string }> = {
    identity: {
        title: 'Hospital Identity',
        description: 'Confirm your hospital name, upload your logo, and pick brand colors.',
    },
    clinics: {
        title: 'Clinics',
        description: 'Add your physical branches with addresses and visiting hours.',
    },
    doctors: {
        title: 'Doctors',
        description: 'Add clinicians, assign them to clinics, and set their schedules.',
    },
    team: {
        title: 'Team',
        description: 'Invite supervisors and call-center agents to your workspace.',
    },
    telephony: {
        title: 'Telephony',
        description: 'Connect Ozonetel and Exotel for inbound and outbound calls.',
    },
    ai: {
        title: 'AI Assistant',
        description: 'Choose your AI provider and customise the assistant prompts.',
    },
};

export const getSetupState = () =>
    apiClient.get<SetupState>('/api/config/setup-state', { silent: true });

export const markSetupStepComplete = (step: SetupStepName, completedBy?: string) =>
    apiClient.put<SetupState>(`/api/config/setup-state/steps/${step}`, {
        completed: true,
        completedBy,
    });

export const markSetupStepIncomplete = (step: SetupStepName) =>
    apiClient.put<SetupState>(`/api/config/setup-state/steps/${step}`, { completed: false });

export const dismissSetupWizard = () =>
    apiClient.post<SetupState>('/api/config/setup-state/dismiss');

export const resetSetupState = () =>
    apiClient.post<SetupState>('/api/config/setup-state/reset');
