(in-package :opencortex)

(defun ask-neuro (prompt &key (system-prompt "You are the Probabilistic Engine engine of a Neurosymbolic Lisp Machine.") (cascade nil) (context nil))
  "Dispatches a neural request through the provider cascade.
   If CASCADE is a function, it is called with CONTEXT to determine backends."
  (let ((backends (cond
                    ((listp cascade) cascade)
                    ((functionp cascade) (funcall cascade context))
                    ((functionp *provider-cascade*) (funcall *provider-cascade* context))
                    (t *provider-cascade*))))
    (dolist (backend backends)
      (let ((backend-fn (gethash backend *neuro-backends*)))
        (when backend-fn
          (kernel-log "PROBABILISTIC ENGINE: Attempting backend ~a..." backend)
          (let* (;; Consult the Economist for the model ID if the skill is available
                 (model (ignore-errors 
                         (uiop:symbol-call :opencortex.skills.org-skill-economist :economist-get-model-for-provider backend)))
                 (result (if model 
                             (funcall backend-fn prompt system-prompt :model model)
                             (funcall backend-fn prompt system-prompt))))
            (if (and (stringp result) (search ":LOG" result) (or (search "Failure" result) (search "missing" result)))
                (kernel-log "PROBABILISTIC ENGINE: Backend ~a failed. Falling back..." backend)
                (return-from ask-neuro result))))))
    "(:type :LOG :payload (:text \"Neural Cascade Failure\"))"))

(defun execute-openrouter-request (prompt system-prompt &key model)
  (let ((api-key (uiop:getenv "OPENROUTER_API_KEY"))
        (endpoint "https://openrouter.ai/api/v1/chat/completions")
        (model-id (or model "google/gemini-2.0-flash-001")))
    (unless api-key (return-from execute-openrouter-request "(:type :LOG :payload (:text \"OpenRouter API Key missing\"))"))
    (let* ((headers `(("Content-Type" . "application/json")
                      ("Authorization" . ,(format nil "Bearer ~a" api-key))
                      ("HTTP-Referer" . "https://github.com/amr/opencortex")))
           (body (cl-json:encode-json-to-string
                  `((model . ,model-id)
                    (messages . (( (role . "system") (content . ,system-prompt) )
                                 ( (role . "user") (content . ,prompt) )))))))
      (handler-case (let* ((response (dex:post endpoint :headers headers :content body :connect-timeout 10 :read-timeout 30))
                           (json (cl-json:decode-json-from-string response)))
                      (cdr (assoc :content (cdr (assoc :message (car (cdr (assoc :choices json))))))))
        (error (c) (format nil "(:type :LOG :payload (:text \"OpenRouter Failure: ~a\"))" c))))))

(defun execute-gemini-request (prompt system-prompt &key model)
  (let* ((auth (get-provider-auth :gemini)) (api-key (getf auth :api-key)) (bearer-token (getf auth :bearer-token))
         ;; Use model from Economist if provided, else default to pro
         (endpoint-base (if model (format nil "https://generativelanguage.googleapis.com/v1beta/models/~a:generateContent" model)
                            "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent")))
    (unless (or api-key bearer-token) (return-from execute-gemini-request "(:type :LOG :payload (:text \"Authentication missing\"))"))
    (let* ((url (if api-key (format nil "~a?key=~a" endpoint-base api-key) endpoint-base))
           (headers `(("Content-Type" . "application/json") ,@(when bearer-token `(("Authorization" . ,(format nil "Bearer ~a" bearer-token))))))
           (body (cl-json:encode-json-to-string `((contents . ((parts . ((text . ,(format nil "~a~%~%Prompt: ~a" system-prompt prompt))))))))))
      (handler-case (let* ((response (dex:post url :headers headers :content body :connect-timeout 10 :read-timeout 30)) (json (cl-json:decode-json-from-string response)))
                      (cdr (assoc :text (cdr (assoc :parts (car (cdr (assoc :parts (car (cdr (assoc :candidates json)))))))))))
        (error (c) (format nil "(:type :LOG :payload (:text \"Neural Engine Failure: ~a\"))" c))))))
