# SPDX-FileCopyrightText: 2023 Michael Hollister
# SPDX-FileCopyrightText: 2024 - 2025 Slavi Pantaleev
# SPDX-FileCopyrightText: 2025 Suguru Hirahara
#
# SPDX-License-Identifier: AGPL-3.0-or-later

---

- name: (Deprecation) Catch and report renamed matrix-media-repo settings
  ansible.builtin.fail:
    msg: >-
      Your configuration contains a variable, which now has a different name.
      Please rename the variable (`{{ item.old }}` -> `{{ item.new }}`) on your configuration file (vars.yml).
  when: "lookup('ansible.builtin.varnames', ('^' + item.old + '$'), wantlist=True) | length > 0"
  with_items:
    - {'old': 'matrix_media_repo_access_tokens', 'new': '<flattened into multiple matrix_media_repo_access_tokens_XXX variables - see roles/custom/matrix-media-repo/defaults/main.yml>'}
    - {'old': 'matrix_media_repo_datastores', 'new': '<flattened into matrix_media_repo_datastores_XXX variables - see roles/custom/matrix-media-repo/defaults/main.yml>'}
    - {'old': 'matrix_media_repo_uploads', 'new': '<flattened into multiple matrix_media_repo_uploads_XXX variables - see roles/custom/matrix-media-repo/defaults/main.yml>'}
    - {'old': 'matrix_media_repo_downloads_cache_enabled', 'new': '<removed>'}
    - {'old': 'matrix_media_repo_downloads_cache_max_size_bytes', 'new': '<removed>'}
    - {'old': 'matrix_media_repo_downloads_cache_max_file_size_bytes', 'new': '<removed>'}
    - {'old': 'matrix_media_repo_downloads_cache_tracked_minutes', 'new': '<removed>'}
    - {'old': 'matrix_media_repo_downloads_cache_min_downloads', 'new': '<removed>'}
    - {'old': 'matrix_media_repo_downloads_cache_min_cache_time_seconds', 'new': '<removed>'}
    - {'old': 'matrix_media_repo_downloads_cache_min_evicted_time_seconds', 'new': '<removed>'}
    - {'old': 'matrix_media_repo_url_previews', 'new': '<flattened into multiple matrix_media_repo_url_previews_XXX variables - see roles/custom/matrix-media-repo/defaults/main.yml>'}
    - {'old': 'matrix_media_repo_thumbnails', 'new': '<flattened into multiple matrix_media_repo_thumbnails_XXX variables - see roles/custom/matrix-media-repo/defaults/main.yml>'}
    - {'old': 'matrix_media_repo_feature_support', 'new': '<removed>'}
    - {'old': 'matrix_media_repo_container_labels_traefik_logout_path_prefix', 'new': 'matrix_media_repo_container_labels_traefik_logout_path_regexp'}
    - {'old': 'matrix_media_repo_container_labels_traefik_admin_path_prefix', 'new': 'matrix_media_repo_container_labels_traefik_admin_path_regexp'}
    - {'old': 'matrix_media_repo_container_labels_traefik_logout_federation_path_prefix', 'new': 'matrix_media_repo_container_labels_traefik_logout_federation_path_regexp'}
    - {'old': 'matrix_media_repo_container_labels_traefik_admin_federation_path_prefix', 'new': 'matrix_media_repo_container_labels_traefik_admin_federation_path_regexp'}
    - {'old': 'matrix_media_repo_docker_image_name_prefix', 'new': 'matrix_media_repo_container_image_registry_prefix'}
    - {'old': 'matrix_media_repo_docker_image', 'new': 'matrix_media_repo_container_image'}
    - {'old': 'matrix_media_repo_docker_image_force_pull', 'new': 'matrix_media_repo_container_image_force_pull'}
    - {'old': 'matrix_media_repo_docker_image_path', 'new': 'matrix_media_repo_container_image_path'}
    - {'old': 'matrix_media_repo_docker_image_registry_prefix', 'new': 'matrix_media_repo_container_image_registry_prefix'}
    - {'old': 'matrix_media_repo_docker_image_registry_prefix_upstream', 'new': 'matrix_media_repo_container_image_registry_prefix_upstream'}
    - {'old': 'matrix_media_repo_docker_image_registry_prefix_upstream_default', 'new': 'matrix_media_repo_container_image_registry_prefix_upstream_default'}
    - {'old': 'matrix_media_repo_docker_image_tag', 'new': 'matrix_media_repo_container_image_tag'}
    - {'old': 'matrix_media_repo_docker_src_files_path', 'new': 'matrix_media_repo_container_src_files_path'}
    - {'old': 'matrix_media_repo_container_image_force_pull', 'new': '<removed> (the new community.docker.docker_image_pull module handles this natively)'}

- name: Fail if required matrix-media-repo settings not defined
  ansible.builtin.fail:
    msg: >
      You need to define a required configuration setting (`{{ item.name }}`).
  when: "item.when | bool and lookup('vars', item.name, default='') | string | length == 0"
  with_items:
    - {'name': 'matrix_media_repo_database_hostname', when: true}
    - {'name': 'matrix_media_repo_container_labels_traefik_internal_media_entrypoints', when: "{{ matrix_media_repo_container_labels_traefik_internal_media_enabled }}"}
    - {'name': 'matrix_media_repo_container_labels_traefik_internal_matrix_client_media_entrypoints', when: "{{ matrix_media_repo_container_labels_traefik_internal_matrix_client_media_enabled }}"}
    - {'name': 'matrix_media_repo_datastore_file_id', when: "{{ (matrix_media_repo_datastore_file_for_kinds | length) > 0 }}"}
    - {'name': 'matrix_media_repo_datastore_s3_id', when: "{{ (matrix_media_repo_datastore_s3_for_kinds | length) > 0 }}"}
