- name: Create Promptfoo working directories
  file:
    path: "{{ item }}"
    state: directory
    mode: "0755"
  loop:
    - "{{ courseware_tools_dir }}/promptfoo"
    - "{{ courseware_lab6_dir }}"

- name: Install Promptfoo locally
  command: "npm install promptfoo@{{ courseware_promptfoo_version }}"
  args:
    chdir: "{{ courseware_tools_dir }}/promptfoo"
  environment:
    PATH: "{{ courseware_node_runtime_bin_dir }}:{{ ansible_env.PATH }}"

- name: Force Promptfoo server to bind to the configured host
  replace:
    path: "{{ courseware_tools_dir }}/promptfoo/node_modules/promptfoo/dist/src/server/server.js"
    regexp: '\.listen\(port, \(\) => \{'
    replace: ".listen(port, process.env.COURSEWARE_BIND_HOST || '0.0.0.0', () => {"

- name: Render promptfoo starter config
  template:
    src: "{{ playbook_dir }}/../templates/promptfoo.yaml.j2"
    dest: "{{ courseware_lab6_dir }}/promptfoo.yaml"
    mode: "0644"
