// Sidekick data extension: registers read-only tools that answer merchant
// questions from the Dynasort backend. The sandbox attaches an App Bridge
// session token as the Authorization header because the request targets the
// app's configured auth domain. Response shaping lives server-side in
// api/sidekick/index.php so it can be iterated without redeploying this
// extension. Tool names here must match tools.json and the endpoint's
// action cases.
const TOOLS = [
  'get_sorting_status',
  'search_collections',
  'get_collection_insights',
  'list_recipes',
  'get_recipe',
  'search_products',
  'get_store_performance',
  'get_serving_status',
];

export default () => {
  for (const name of TOOLS) {
    shopify.tools.register(name, async (input) => {
      const response = await fetch('https://dynasort.immersiveecommerce.com/api/sidekick/', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({action: name, input: input || {}}),
      });
      return response.json();
    });
  }
};
