---
# Lab start scripts setup

- name: Create lab1 start script (Transformer Lab)
  ansible.builtin.copy:
    dest: "{{ llmlab_base }}/lab1/start.sh"
    content: |
      #!/bin/bash
      set -e
      
      export NUMPY_DISABLE_OPTIMIZATION_CHECK=1
      source "{{ llmlab_base }}/.transformerlab/miniforge3/etc/profile.d/conda.sh"
      conda activate transformerlab
      
      cd "{{ llmlab_base }}/.transformerlab/src"
      ./run.sh
    mode: '0755'
    force: no

- name: Create lab2 start script (Ollama)
  ansible.builtin.copy:
    dest: "{{ llmlab_base }}/lab2/start.sh"
    content: |
      #!/bin/bash
      set -e
      
      echo "Starting Ollama..."
      
      # Check if already running
      if pgrep -f "ollama serve" > /dev/null; then
        echo "Ollama is already running."
        exit 0
      fi
      
      # Start Ollama
      nohup ollama serve > {{ llmlab_base }}/.llmlab/logs/ollama.log 2>&1 &
      echo "Ollama started (PID: $!)"
      echo "Ollama is available at http://localhost:11434"
    mode: '0755'
    force: no

- name: Create lab3 start script (Open WebUI)
  ansible.builtin.copy:
    dest: "{{ llmlab_base }}/lab3/start.sh"
    content: |
      #!/bin/bash
      set -e
      
      export OPEN_WEBUI_PORT=8080
      export OPEN_WEBUI_HOST=0.0.0.0
      
      # Check if already running
      if pgrep -f "open-webui serve" > /dev/null; then
        echo "Open WebUI is already running."
        exit 0
      fi
      
      # Start Open WebUI
      nohup open-webui serve \
        --port ${OPEN_WEBUI_PORT} \
        --host ${OPEN_WEBUI_HOST} \
        > {{ llmlab_base }}/.llmlab/logs/open-webui.log 2>&1 &
      
      echo "Open WebUI started on http://${OPEN_WEBUI_HOST}:${OPEN_WEBUI_PORT}"
      echo "PID: $!"
    mode: '0755'
    force: no

- name: Create lab4 start script (ChunkViz)
  ansible.builtin.copy:
    dest: "{{ llmlab_base }}/lab4/start.sh"
    content: |
      #!/bin/bash
      set -e
      
      CHUNKVIZ_PORT=${PORT:-3001}
      
      # Start ChunkViz in background
      cd "{{ llmlab_base }}/lab4/ChunkViz"
      nohup npm start > {{ llmlab_base }}/.llmlab/logs/chunkviz.log 2>&1 &
      CHUNKVIZ_PID=$!
      
      echo "ChunkViz started on http://0.0.0.0:${CHUNKVIZ_PORT}"
      echo "PID: ${CHUNKVIZ_PID}"
    mode: '0755'
    force: no

- name: Create lab5 start script (Promptfoo)
  ansible.builtin.copy:
    dest: "{{ llmlab_base }}/lab6/start.sh"
    content: |
      #!/bin/bash
      set -e
      
      cd "{{ llmlab_base }}/lab6"
      
      # Run Promptfoo evaluation
      npx promptfoo eval -c promptfoo.yaml
    mode: '0755'
    force: no

- name: Create lab stop scripts
  ansible.builtin.copy:
    dest: "{{ llmlab_base }}/lab{{ item }}/stop.sh"
    content: |
      #!/bin/bash
      set -e
      
      echo "Stopping Lab {{ item }}..."
      
      case "{{ item }}" in
        1)
          pkill -f "transformerlab.*run.sh" 2>/dev/null || true
          ;;
        2)
          pkill -f "ollama serve" 2>/dev/null || true
          ;;
        3)
          pkill -f "open-webui" 2>/dev/null || true
          ;;
        4)
          pkill -f "ChunkViz" 2>/dev/null || true
          ;;
        5)
          pkill -f "promptfoo" 2>/dev/null || true
          ;;
      esac
      
      echo "Lab {{ item }} stopped."
    mode: '0755'
  loop: [1, 2, 3, 4, 5]

- name: Display lab scripts creation
  ansible.builtin.debug:
    msg: "All lab start/stop scripts created in {{ llmlab_base }}/"
