;; --------------------------------------------------------------------------
;; Coni Structural Autogen AI
;; --------------------------------------------------------------------------

;; Generates new physical WebAudio nodes dynamically and structurally wires them 
;; into the existing synthesis graph.

(defn autogen-step! []
  (let [db @*db*
        nodes (:nodes db)
        window (js/global "window")
        Math (js/global "Math")]
    (if (or (nil? nodes) (= (count (keys nodes)) 0))
      ;; If graph is empty, spawn a master destination first!
      (let [out-id (next-id)
            ctx (init-audio!)
            audio-node ((:create (get node-registry :destination)) ctx {})
            out-node {:id out-id :type :destination :x 800 :y 300 :params {} :audio-node audio-node}]
        (swap! *db* (fn [db] (assoc-in db [:nodes out-id] out-node))))
      
      ;; Otherwise, pick a random existing node as an anchor
      (let [node-keys (keys nodes)
            target-idx (math/random-int (count node-keys))
            target-id (get node-keys target-idx)
            target-node (get nodes target-id)
            target-type (:type target-node)
            registry node-registry
            target-def (get registry (keyword target-type))
            target-inputs (:inputs target-def)]
            
        (if (and target-inputs (> (count target-inputs) 0))
          (let [new-node-id (next-id)
                node-types (keys registry)
                new-type-idx (math/random-int (count node-types))
                new-type-kw (get node-types new-type-idx)
                new-type (name new-type-kw)
                new-def (get registry new-type-kw)
                new-outputs (:outputs new-def)]
                
            (if (and new-outputs (> (count new-outputs) 0) (not= new-type "destination"))
              (let [;; Position to the left of the target node
                    new-x (- (:x target-node) (+ 250 (* (math/random) 100)))
                    new-y (+ (:y target-node) (- (* (math/random) 200) 100))
                    
                    ;; Initialize default parameters dynamically via reduce loop
                    new-params (loop [ps (:params new-def), acc {}]
                                 (if (= (count ps) 0) 
                                   acc
                                   (let [p (first ps)]
                                     (recur (rest ps) (assoc acc (:id p) (:default p))))))
                                     
                    ctx (init-audio!)
                    audio-node ((:create new-def) ctx new-params)
                    new-node {:id new-node-id :type new-type-kw :x new-x :y new-y :params new-params :audio-node audio-node}
                    
                    ;; Select random compatible ports
                    target-port-idx (math/random-int (count target-inputs))
                    target-port-kw (get target-inputs target-port-idx)
                    target-port (name target-port-kw)
                    
                    src-port-kw (get new-outputs 0)
                    src-port (name src-port-kw)]
                    
                ;; Inject node actively via native swap!
                (swap! *db* (fn [db] (assoc-in db [:nodes new-node-id] new-node)))
                (if (= new-type "analyser")
                  (js/call window "setTimeout" (fn [] (draw-analyser-loop new-node-id)) 100)
                  nil)
                
                ;; Let DOM settle slightly, then connect paths natively
                (js/call window "setTimeout"
                  (fn []
                    (connect-nodes! new-node-id src-port target-id target-port))
                  150))
              nil))
          nil)))))
