/* eslint-disable canonical/id-match */
'use client';
import { useOrderStore } from './context';
import { type Steps } from './types';
import { useCustomerQuery } from '@/hooks/api/customers';
import { Enum_Customer_Role } from '@repo/graphql/types';
import { useEffect } from 'react';

const STEPS: Steps[] = [
  'master-select',
  'client-select',
  'service-select',
  'datetime-select',
  'success',
];
export const MASTER_STEPS: Steps[] = STEPS.filter((step) => step !== 'master-select');
export const CLIENT_STEPS: Steps[] = STEPS.filter((step) => step !== 'client-select');

export function useInitOrderStore() {
  const { data: { customer } = {} } = useCustomerQuery();

  const setMasterId = useOrderStore((store) => store.setMasterId);
  const setClientId = useOrderStore((store) => store.setClientId);
  const setStep = useOrderStore((store) => store.setStep);
  const setStepsSequence = useOrderStore((store) => store._setStepSequence);

  useEffect(() => {
    const role = customer?.role;

    if (role === Enum_Customer_Role.Master && customer) {
      setMasterId(customer?.documentId);
    }

    if (role === Enum_Customer_Role.Client && customer) {
      setClientId(customer?.documentId);
    }

    const steps = role === Enum_Customer_Role.Master ? MASTER_STEPS : CLIENT_STEPS;
    const initialStep = steps[0] as Steps;

    setStepsSequence(steps);
    setStep(initialStep);
  }, [customer, setClientId, setMasterId, setStep, setStepsSequence]);
}
