(async (bot) => {

    // Planning for this turn:
    // 1. Check if the bot has a pickaxe in inventory.
    // 2. If not, craft a stone pickaxe:
    //    a. Find a suitable temporary location for a crafting table near the bot.
    //    b. Place the crafting table using bot.equip for the crafting table.
    //    c. Craft a stone pickaxe using bot.craft.craftItem.
    //    d. Break the temporary crafting table to reclaim it, ensuring the newly crafted pickaxe is equipped.
    // 3. Equip the best available pickaxe (stone if crafted, otherwise wooden).
    
    // Step 0: Ensure bot has a pickaxe to clear obstacles
    let hasPickaxe = bot.inventory.items().some(item => item.name.includes("pickaxe")); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    if (!hasPickaxe) {
        bot.chat("No pickaxe found. Crafting one now."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
        // Find a temporary spot to place the crafting table for self-crafting
        let botPos = bot.entity.position.offset(0, -1, 0).floored(); // Block bot is standing on
        let tempCraftingTablePos = botPos.offset(1, 0, 0); // Try 1 block to the east of bot's base
        let blockAtTemp = bot.blockAt(tempCraftingTablePos); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        let blockBelowTemp = bot.blockAt(tempCraftingTablePos.offset(0, -1, 0)); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
        let foundSpotToPlaceCraftingTable = false; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        let actualCraftingTablePos = null; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
        // First, try the immediate spot
        if (blockAtTemp && blockAtTemp.name === "air" && blockBelowTemp && blockBelowTemp.type !== 0) {
            foundSpotToPlaceCraftingTable = true; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            actualCraftingTablePos = tempCraftingTablePos; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        } else {
            // Fallback: If cannot find a simple adjacent spot, try a more robust search
            for (let dy = -1; dy <= 1; dy++) {
                for (let dx = -2; dx <= 2; dx++) {
                    for (let dz = -2; dz <= 2; dz++) {
                        if (dx === 0 && dy === 0 && dz === 0) continue; // Don't try current spot
                        let potentialPos = botPos.offset(dx, dy, dz); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                        let potentialBlockAt = bot.blockAt(potentialPos); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                        let potentialBlockBelow = bot.blockAt(potentialPos.offset(0, -1, 0)); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
                        if (potentialBlockAt && potentialBlockAt.name === "air" && potentialBlockBelow && potentialBlockBelow.type !== 0) {
                            foundSpotToPlaceCraftingTable = true; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                            actualCraftingTablePos = potentialPos; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                            break; // Exit dz loop
                        }
                    }
                    if (foundSpotToPlaceCraftingTable) break; // Exit dx loop
                }
                if (foundSpotToPlaceCraftingTable) break; // Exit dy loop
            }
        }
    
        if (foundSpotToPlaceCraftingTable) {
            bot.chat(`Found a spot at ${actualCraftingTablePos.x}, ${actualCraftingTablePos.y}, ${actualCraftingTablePos.z}. Placing temporary crafting table.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            const craftingTableItem = bot.inventory.items().find(item => item.name === "crafting_table"); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            if (!craftingTableItem) {
                bot.chat("Error: No crafting table in inventory to place. Cannot craft pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                throw new Error("No crafting table in inventory to craft pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
            await bot.equip(craftingTableItem, 'hand'); // Equip the crafting table to hotbar
            await skills.placeBlock(bot, "crafting_table", actualCraftingTablePos.x, actualCraftingTablePos.y, actualCraftingTablePos.z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            await skills.wait(bot, 500); // Give a moment for the block to place
    
            const craftingTableBlock = bot.blockAt(actualCraftingTablePos); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            if (!craftingTableBlock || craftingTableBlock.name !== "crafting_table") {
                bot.chat("Failed to place crafting table. Cannot craft pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                throw new Error("Failed to place crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
    
            const stonePickaxeRecipe = bot.recipesFor("stone_pickaxe", null, 1, craftingTableBlock)[0]; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            if (stonePickaxeRecipe) {
                bot.chat("Crafting stone pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                await bot.craft.craftItem(stonePickaxeRecipe, 1, craftingTableBlock); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                bot.chat("Crafted stone pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            } else {
                bot.chat("No recipe found for stone pickaxe. This shouldn't happen with cobblestone and sticks. Check inventory."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                throw new Error("No recipe for stone pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
    
            // Reclaim the crafting table
            bot.chat("Breaking temporary crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            const newPickaxe = bot.inventory.items().find(item => item.name.includes("pickaxe")); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            if (newPickaxe) {
                await bot.equip(newPickaxe, 'hand'); // Equip the newly crafted pickaxe
            } else {
                bot.chat("Warning: Pickaxe not found after crafting for breaking crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
            await skills.breakBlockAt(bot, actualCraftingTablePos.x, actualCraftingTablePos.y, actualCraftingTablePos.z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            await skills.wait(bot, 500); // Wait for item to drop and be collected
            bot.chat("Reclaimed crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        } else {
            bot.chat("Could not find any suitable spot to place temporary crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            throw new Error("Could not find suitable spot for temporary crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    } else {
        bot.chat("Already have a pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    }
    
    // Ensure a pickaxe is equipped for general pathfinding/breaking
    const equippedPickaxe = bot.inventory.items().find(item => item.name.includes("pickaxe")); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    if (equippedPickaxe) {
        await bot.equip(equippedPickaxe, 'hand'); // Equip the best available pickaxe
        bot.chat(`Equipped ${equippedPickaxe.name}.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    } else {
        bot.chat("No pickaxe found even after attempting to craft. Cannot proceed without a pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        throw new Error("No pickaxe to proceed."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    }

log(bot, 'Code finished.');

})