import cadquery as cq

def book_case(
        length:float = 75, 
        width:float = 25, 
        height:float = 50,
        segments:int = 3,
        margin_top:float = 2,
        margin_sides:float = 2,
        back_translate:float = 1
    ):
    case = cq.Workplane("XY").box(length,width,height)
    spacing = ((height-margin_top*2)/segments)
    segment_height = spacing - margin_top * 2
    segment_length = length - margin_sides * 2
    segment_width = width
    
    segment = (
        cq.Workplane("XY")
        .box(
            segment_length,
            segment_width, 
            segment_height
        )
        .rotate((1,0,0),(0,0,0),90)
    )
    
    def add_segment(loc:cq.Location)->cq.Shape:
        return segment.val().located(loc) #type:ignore
    
    segment_cutout:cq.Workplane = (
        cq.Workplane("XY")
        .rarray(
            xSpacing = length, 
            ySpacing = spacing,
            xCount = 1, 
            yCount= segments, 
            center = True)
        .eachpoint(add_segment)
    ).rotate((1,0,0),(0,0,0),90)
    
    scene = (
        cq.Workplane("XY")
        .union(case)
        .cut(segment_cutout.translate((0,back_translate,0)))
    )

    return scene