(async (bot) => {

    
    // Define house dimensions
    const houseWidth = 7; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const houseDepth = 7; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const houseHeight = 4; // Including floor and ceiling
    
    // Find a suitable flat and elevated location for the house
    // Search for a space large enough for the house within 16 blocks.
    const startPos = await world.getNearestFreeSpace(bot, Math.max(houseWidth, houseDepth), 16); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    if (!startPos) {
        throw new Error("Could not find a suitable flat space to build the house. Please ensure there's enough clear space nearby."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    }
    
    // Define Y coordinates for different house levels
    const floorY = startPos.y; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const wallBottomY = floorY + 1; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const wallTopY = startPos.y + houseHeight - 2; // Top layer of blocks for walls, below the ceiling
    const ceilingY = startPos.y + houseHeight - 1; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // Define building material (using oak_log due to inventory, as it was specified 'logs/planks')
    const material = 'oak_log'; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // Define door placement (centered on the front wall)
    const doorX = startPos.x + Math.floor(houseWidth / 2); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const doorZ = startPos.z; // Front wall
    const doorHeight = 2; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // Define window placement (1x2 openings, symmetrical)
    const windowY_bottom = floorY + 1; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const windowY_top = floorY + 2; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // Front/Back Wall Windows: at x = startX + 1, startX + width - 2
    const windowX1 = startPos.x + 1; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const windowX2 = startPos.x + houseWidth - 2; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // Side Wall Windows: at z = startZ + 1, startZ + depth - 2 (avoids corners)
    const windowZ1 = startPos.z + 1; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const windowZ2 = startPos.z + houseDepth - 2; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    
    // 1. Build the floor
    for (let x = startPos.x; x < startPos.x + houseWidth; x++) {
        for (let z = startPos.z; z < startPos.z + houseDepth; z++) {
            await skills.placeBlock(bot, material, x, floorY, z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    }
    
    // 2. Build the walls (layer by layer, from immediately above floor to immediately below ceiling)
    for (let y = wallBottomY; y <= wallTopY; y++) {
        // Front wall (at z = startPos.z)
        for (let x = startPos.x; x < startPos.x + houseWidth; x++) {
            const isDoorSpace = (x === doorX && y >= wallBottomY && y < wallBottomY + doorHeight); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            const isFrontWindowSpace = ((x === windowX1 || x === windowX2) && (y === windowY_bottom || y === windowY_top)); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
            if (!isDoorSpace && !isFrontWindowSpace) {
                await skills.placeBlock(bot, material, x, y, startPos.z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
        }
    
        // Back wall (at z = startPos.z + houseDepth - 1)
        for (let x = startPos.x; x < startPos.x + houseWidth; x++) {
            const isBackWindowSpace = ((x === windowX1 || x === windowX2) && (y === windowY_bottom || y === windowY_top)); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
            if (!isBackWindowSpace) {
                await skills.placeBlock(bot, material, x, y, startPos.z + houseDepth - 1); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
        }
    
        // Left wall (at x = startPos.x) - iterate z from startPos.z + 1 to startPos.z + depth - 2 to avoid corner overlap
        for (let z = startPos.z + 1; z < startPos.z + houseDepth - 1; z++) {
            const isLeftWindowSpace = ((z === windowZ1 || z === windowZ2) && (y === windowY_bottom || y === windowY_top)); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
            if (!isLeftWindowSpace) {
                await skills.placeBlock(bot, material, startPos.x, y, z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
        }
    
        // Right wall (at x = startPos.x + houseWidth - 1) - iterate z from startPos.z + 1 to startPos.z + depth - 2
        for (let z = startPos.z + 1; z < startPos.z + houseDepth - 1; z++) {
            const isRightWindowSpace = ((z === windowZ1 || z === windowZ2) && (y === windowY_bottom || y === windowY_top)); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
            if (!isRightWindowSpace) {
                await skills.placeBlock(bot, material, startPos.x + houseWidth - 1, y, z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
        }
    }
    
    // 3. Place the door
    await skills.placeBlock(bot, 'oak_door', doorX, wallBottomY, doorZ); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // 4. Build the ceiling
    for (let x = startPos.x; x < startPos.x + houseWidth; x++) {
        for (let z = startPos.z; z < startPos.z + houseDepth; z++) {
            await skills.placeBlock(bot, material, x, ceilingY, z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    }

log(bot, 'Code finished.');

})