import argparse

def generate_cut_plan(total_width, total_length, height, base_thickness, total_thickness, slot_width, u_boxes, v_boxes):
    """
    Calculates and prints a markdown cut plan for the operator.
    """
    
    # --- Calculations ---
    inner_width = total_width - 2 * height
    inner_length = total_length - 2 * height
    groove_depth = total_thickness - base_thickness
    
    # --- Collect all cut positions ---
    vertical_cuts = []
    if u_boxes > 1:
        compartment_width = (inner_width - (u_boxes - 1) * slot_width) / u_boxes
        for i in range(1, u_boxes):
            pos = height + (i * compartment_width) + (i * slot_width)
            vertical_cuts.append(pos)
    vertical_cuts.extend([height, total_width - height])
    
    horizontal_cuts = []
    if v_boxes > 1:
        compartment_length = (inner_length - (v_boxes - 1) * slot_width) / v_boxes
        for i in range(1, v_boxes):
            pos = height + (i * compartment_length) + (i * slot_width)
            horizontal_cuts.append(pos)
    horizontal_cuts.extend([height, total_length - height])

    # --- Markdown Output ---
    print("# Saw Operator Cut Plan")
    print(f"## Board Dimensions: `{total_width} x {total_length} mm`")
    print("")
    print("## Specifications")
    print(f"* **V-Groove Spec:** Depth `{groove_depth:.2f} mm`")
    print(f"* **Slot Spec:** Width `{slot_width} mm`, Depth `{groove_depth:.2f} mm`")
    print("")
    print("## Blade Setup")
    print("* **For V-Grooves:** Set saw blade angle to **45 degrees**.")
    print("* **For Slots:** Set saw blade angle to **0 degrees** (straight up).")
    print("")

    # --- Setup 1: Vertical Cuts ---
    print("## SETUP 1: VERTICAL CUTS")
    print("### 1. Reference from LEFT edge:")
    left_cuts = sorted([dist for dist in vertical_cuts if dist <= total_width / 2])
    for dist in left_cuts:
        cut_type = "V-GROOVE" if dist == height else "SLOT"
        print(f"* Set fence to: `{dist:.2f} mm`  -- for **{cut_type}**")

    print("### 2. Reference from RIGHT edge (FLIP board 180 deg):")
    right_cuts = sorted([total_width - dist for dist in vertical_cuts if dist > total_width / 2])
    for dist in right_cuts:
        abs_pos = total_width - dist
        cut_type = "V-GROOVE" if abs_pos == (total_width - height) else "SLOT"
        print(f"* Set fence to: `{dist:.2f} mm`  -- for **{cut_type}**")
    print("")

    # --- Setup 2: Horizontal Cuts ---
    print("## SETUP 2: HORIZONTAL CUTS")
    print("### 1. Rotate board 90 degrees CCW, reference from NEW LEFT edge (original BOTTOM edge):")
    bottom_cuts = sorted([dist for dist in horizontal_cuts if dist <= total_length / 2])
    for dist in bottom_cuts:
        cut_type = "V-GROOVE" if dist == height else "SLOT"
        print(f"* Set fence to: `{dist:.2f} mm`  -- for **{cut_type}**")

    print("### 2. Reference from NEW RIGHT edge (original TOP edge, FLIP board 180 deg):")
    top_cuts = sorted([total_length - dist for dist in horizontal_cuts if dist > total_length / 2])
    for dist in top_cuts:
        abs_pos = total_length - dist
        cut_type = "V-GROOVE" if abs_pos == (total_length - height) else "SLOT"
        print(f"* Set fence to: `{dist:.2f} mm`  -- for **{cut_type}**")
        
    print("")
    print("---")
    print("*End of Plan*")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Generate a cut plan for the poly-mech box.")
    parser.add_argument('--total-width', type=float, default=500, help='Total width of the board')
    parser.add_argument('--total-length', type=float, default=500, help='Total length of the board')
    parser.add_argument('--height', type=float, default=80, help='Height of the box walls')
    parser.add_argument('--base-thickness', type=float, default=3, help='Remaining thickness after V-groove')
    parser.add_argument('--total-thickness', type=float, default=8, help='Total thickness of the material')
    parser.add_argument('--slot-width', type=float, default=8, help='Width of the slots for internal dividers')
    parser.add_argument('--u-boxes', type=int, default=2, help='Number of boxes along the width (U-axis)')
    parser.add_argument('--v-boxes', type=int, default=2, help='Number of boxes along the length (V-axis)')
    
    args = parser.parse_args()
    
    generate_cut_plan(
        args.total_width,
        args.total_length,
        args.height,
        args.base_thickness,
        args.total_thickness,
        args.slot_width,
        args.u_boxes,
        args.v_boxes
    ) 