import { getCurrentOrg, getOrgById } from '@/atoms/user';
import { useNavigate } from '@umijs/max';
import { modelCategoriesMap } from '../../llmodels/config';
import { categoryToPathMap } from '../../llmodels/config/button-actions';

const useOpenPlayground = () => {
  const navigate = useNavigate();

  const generateModelName = (row: any) => {
    const org = getOrgById(row.owner_principal_id) ?? getCurrentOrg();
    const rawModel =
      org?.name && !org.is_platform ? `${org.name}/${row.name}` : row.name;
    return rawModel;
  };

  const handleOpenPlayGround = (row: any) => {
    // Match the id format the OpenAI ``/v1/models`` endpoint reports: an
    // org's models are namespaced as ``{org}/{name}``, while the platform
    // org's carry no prefix (the server strips it). Prefer the row's own
    // ``owner_principal_id`` (an org principal id); fall back to the Org the
    // caller is currently acting under for the admin "All" view, where the
    // row's owner still resolves via the platform-wide org cache.
    const rawModel = generateModelName(row);
    const modelName = encodeURIComponent(rawModel);

    for (const [category, path] of Object.entries(categoryToPathMap)) {
      if (
        row.categories?.includes(category) &&
        [
          modelCategoriesMap.text_to_speech,
          modelCategoriesMap.speech_to_text
        ].includes(category)
      ) {
        navigate(`${path}&model=${modelName}`);
        return;
      }
      if (row.categories?.includes(category)) {
        navigate(`${path}?model=${modelName}`);
        return;
      }
    }
    navigate(`/playground/chat?model=${modelName}`);
  };
  return {
    handleOpenPlayGround,
    generateModelName
  };
};

export default useOpenPlayground;
