---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

- vars:
    pname: "{{ name_prefix }}-build"
    cname: "{{ name_prefix }}-container"
    iname: "{{ name_prefix }}-image"
    project_src: "{{ remote_tmp_dir }}/{{ pname }}"
    test_service: |
      services:
        {{ cname }}:
          build: ./build
          image: "{{ iname }}"
          pull_policy: never
          stop_grace_period: 1s

  block:
    - name: Registering container name
      set_fact:
        cnames: "{{ cnames + [pname ~ '-' ~ cname ~ '-1'] }}"
        dnetworks: "{{ dnetworks + [pname ~ '_default'] }}"
        images: "{{ images + [iname] }}"

    - name: Create project directory
      file:
        path: '{{ item }}'
        state: directory
      loop:
        - '{{ project_src }}'
        - '{{ project_src }}/build'

    - name: Template default project file
      copy:
        dest: '{{ project_src }}/docker-compose.yml'
        content: '{{ test_service }}'

    - name: Template Dockerfile
      copy:
        dest: '{{ project_src }}/build/Dockerfile'
        content: |
          FROM {{ docker_test_image_alpine }}
          ENTRYPOINT ["/bin/sh", "-c", "sleep 10m"]

    - name: Present (check)
      docker_compose_v2:
        project_src: '{{ project_src }}'
        state: present
      check_mode: true
      register: present_1_check

    - name: Present
      docker_compose_v2:
        project_src: '{{ project_src }}'
        state: present
      register: present_1

    - name: Present (idempotent check)
      docker_compose_v2:
        project_src: '{{ project_src }}'
        state: present
      check_mode: true
      register: present_2_check

    - name: Present (idempotent)
      docker_compose_v2:
        project_src: '{{ project_src }}'
        state: present
      register: present_2

    - name: Present (idempotent check, build=always, ignore_build_events=false)
      docker_compose_v2:
        project_src: '{{ project_src }}'
        state: present
        build: always
        ignore_build_events: false
      check_mode: true
      register: present_3_check

    - name: Present (idempotent, build=always, ignore_build_events=false)
      docker_compose_v2:
        project_src: '{{ project_src }}'
        state: present
        build: always
        ignore_build_events: false
      register: present_3

    - name: Present (idempotent check, build=always, ignore_build_events=true)
      docker_compose_v2:
        project_src: '{{ project_src }}'
        state: present
        build: always
        ignore_build_events: true
      check_mode: true
      register: present_4_check

    - name: Present (idempotent, build=always, ignore_build_events=true)
      docker_compose_v2:
        project_src: '{{ project_src }}'
        state: present
        build: always
        ignore_build_events: true
      register: present_4

    - assert:
        that:
          - present_1_check is changed
          - present_1_check.warnings | default([]) | select('regex', ' Please report this at ') | length == 0
          - present_1 is changed
          - present_1.containers | length == 1
          - present_1.containers[0].Name == (pname ~ '-' ~ cname ~ '-1')
          - present_1.images | length == 1
          - present_1.images[0].ContainerName == (pname ~ '-' ~ cname ~ '-1')
          - present_1.images[0].Repository == iname
          - present_1.images[0].Tag == "latest"
          - present_1.warnings | default([]) | select('regex', ' Please report this at ') | length == 0
          - present_2_check is not changed
          - present_2_check.warnings | default([]) | select('regex', ' Please report this at ') | length == 0
          - present_2 is not changed
          - present_2.warnings | default([]) | select('regex', ' Please report this at ') | length == 0
          - present_3_check is changed
          - present_3_check.warnings | default([]) | select('regex', ' Please report this at ') | length == 0
          - ((present_3 is changed) if docker_compose_version is version('2.31.0', '>=') and docker_compose_version is version('2.32.2', '<') else (present_3 is not changed))
          - present_3.warnings | default([]) | select('regex', ' Please report this at ') | length == 0
          - present_4_check is changed
          - present_4_check.warnings | default([]) | select('regex', ' Please report this at ') | length == 0
          - present_4 is not changed
          - present_4.warnings | default([]) | select('regex', ' Please report this at ') | length == 0

  always:
    - name: Cleanup
      docker_compose_v2:
        project_src: '{{ project_src }}'
        state: absent
