- name: Phase 1 - Prepare and Pull FSL Client
  hosts: clients
  become: true
  serial: 3
  vars:
    session_id: "manual_run"
    scenario_id: ""
    image_tag: "latest"
    client_image: "{{ registry | default('cindyncl26') }}/fsl-client:{{ image_tag }}"

  tasks:
    - name: Ensure directories exist
      ansible.builtin.file:
        path: "{{ item }}"
        state: directory
        mode: '0755'
      loop:
        - /home/pi/dataset/processed
        - /home/pi/dataset/holdout
        - /home/pi/results
        - /home/pi/bestweights

    - name: Synchronize dataset from Mac to Pis (this may take a few mins)
      ansible.posix.synchronize:
        src: ../dataset/processed/
        dest: /home/pi/dataset/processed/
        archive: true
        compress: true
        rsync_opts:
          - "--ignore-existing"  # Only upload missing files to save time

    - name: Sync config.yaml to Pi
      ansible.posix.synchronize:
        src: ../config.yaml
        dest: /home/pi/config.yaml
        archive: false
        checksum: true

    - name: Sync matrix.yaml to Pi
      ansible.posix.synchronize:
        src: ../matrix.yaml
        dest: /home/pi/matrix.yaml
        archive: false
        checksum: true

    - name: Pull FSL client image
      ansible.builtin.shell: docker pull {{ client_image }}
      register: pull_result
      changed_when: "'Downloaded newer image' in pull_result.stdout"
      tags: [pull]

- name: Phase 2 - Start FSL Client (Simultaneous)
  hosts: clients
  become: true
  serial: "100%"
  vars:
    session_id: "manual_run"
    scenario_id: ""
    image_tag: "latest"
    client_image: "{{ registry | default('cindyncl26') }}/fsl-client:{{ image_tag }}"
  tasks:
    - name: Remove old fsl-client container (if exists)
      ansible.builtin.shell: docker stop fsl-client 2>/dev/null; docker rm fsl-client 2>/dev/null; true
      changed_when: false
      tags: [run]

    - name: Start fsl-client container
      ansible.builtin.shell: >
        docker run -d
        --name fsl-client
        --restart no
        -e CLIENT_ID={{ client_id }}
        -e FSL_SERVER_HOST={{ server_host }}
        -e FSL_CONFIG_PATH=/app/config.yaml
        -e PYTHONUNBUFFERED=1
        -e PYTHONPATH=/app
        -e FSL_DEVICE=cpu
        -e SESSION_ID={{ session_id }}
        -e SCENARIO_ID={{ scenario_id }}
        -v /home/pi/config.yaml:/app/config.yaml:ro
        -v /home/pi/dataset/processed:/app/dataset/processed:ro
        -v /home/pi/matrix.yaml:/app/matrix.yaml:ro
        -v /home/pi/results:/app/results
        -v /home/pi/bestweights:/app/bestweights
        {{ client_image }}
        python src/nodes/run_scenario_loop.py --role client
      tags: [run]

    - name: Show container status
      ansible.builtin.command: docker ps --filter name=fsl-client
      register: container_status
      changed_when: false
      tags: [run]

    - name: Print container status
      ansible.builtin.debug:
        msg: "{{ container_status.stdout_lines }}"
      tags: [run]
