"""Pipeline for the UI / CLI: SVG -> geometry -> print events (+ stats)."""
from dataclasses import dataclass

from colors import assign_nozzles # import from the newly added colors.py
from config import MachineConfig
from geometry import Polyline, place_in_area, simplify # import the geometry functions
from preview import stats
from svg_input import load_svg
from toolpath import Event, plan


@dataclass
class Job:
    paths: list[Polyline]   # placed geometry, bed coordinates
    events: list[Event]     # machine coordinates
    stats: dict # stats for the job
    color_map: dict[str | None, int]


# def assign_nozzles(paths: list[Polyline], cfg: MachineConfig) -> None:
#     """Everything goes to the first nozzle in color_order.
#     TODO Will be updated later to map SVG colours to nozzles."""
#     for p in paths:
#         p.nozzle = cfg.print.color_order[0] # assign the first nozzle to the paths


def svg_to_job(svg_source, cfg: MachineConfig) -> Job: # convert the svg to a job
    # now supports multiple colors
    paths = load_svg(svg_source)
    color_map = assign_nozzles(paths, cfg)                  # nearest nozzle colour (colors.py)
    area = cfg.reachable_area(set(color_map.values()))     # where every nozzle in use can reach
    d = cfg.design
    paths = place_in_area(paths, area, d.margin, d.fit_to_print_area, d.max_size)
    paths = simplify(paths, cfg.print.curve_tolerance)
    events = plan(paths, cfg)
    return Job(paths, events, stats(events, cfg), color_map)
