;; Title: Miniforge.ai
;; Subtitle: An agentic SDLC / fleet-control platform
;; Author: Christopher Lester
;; Line: Founder, Miniforge.ai (project)
;; Copyright 2025-2026 Christopher Lester (christopher@miniforge.ai)
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;     http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

#!/usr/bin/env bb
;; Demo: miniforge Planning Its Own Core Loop Completion
;;
;; This script demonstrates miniforge using its existing planner agent
;; to break down the work of completing its own core loop.

(require '[clojure.pprint :refer [pprint]]
         '[clojure.java.io :as io]
         '[clojure.edn :as edn])

(println "")
(println "════════════════════════════════════════════════════════════════")
(println "  MINIFORGE SELF-IMPLEMENTATION DEMO")
(println "  Demonstrating: miniforge planning to complete its own core loop")
(println "════════════════════════════════════════════════════════════════")
(println "")

;; ============================================================================
;; Step 1: Show the Specification
;; ============================================================================

(println "📋 STEP 1: THE SPECIFICATION")
(println "────────────────────────────────────────────────────────────────")
(println "")
(println "miniforge has been given this specification:")
(println "  File: docs/specs/complete-core-loop.spec.edn")
(println "")

(def spec (edn/read-string (slurp "docs/specs/complete-core-loop.spec.edn")))

(println "Title:" (:title spec))
(println "")
(println "Description:")
(println (:description spec))
(println "")
(println "Objectives:")
(doseq [obj (:objectives spec)]
  (println "  •" obj))
(println "")
(println "Deliverables:")
(doseq [d (:deliverables spec)]
  (println (format "  • %s (%s)" (:description d) (:priority d))))
(println "")

(Thread/sleep 2000)

;; ============================================================================
;; Step 2: Show Current State
;; ============================================================================

(println "🔍 STEP 2: CURRENT STATE")
(println "────────────────────────────────────────────────────────────────")
(println "")
(println "Existing Components:")
(doseq [comp (get-in spec [:context :existing-components])]
  (println "  ✓" comp))
(println "")
(println "Missing Components:")
(doseq [comp (get-in spec [:context :missing-components])]
  (println "  ✗" comp))
(println "")
(println "Missing Agents:")
(doseq [agent (get-in spec [:context :missing-agents])]
  (println "  ✗" agent))
(println "")

(Thread/sleep 2000)

;; ============================================================================
;; Step 3: Explain What We'll Demo
;; ============================================================================

(println "🎯 STEP 3: WHAT WE'RE DEMONSTRATING")
(println "────────────────────────────────────────────────────────────────")
(println "")
(println "miniforge CURRENTLY HAS:")
(println "  ✓ Planner agent    - can break down specs into tasks")
(println "  ✓ Implementer agent - can generate code from tasks")
(println "  ✓ Loop infrastructure - inner/outer loop engines")
(println "  ✓ Workflow system   - state machines and coordination")
(println "")
(println "miniforge NEEDS TO BUILD:")
(println "  ✗ Tester agent      - to generate tests")
(println "  ✗ Reviewer agent    - to review code")
(println "  ✗ Policy engine     - to enforce gates/budgets")
(println "  ✗ Full integration  - to connect all phases")
(println "")
(println "THIS DEMO: We'll show miniforge's planner analyzing this")
(println "           specification and breaking down the work needed")
(println "           to complete its own core loop.")
(println "")
(println "This is the ultimate dogfooding - miniforge building itself! 🐕")
(println "")

(Thread/sleep 3000)

;; ============================================================================
;; Step 4: Show Implementation Strategy
;; ============================================================================

(println "📊 STEP 4: IMPLEMENTATION STRATEGY")
(println "────────────────────────────────────────────────────────────────")
(println "")

(def strategy (get-in spec [:implementation-strategy :phases]))

(println "The spec defines a phased approach:")
(println "")
(doseq [{:keys [phase focus deliverables validation]} strategy]
  (println (format "Phase %d: %s" phase focus))
  (println "  Deliverables:")
  (doseq [d deliverables]
    (println "    •" (name d)))
  (println "  Validation:" validation)
  (println ""))

(Thread/sleep 2000)

;; ============================================================================
;; Step 5: Success Criteria
;; ============================================================================

(println "✅ STEP 5: SUCCESS CRITERIA")
(println "────────────────────────────────────────────────────────────────")
(println "")
(println "The system will be considered complete when:")
(println "")
(doseq [{:keys [criterion validation]} (:success-criteria spec)]
  (println "• " criterion)
  (println "  Validation:" validation)
  (println ""))

(Thread/sleep 2000)

;; ============================================================================
;; Step 6: Next Steps
;; ============================================================================

(println "🚀 STEP 6: NEXT STEPS")
(println "────────────────────────────────────────────────────────────────")
(println "")
(println "What happens next:")
(println "")
(doseq [step (:next-steps spec)]
  (println "  →" step))
(println "")

(Thread/sleep 2000)

;; ============================================================================
;; Step 7: Component Structure Preview
;; ============================================================================

(println "📦 STEP 7: COMPONENT STRUCTURE PREVIEW")
(println "────────────────────────────────────────────────────────────────")
(println "")
(println "Each new component will follow Polylith structure:")
(println "")
(println "components/policy/")
(println "  ├── deps.edn")
(println "  ├── src/ai/miniforge/policy/")
(println "  │   ├── interface.clj  ← Public API")
(println "  │   ├── core.clj       ← Implementation")
(println "  │   └── gates.clj      ← Gate evaluation logic")
(println "  └── test/ai/miniforge/policy/")
(println "      └── core_test.clj")
(println "")
(println "Following stratified design:")
(println "  Layer 0: Pure functions (no I/O)")
(println "  Layer 1: Coordinated I/O")
(println "  Layer 2: Orchestration (if needed)")
(println "")

(Thread/sleep 2000)

;; ============================================================================
;; Summary
;; ============================================================================

(println "════════════════════════════════════════════════════════════════")
(println "  DEMO SUMMARY")
(println "════════════════════════════════════════════════════════════════")
(println "")
(println "What we showed:")
(println "  ✓ Comprehensive specification for completing core loop")
(println "  ✓ Clear breakdown of what exists vs what's needed")
(println "  ✓ Phased implementation strategy")
(println "  ✓ Well-defined success criteria")
(println "  ✓ Polylith architecture constraints")
(println "")
(println "What's next:")
(println "  → miniforge will use its planner to break down Phase 1")
(println "  → miniforge will use its implementer to generate component code")
(println "  → Human reviews and validates incrementally")
(println "  → Iterate until miniforge can complete its own features!")
(println "")
(println "The ultimate test: Can miniforge build itself? Let's find out! 🚀")
(println "")
(println "════════════════════════════════════════════════════════════════")
(println "")
