- name: Ensure user group for audio/video exists
  ansible.builtin.group:
    name: "{{ item }}"
    state: "present"
  loop: "{{ audio_groups }}"

- name: Put user in audio group
  ansible.builtin.user:
    name: "{{ user_name | default('root') }}"
    groups: "{{ audio_groups }}"
    append: true

- name: Install pipewire
  community.general.xbps:
    name:
      - pipewire
    state: "{{ desired_package_state | default('present') }}"
  tags: packages

- name: Install pipewire bluetooth
  community.general.xbps:
    name:
      - libspa-bluetooth
    state: "{{ desired_package_state | default('present') }}"
  tags:
    - packages
    - bluetooth

- name: Set up wireplumber to auto start
  ansible.builtin.file:
    dest: "/etc/pipewire/pipewire.conf.d"
    state: directory

  # FIXME: Does not work automatically for some reason?
- name: Set up wireplumber to auto start
  ansible.builtin.file:
    force: "yes"
    src: "/usr/share/examples/wireplumber/10-wireplumber.conf"
    dest: "/etc/pipewire/pipewire.conf.d/10-wireplumber.conf"
    state: link

- name: Enable pipewire-pulse interface
  ansible.builtin.file:
    force: "yes"
    src: "/usr/share/examples/pipewire/20-pipewire-pulse.conf"
    dest: "/etc/pipewire/pipewire.conf.d/20-pipewire-pulse.conf"
    state: link
  tags: pulseaudio

- name: Install alsa integration package
  community.general.xbps:
    name:
      - alsa-pipewire
    state: "{{ desired_package_state | default('present') }}"
  tags:
    - packages
    - alsa

- name: Set up wireplumber to auto start
  ansible.builtin.file:
    dest: "/etc/alsa/conf.d"
    state: directory

- name: Enable alsa-pipewire interface
  become: true
  ansible.builtin.file:
    force: "yes"
    src: "/usr/share/alsa/alsa.conf.d/50-pipewire.conf"
    dest: "/etc/alsa/conf.d/50-pipewire.conf"
    state: link
  tags: alsa

- name: Make alsa-pipewire interface default for alsa
  ansible.builtin.file:
    force: "yes"
    src: "/usr/share/alsa/alsa.conf.d/99-pipewire-default.conf"
    dest: "/etc/alsa/conf.d/99-pipewire-default.conf"
    state: link
  tags: alsa

# TODO: Find way to install and enable pipewire-roc-sink module (and enable ~/.config/pipewire/pipewire.conf.d/roc-sink.conf)