---
# Ensure Required Directories Exist
- name: Ensure matrix-element-call paths exist
  ansible.builtin.file:
    path: "{{ item.path }}"
    state: directory
    mode: 0750
    owner: "{{ matrix_user_username }}"
    group: "{{ matrix_user_groupname }}"
  loop:
    - path: "{{ matrix_element_call_base_path }}"
    - path: "{{ matrix_element_call_base_path }}/data"
    - path: "{{ matrix_element_call_base_path }}/config"
    - path: "{{ matrix_element_call_base_path }}/backend"  # For LiveKit and Redis config

# Ensure Configuration Files are in Place
- name: Ensure Element Call config.json is in place
  ansible.builtin.template:
    src: "{{ role_path }}/templates/config.json.j2"
    dest: "{{ matrix_element_call_base_path }}/config/config.json"
    mode: 0640
    owner: "{{ matrix_user_username }}"
    group: "{{ matrix_user_groupname }}"

- name: Ensure LiveKit livekit.yaml is in place
  ansible.builtin.template:
    src: "{{ role_path }}/templates/livekit.yaml.j2"
    dest: "{{ matrix_element_call_base_path }}/backend/livekit.yaml"
    mode: 0640
    owner: "{{ matrix_user_username }}"
    group: "{{ matrix_user_groupname }}"

- name: Ensure Redis redis.conf is in place
  ansible.builtin.template:
    src: "{{ role_path }}/templates/redis.conf.j2"
    dest: "{{ matrix_element_call_base_path }}/backend/redis.conf"
    mode: 0640
    owner: "{{ matrix_user_username }}"
    group: "{{ matrix_user_groupname }}"

- name: Ensure matrix-element-call environment file is in place
  ansible.builtin.template:
    src: "{{ role_path }}/templates/env.j2"
    dest: "{{ matrix_element_call_base_path }}/config/env"
    mode: 0640
    owner: "{{ matrix_user_username }}"
    group: "{{ matrix_user_groupname }}"

- name: Ensure matrix-element-call Docker labels file is in place
  ansible.builtin.template:
    src: "{{ role_path }}/templates/labels.j2"
    dest: "{{ matrix_element_call_base_path }}/config/labels"
    mode: 0640
    owner: "{{ matrix_user_username }}"
    group: "{{ matrix_user_groupname }}"

# Ensure Docker Images are Pulled
- name: Ensure matrix-element-call Docker image is pulled
  community.docker.docker_image:
    name: "{{ matrix_element_call_image }}"
    source: pull
    force_source: "{{ matrix_element_call_container_image_force_pull }}"
  register: element_call_image_result
  retries: "{{ devture_playbook_help_container_retries_count }}"
  delay: "{{ devture_playbook_help_container_retries_delay }}"
  until: element_call_image_result is not failed

- name: Ensure jwt-service Docker image is pulled
  community.docker.docker_image:
    name: "ghcr.io/element-hq/lk-jwt-service:latest-ci"
    source: pull
  register: jwt_image_result
  retries: 3
  delay: 10
  until: jwt_image_result is not failed

- name: Ensure livekit Docker image is pulled
  community.docker.docker_image:
    name: "livekit/livekit-server:latest"
    source: pull
  register: livekit_image_result
  retries: 3
  delay: 10
  until: livekit_image_result is not failed

- name: Ensure redis Docker image is pulled
  community.docker.docker_image:
    name: "redis:6-alpine"
    source: pull
  register: redis_image_result
  retries: 3
  delay: 10
  until: redis_image_result is not failed

- name: Debug matrix_element_call_base_path
  ansible.builtin.debug:
    var: matrix_element_call_base_path

- name: Debug labels file content
  ansible.builtin.debug:
    msg: "{{ lookup('file', matrix_element_call_base_path ~ '/config/labels') | from_yaml }}"

# Ensure Docker Containers are Running
- name: Run matrix-element-call Docker container
  community.docker.docker_container:
    name: "matrix-element-call"
    image: "{{ matrix_element_call_image }}"
    state: started
    restart_policy: unless-stopped
    env_file: "{{ matrix_element_call_base_path }}/config/env"
    labels: "{{ lookup('file', matrix_element_call_base_path ~ '/config/labels') | from_yaml }}"
    networks:
      - name: "{{ matrix_element_call_container_network }}"
    volumes:
      - "{{ matrix_element_call_base_path }}/config/config.json:/app/config.json"
      - "{{ matrix_element_call_base_path }}/data:/data"

- name: Run jwt-service Docker container
  community.docker.docker_container:
    name: "matrix-jwt-service"
    image: "ghcr.io/element-hq/lk-jwt-service:latest-ci"
    state: started
    restart_policy: unless-stopped
    environment:
      LIVEKIT_SECRET: "{{ matrix_element_call_livekit_dev_key }}"  # User-specified key
      LIVEKIT_URL: "{{ matrix_element_call_livekit_service_url }}"
      LIVEKIT_KEY: "devkey"
    networks:
      - name: "{{ matrix_element_call_container_network }}"

- name: Run livekit Docker container
  community.docker.docker_container:
    name: "matrix-livekit"
    image: "livekit/livekit-server:latest"
    state: started
    restart_policy: unless-stopped
    command: "--dev --config /etc/livekit.yaml"
    volumes:
      - "{{ matrix_element_call_base_path }}/backend/livekit.yaml:/etc/livekit.yaml"
    network_mode: "host"

- name: Run redis Docker container
  community.docker.docker_container:
    name: "matrix-redis"
    image: "redis:6-alpine"
    state: started
    restart_policy: unless-stopped
    command: redis-server /etc/redis.conf
    volumes:
      - "{{ matrix_element_call_base_path }}/backend/redis.conf:/etc/redis.conf"
    networks:
      - name: "{{ matrix_element_call_container_network }}"

# Ensure Systemd Services are Set Up
- name: Ensure matrix-element-call systemd service is installed
  ansible.builtin.template:
    src: "{{ role_path }}/templates/element-call.service.j2"
    dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-element-call.service"
    mode: 0644

- name: Ensure matrix-element-call systemd service is enabled and started
  ansible.builtin.systemd:
    name: matrix-element-call
    enabled: true
    state: started
    daemon_reload: true