---
- name: Install build dependencies
  ansible.builtin.dnf:
    name:
      - cmake
      - gcc-c++
      - git
      - patchelf
      - hipblas-devel
      - rocblas-devel
      - hip-devel
      - hipcc
    state: present

- name: Clone llama.cpp
  ansible.builtin.git:
    repo: https://github.com/ggerganov/llama.cpp
    dest: /opt/llama.cpp
    depth: 1
    version: "{{ llama_server_commit | default('HEAD') }}"
  register: llama_clone

- name: Check if llama-server binary exists
  ansible.builtin.stat:
    path: /opt/llama.cpp/build/bin/llama-server
  register: llama_server_binary_stat

- name: Configure cmake build (ROCm HIP)
  ansible.builtin.command:
    cmd: >
      cmake -B build
      -DGGML_HIP=ON
      -DAMDGPU_TARGETS={{ llama_amdgpu_targets }}
      -DCMAKE_BUILD_TYPE=Release
      -DROCM_PATH={{ rocm_path }}
      -DCMAKE_PREFIX_PATH={{ rocm_path }}
      -DCMAKE_HIP_COMPILER={{ rocm_path }}/llvm/bin/clang++
    chdir: /opt/llama.cpp
  environment:
    PATH: "{{ rocm_path }}/bin:{{ ansible_env.PATH }}"
  when: llama_clone is changed or not llama_server_binary_stat.stat.exists

- name: Build llama-server
  ansible.builtin.command:
    cmd: cmake --build build --target llama-server -j{{ ansible_processor_nproc }}
    chdir: /opt/llama.cpp
  environment:
    PATH: "{{ rocm_path }}/bin:{{ ansible_env.PATH }}"
  when: llama_clone is changed or not llama_server_binary_stat.stat.exists

- name: Deploy systemd service
  ansible.builtin.template:
    src: llama-server.service.j2
    dest: /etc/systemd/system/llama-server.service
    mode: '0644'
  notify:
    - Reload systemd
    - Restart llama-server

- name: Enable llama-server service
  ansible.builtin.systemd:
    name: llama-server
    enabled: true
    daemon_reload: true
    state: started

- name: Open port in firewall
  ansible.posix.firewalld:
    port: "{{ llama_server_port }}/tcp"
    permanent: true
    state: enabled
    immediate: true

- name: Disable lm-studio service (replaced by llama-server)
  ansible.builtin.systemd:
    name: lm-studio
    enabled: false
    state: stopped
  ignore_errors: true

# --- Instance dédiée embeddings (nomic-embed-text) — optionnelle ---------------

- name: Ensure embedding model directory exists
  ansible.builtin.file:
    path: "{{ llama_embed_model_path | dirname }}"
    state: directory
    mode: '0755'
  when: llama_embed_enabled and llama_embed_model_path | length > 0

- name: Download embedding model (GGUF) if absent
  ansible.builtin.get_url:
    url: "{{ llama_embed_model_url }}"
    dest: "{{ llama_embed_model_path }}"
    mode: '0644'
    timeout: 120
  when:
    - llama_embed_enabled
    - llama_embed_model_url | length > 0
    - llama_embed_model_path | length > 0

- name: Deploy embedding llama-server service
  ansible.builtin.template:
    src: llama-embed.service.j2
    dest: /etc/systemd/system/llama-embed.service
    mode: '0644'
  notify:
    - Reload systemd
    - Restart llama-embed
  when: llama_embed_enabled

- name: Enable embedding llama-server service
  ansible.builtin.systemd:
    name: llama-embed
    enabled: true
    daemon_reload: true
    state: started
  when: llama_embed_enabled

- name: Open embedding port in firewall
  ansible.posix.firewalld:
    port: "{{ llama_embed_port }}/tcp"
    permanent: true
    state: enabled
    immediate: true
  when: llama_embed_enabled

# --- Heartbeat embed (garde chaud le slot :1238) — optionnel ------------------

- name: Deploy llama-embed-heartbeat script
  ansible.builtin.template:
    src: llama-embed-heartbeat.sh.j2
    dest: /usr/local/bin/llama-embed-heartbeat
    mode: '0755'
  notify: Restart llama-embed-heartbeat
  when: llama_embed_enabled and llama_embed_heartbeat_enabled

- name: Deploy llama-embed-heartbeat service
  ansible.builtin.template:
    src: llama-embed-heartbeat.service.j2
    dest: /etc/systemd/system/llama-embed-heartbeat.service
    mode: '0644'
  notify:
    - Reload systemd
    - Restart llama-embed-heartbeat
  when: llama_embed_enabled and llama_embed_heartbeat_enabled

- name: Enable llama-embed-heartbeat service
  ansible.builtin.systemd:
    name: llama-embed-heartbeat
    enabled: true
    daemon_reload: true
    state: started
  when: llama_embed_enabled and llama_embed_heartbeat_enabled

# --- Watchdog auto-réparation (wedge ROCm) — optionnel ------------------------

- name: Deploy llama-watchdog script
  ansible.builtin.template:
    src: llama-watchdog.sh.j2
    dest: /usr/local/bin/llama-watchdog
    mode: '0755'
  notify: Restart llama-watchdog
  when: llama_watchdog_enabled

- name: Deploy llama-watchdog service
  ansible.builtin.template:
    src: llama-watchdog.service.j2
    dest: /etc/systemd/system/llama-watchdog.service
    mode: '0644'
  notify:
    - Reload systemd
    - Restart llama-watchdog
  when: llama_watchdog_enabled

- name: Enable llama-watchdog service
  ansible.builtin.systemd:
    name: llama-watchdog
    enabled: true
    daemon_reload: true
    state: started
  when: llama_watchdog_enabled
