import { useAtomValue } from 'jotai';
import { ContextEnvEnum, ContextSourceEnum } from '@spiffy-ai/commerce-api-client';
import { VariantInfo } from 'src/application/models';
import { variantInfoAtom } from 'src/atoms/app/variant';
import {
  contextSourceAtom,
  envAtom as enviveEnvAtom,
  orgShortNameAtom as enviveOrgShortNameAtom,
  orgIdAtom,
} from 'src/atoms/envive/enviveConfig';
import { chatIdAtom, userIdAtom } from 'src/atoms/app/index';

export interface AppDetails {
  orgId: string;
  orgShortName: string;
  chatId: string;
  userId: string;
  source: ContextSourceEnum;
  env: ContextEnvEnum;
  variantInfo: VariantInfo;
}

export const useAppDetails = (): AppDetails => {
  const fetchedOrgId = useAtomValue(orgIdAtom);
  const orgId = fetchedOrgId ?? ''; // Provide a default empty string if undefined

  const orgShortName = useAtomValue(enviveOrgShortNameAtom) ?? 'spiffy-ai';
  const chatId = useAtomValue(chatIdAtom);
  const userId = useAtomValue(userIdAtom);
  const source = useAtomValue(contextSourceAtom) ?? ContextSourceEnum.App;
  const env = (useAtomValue(enviveEnvAtom) as ContextEnvEnum) ?? ContextEnvEnum.Dev; // Cast to ContextEnvEnum
  const variantInfo = useAtomValue(variantInfoAtom);

  return {
    orgId,
    orgShortName,
    chatId,
    userId,
    source,
    env,
    variantInfo,
  };
};
