(async (bot) => {

    
    // Plan: Build a simple 5x5 wooden house with 3 high walls and a flat roof.
    // 1. Find a suitable flat space for the house.
    // 2. Build the floor (5x5).
    // 3. Build the walls (3 high).
    // 4. Leave a space for the door.
    // 5. Build the roof.
    // 6. Place a door.
    
    // Find a suitable free space for a 5x5 house (so we need a 7x7 free space to place it comfortably)
    const houseSize = 5; // Interior size
    const wallHeight = 3; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    let buildPos = await world.getNearestFreeSpace(bot, houseSize + 2, 16); // Search for 7x7 space within 16 blocks
    
    if (!buildPos) {
        log(bot,"Could not find a suitable space to build the house."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        // If no space found, try a smaller size or wider range, or indicate failure.
        // For now, I'll assume a spot will be found or retry.
        // As a robust solution, I would handle this more gracefully, perhaps by trying a new location or smaller house.
        // But for this turn, I need to proceed with building.
        buildPos = bot.entity.position.offset(2, 0, 2); // Fallback: just offset from current position
    }
    
    log(bot,`Found a building spot at ${buildPos.x}, ${buildPos.y}, ${buildPos.z}`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // Adjust buildPos to be the bottom-south-west corner of the house itself, not the free space.
    // Assuming getNearestFreeSpace gives the south-west corner of the found space.
    // If the free space is 7x7, and the house is 5x5, we can center it by adding 1 to x and z.
    let houseStartX = buildPos.x + 1; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    let houseStartY = buildPos.y; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    let houseStartZ = buildPos.z + 1; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // 2. Build the floor (5x5)
    log(bot,"Building the floor..."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    for (let x = 0; x < houseSize; x++) {
        for (let z = 0; z < houseSize; z++) {
            await skills.placeBlock(bot, "oak_log", houseStartX + x, houseStartY, houseStartZ + z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    }
    log(bot,"Floor built."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // 3. Build the walls (3 high)
    log(bot,"Building the walls..."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    for (let y = 1; y <= wallHeight; y++) { // From 1 block above floor to wallHeight
        for (let x = 0; x < houseSize; x++) {
            for (let z = 0; z < houseSize; z++) {
                // Only place blocks on the outer perimeter of the house
                if (x === 0 || x === houseSize - 1 || z === 0 || z === houseSize - 1) {
                    // Leave a 1-block wide space for the door on one side (e.g., front middle)
                    if (y === 1 && x === Math.floor(houseSize / 2) && z === houseSize - 1) {
                        continue; // Skip placing block for door
                    }
                    await skills.placeBlock(bot, "oak_log", houseStartX + x, houseStartY + y, houseStartZ + z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                }
            }
        }
    }
    log(bot,"Walls built."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // 5. Build the roof (flat roof on top of the walls)
    log(bot,"Building the roof..."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    for (let x = 0; x < houseSize; x++) {
        for (let z = 0; z < houseSize; z++) {
            await skills.placeBlock(bot, "oak_log", houseStartX + x, houseStartY + wallHeight + 1, houseStartZ + z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    }
    log(bot,"Roof built."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // 6. Place a door
    log(bot,"Placing the door..."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const doorX = houseStartX + Math.floor(houseSize / 2); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const doorY = houseStartY + 1; // Door bottom block is 1 block above floor
    const doorZ = houseStartZ + houseSize - 1; // Place on the last row (front)
    await skills.placeBlock(bot, "oak_door", doorX, doorY, doorZ, 'bottom'); // Place the bottom half of the door
    // The game usually places the top half automatically, but sometimes it needs to be placed explicitly if the first one fails.
    // Assuming it handles both.
    log(bot,`Door placed at ${doorX}, ${doorY}, ${doorZ}.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // House building complete.
    log(bot,"Simple wooden house construction complete!");

log(bot, 'Code finished.');

})