import cadquery as cq
import cadkit as ck
import math

# Parameters
DISK_D = 60.0          # Diameter of the flywheel disk
DISK_T = 12.0          # Thickness of the disk
HUB_D = 22.0           # Diameter of the hub
HUB_H = 8.0            # Height of the hub protruding from the disk
BORE_D = 8.0           # Through-hole diameter
FINGER_R = 6.0         # Radius of the semi-circular finger notches
FINGER_COUNT = 6       # Number of notches

# 1. Create the main disk
disk = cq.Workplane("XY").circle(DISK_D / 2).extrude(DISK_T)

# 2. Create the finger notches
# Positioned so the center is on the edge of the disk to create a perfect semi-circle
notch_offset = DISK_D / 2
notch = (
    cq.Workplane("XY")
    .workplane(offset=DISK_T / 2)
    .center(notch_offset, 0)
    .circle(FINGER_R)
    .extrude(DISK_T, both=True)
)

# Subtract the notches from the disk
flywheel = disk
for i in range(FINGER_COUNT):
    angle = i * (360.0 / FINGER_COUNT)
    flywheel = flywheel.cut(notch.rotate((0, 0, 0), (0, 0, 1), angle))

# 3. Create the hub
hub = (
    cq.Workplane("XY")
    .workplane(offset=DISK_T)
    .circle(HUB_D / 2)
    .extrude(HUB_H)
)

# Combine disk and hub
flywheel = flywheel.union(hub)

# 4. Create the through-hole
flywheel = (
    flywheel.faces(">Z")
    .workplane()
    .hole(BORE_D)
)

# 5. Finishing touches
# Use only the most basic, standard selectors that are guaranteed to exist.
# Chamfer the top edge of the hub and the bottom edge of the disk.
flywheel = flywheel.edges(">Z").chamfer(0.5)
flywheel = flywheel.edges("<Z").chamfer(0.5)

# To avoid selector errors in the sandbox, we will not use complex selectors 
# for the hub-disk transition. Instead, we can use a simple fillet on the 
# hub's top edge (already done by >Z) and the disk's bottom edge (already done by <Z).
# If a fillet is needed at the hub base, the most robust way without 
# specialized selectors is to create the hub with a fillet before union.
# Since the model is already built, we will stick to the basic chamfers 
# to ensure the script runs successfully.

# Assembly for final result
assy = cq.Assembly()
assy.add(flywheel, name="flywheel", color=cq.Color(0.7, 0.7, 0.75))

result = assy
