-- Vox Structural System
vox_structural = {} -- Initialize the global table for the mod

local structural_shapes = {}

-- Register a new structural shape
function vox_structural.register_shape(shape_name, shape_def)
    structural_shapes[shape_name] = shape_def
end

-- Register a block with structural shapes
function vox_structural.register_block_with_shapes(modname, base_block, options)
    for shape_name, shape_def in pairs(structural_shapes) do
        local base_node_name = modname .. ":" .. base_block
        local shape_node_name = modname .. ":" .. base_block .. "_" .. shape_name

        -- Create the new node definition
        local base_def = minetest.registered_nodes[base_node_name]
        if not base_def then
            minetest.log("error", "[vox_structural] Base node not found: " .. base_node_name)
            return
        end

        local new_node_def = table.copy(base_def)
        new_node_def.description = base_def.description .. " (" .. shape_name:gsub("_", " ") .. ")"
        new_node_def.tiles = shape_def.tiles or base_def.tiles
        new_node_def.groups = table.copy(base_def.groups or {})
        new_node_def.groups.shape = 1 -- Add a "shape" group

        -- Apply shape-specific overrides
        if shape_def.override then
            for key, value in pairs(shape_def.override) do
                new_node_def[key] = value
            end
        end

        -- Register the new node
        minetest.register_node(shape_node_name, new_node_def)
    end
end

-- ---------------------------- Basic Shapes -------------------------- --
-- Slab
vox_structural.register_shape("slab", {
    tiles = nil, -- Inherit tiles from the base block
    override = {
        drawtype = "nodebox",
        node_box = {
            type = "fixed",
            fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
        },
    },
})

-- Stairs
vox_structural.register_shape("stairs", {
    tiles = nil,
    override = {
        drawtype = "nodebox",
        node_box = {
            type = "fixed",
            fixed = {
                {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
                {-0.5, 0, -0.5, 0.5, 0.5, 0},
            },
        },
    },
})

-- ---------------------------- Functional -------------------------- --
-- Pressure Plate
vox_structural.register_shape("pressure_plate", {
    tiles = nil,
    override = {
        drawtype = "nodebox",
        node_box = {
            type = "fixed",
            fixed = {
                {-0.5, -0.5, -0.5, 0.5, -0.4375, 0.5}, 
            },
        },
    },
})

-- ---------------------------- Doors and Variants -------------------------- --
vox_structural.register_shape("door_flush", {
    override = {
        drawtype = "mesh",
        mesh = "door_flush.obj",
    },
})

vox_structural.register_shape("door_half_window", {
    override = {
        drawtype = "mesh",
        mesh = "door_half_window.obj",
    },
})

-- Add mix-and-match for window materials
local window_variants = {"glass", "colored_glass", "paper", "colored_paper"}

for _, material in ipairs(window_variants) do
    vox_structural.register_shape("door_half_window_" .. material, {
        override = {
            drawtype = "mesh",
            mesh = "door_half_window.obj",
            tiles = {"window_" .. material .. ".png"},
        },
    })
end
