---
# Open WebUI installation and setup
# Using Docker installation as it's more compatible and the recommended method

- name: Check if Docker is installed
  ansible.builtin.command: docker --version
  register: docker_check
  changed_when: false
  failed_when: false

- name: Display Open WebUI installation method
  ansible.builtin.debug:
    msg: "Open WebUI will be installed via Docker. Run: docker run -d -p 3080:8080 -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main"
  when: docker_check.rc == 0

- name: Skip Open WebUI pip installation (use Docker instead)
  ansible.builtin.debug:
    msg: "Skipping pip installation of Open WebUI due to Python version incompatibility. Use Docker instead."
  when: docker_check.rc != 0

- name: Create Open WebUI start script (Docker)
  ansible.builtin.copy:
    dest: "{{ llmlab_base }}/lab3/start.sh"
    content: |
      #!/bin/bash
      set -e
      
      # Check if Docker is running
      if ! command -v docker &> /dev/null; then
        echo "Error: Docker is not installed. Please install Docker first."
        exit 1
      fi
      
      # Check if container is already running
      if docker ps | grep -q open-webui; then
        echo "Open WebUI is already running."
        exit 0
      fi
      
      # Start Open WebUI container
      docker run -d \
        -p 3080:8080 \
        -v open-webui:/app/backend/data \
        --name open-webui \
        --restart always \
        ghcr.io/open-webui/open-webui:main
      
      echo "Open WebUI started on http://localhost:3080"
    mode: '0755'

- name: Display Open WebUI installation
  ansible.builtin.debug:
    msg: "Open WebUI installed via Docker. Access at http://localhost:3080"
