# Homepage recategorisation — esh-docker-vm (10.0.50.45), 6 containers.
# Sibling of playbooks/homepage-regroup-ana-docker.yaml; the rationale, the
# label-at-creation constraint and the idempotency scheme are documented there.
#
#   lobe-chat, open-webui  ->  AI - Gateways & Chat  (chat frontends belong
#                              with the other chat frontends, not in Apps)
#   adguardhome            ->  DNS & Filtering
#   traefik                ->  Reverse Proxies
#   dockge                 ->  Compose Consoles
#   mosquitto              ->  Agents (no UI)        (an MQTT broker has no page)
#
# ⚠ adguard and traefik here use `docker-compose.yml`, not `compose.yaml`.
#
# Run:  scripts/elway infra-ops@10.0.50.45 --playbook playbooks/homepage-regroup-esh-docker-vm.yaml

steps:
  - name: lobe-chat -> AI - Gateways & Chat
    sudo: true
    shell: >-
      sed -i 's|homepage.group=Apps$|homepage.group=AI - Gateways \& Chat|'
      /opt/docker/compose/lobe-chat/compose.yaml
    when: grep -q 'homepage.group=Apps$' /opt/docker/compose/lobe-chat/compose.yaml

  - name: open-webui -> AI - Gateways & Chat
    sudo: true
    shell: >-
      sed -i 's|homepage.group=Apps$|homepage.group=AI - Gateways \& Chat|'
      /opt/docker/compose/open-webui/compose.yaml
    when: grep -q 'homepage.group=Apps$' /opt/docker/compose/open-webui/compose.yaml

  # Site suffix — the ana instance is also called "Open WebUI" and the two now
  # sit side by side in the same group. See the sibling step in
  # playbooks/homepage-regroup-ana-docker.yaml.
  - name: open-webui -> "Open WebUI (esh)"
    sudo: true
    shell: >-
      sed -i 's|homepage.name=Open WebUI$|homepage.name=Open WebUI (esh)|'
      /opt/docker/compose/open-webui/compose.yaml
    when: grep -q 'homepage.name=Open WebUI$' /opt/docker/compose/open-webui/compose.yaml

  - name: mosquitto -> Agents (no UI)
    sudo: true
    shell: >-
      sed -i 's|homepage.group=Apps$|homepage.group=Agents (no UI)|'
      /opt/docker/compose/mosquitto/compose.yaml
    when: grep -q 'homepage.group=Apps$' /opt/docker/compose/mosquitto/compose.yaml

  - name: adguardhome -> DNS & Filtering
    sudo: true
    shell: >-
      sed -i 's|homepage.group=Service Networking$|homepage.group=DNS \& Filtering|'
      /opt/docker/compose/adguard/docker-compose.yml
    when: grep -q 'homepage.group=Service Networking$' /opt/docker/compose/adguard/docker-compose.yml

  - name: traefik -> Reverse Proxies
    sudo: true
    shell: >-
      sed -i 's|homepage.group=Service Networking$|homepage.group=Reverse Proxies|'
      /opt/docker/compose/traefik/docker-compose.yml
    when: grep -q 'homepage.group=Service Networking$' /opt/docker/compose/traefik/docker-compose.yml

  - name: dockge -> Compose Consoles
    sudo: true
    shell: >-
      sed -i 's|homepage.group=Service Networking$|homepage.group=Compose Consoles|'
      /opt/docker/compose/dockge/compose.yaml
    when: grep -q 'homepage.group=Service Networking$' /opt/docker/compose/dockge/compose.yaml

  # ---- recreates ---------------------------------------------------------
  - name: recreate lobe-chat
    sudo: true
    shell: cd /opt/docker/compose/lobe-chat && docker compose up -d lobe-chat

  - name: recreate open-webui
    sudo: true
    shell: cd /opt/docker/compose/open-webui && docker compose up -d open-webui

  - name: recreate mosquitto
    sudo: true
    shell: cd /opt/docker/compose/mosquitto && docker compose up -d mosquitto

  - name: recreate dockge
    sudo: true
    shell: cd /opt/docker/compose/dockge && docker compose up -d dockge

  - name: recreate adguardhome
    sudo: true
    shell: cd /opt/docker/compose/adguard && docker compose up -d adguardhome

  - name: recreate traefik
    sudo: true
    shell: cd /opt/docker/compose/traefik && docker compose up -d traefik

verify:
  - name: every relabelled container now carries its new group
    sudo: true
    changed_when: "false"
    shell: >-
      docker inspect -f '{{.Name}} {{index .Config.Labels "homepage.group"}}'
      lobe-chat open-webui mosquitto adguardhome traefik dockge

  - name: no container is left in the retired Service Networking group
    sudo: true
    changed_when: "false"
    shell: >-
      test -z "$(docker ps -q --filter 'label=homepage.group=Service Networking')"

  - name: adguard is back
    changed_when: "false"
    shell: >-
      for i in 1 2 3 4 5 6 7 8 9 10; do
      curl -sfL -o /dev/null -m 5 http://127.0.0.1:8080/ && exit 0;
      sleep 3; done; exit 1

  - name: traefik still routes
    changed_when: "false"
    shell: >-
      for i in 1 2 3 4 5 6 7 8 9 10; do
      curl -sfL -o /dev/null -m 5 http://127.0.0.1:8380/dashboard/ && exit 0;
      sleep 3; done; exit 1

  # ⚠ Must be 10.0.50.45, NOT 127.0.0.1. `HOMEPAGE_ALLOWED_HOSTS` matches
  # host AND port, and `127.0.0.1:5100` is not in the list — it answers 400
  # while the dashboard is perfectly healthy. The first run of this playbook
  # failed here on rc=22 for exactly that reason.
  - name: the dashboard itself is still served
    changed_when: "false"
    shell: curl -sf -o /dev/null -m 10 http://10.0.50.45:5100/api/services

  - name: everything is running
    sudo: true
    changed_when: "false"
    shell: >-
      test "$(docker inspect -f '{{.State.Running}}' lobe-chat open-webui
      mosquitto adguardhome traefik dockge | sort -u)" = "true"
