"""Casting supports — the sprue + feeder stems the resin print needs.

Per the jeweler's reference (green stems in his CAD screenshots): a main sprue
rod feeding the bottom of the shank plus two angled feeder stems lower on the
band. Stones are set after casting, so stems never touch a stone seat.

`sprues(spec)` recomputes the band radii from the spec exactly as
templates.band() does, so a design file can expose `gen_supports()` without
rebuilding any geometry (fast enough to render every time).
"""

from __future__ import annotations

import math

from ringcli.parts import SUPPORT, seg, inner_radius, MIN_WALL


def sprues(spec: dict, band_thickness: float, drop_r: float | None = None):
    """Green support solids: main sprue + 2 stems, ALL vertical.

    Everything drops straight down to the build-plate plane — an angled stem
    is itself an unsupported overhang and makes the print fail. The ring
    prints upright exactly as rendered. drop_r (shank meta) overrides the
    band-derived lowest radius for helix/path shanks."""
    outer_r = drop_r if drop_r is not None else (
        inner_radius(spec["size"]) + max(band_thickness, MIN_WALL))
    z_plate = -outer_r - 6.0
    parts = []
    # main sprue: tapered rod straight down from the bottom of the shank,
    # embedded 0.6mm so the print fuses to it
    main = seg((0, 0, -outer_r + 0.6), (0, 0, z_plate), 1.05, 1.45)
    main.color = SUPPORT
    parts.append(main)
    # two side stems at ±40° around the band, dropping VERTICALLY to the plate
    for sgn in (1, -1):
        a = math.radians(40)
        x = sgn * outer_r * math.sin(a)
        z_touch = -outer_r * math.cos(a)
        stem = seg((x, 0, z_touch + 0.5), (x, 0, z_plate), 0.7, 1.0)
        stem.color = SUPPORT
        parts.append(stem)
    return parts
