---
- name: Install Python pip and devel
  ansible.builtin.dnf:
    name:
      - python3-pip
      - python3-devel
    state: present

- name: Create litellm system user
  ansible.builtin.user:
    name: "{{ litellm_user }}"
    system: true
    shell: /sbin/nologin
    home: /opt/litellm
    create_home: true
    state: present

- name: Install litellm[proxy] in venv
  ansible.builtin.pip:
    name: "litellm[proxy]"
    virtualenv: "{{ litellm_venv }}"
    virtualenv_command: python3 -m venv
    state: present

- name: Create config directory
  ansible.builtin.file:
    path: "{{ litellm_config_dir }}"
    state: directory
    owner: "{{ litellm_user }}"
    group: "{{ litellm_user }}"
    mode: '0750'

- name: Deploy LiteLLM config
  ansible.builtin.template:
    src: config.yaml.j2
    dest: "{{ litellm_config_dir }}/config.yaml"
    owner: "{{ litellm_user }}"
    group: "{{ litellm_user }}"
    mode: '0640'
  notify: Restart litellm

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

- name: Deploy hermes-switch script
  ansible.builtin.copy:
    src: hermes-switch
    dest: /usr/local/bin/hermes-switch
    mode: '0755'

- name: Enable and start litellm
  ansible.builtin.systemd:
    name: litellm
    enabled: true
    daemon_reload: true
    state: started

# ── Heartbeat : garde chaud le tuyau LiteLLM↔llama-server (anti-502 connexion périmée) ──
- name: Deploy llm-heartbeat script
  ansible.builtin.template:
    src: llm-heartbeat.sh.j2
    dest: /usr/local/bin/llm-heartbeat
    mode: '0755'
  notify: Restart llm-heartbeat

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

- name: Enable and start llm-heartbeat
  ansible.builtin.systemd:
    name: llm-heartbeat
    enabled: "{{ llm_heartbeat_enabled }}"
    state: "{{ 'started' if llm_heartbeat_enabled else 'stopped' }}"
    daemon_reload: true
