---
- name: Merge variables
  block:
  - name: Merge users
    set_fact:
      users: "{{ default_users | combine(users| default({}), recursive=True) }}"

  - name: Merge system_email definitions
    set_fact:
      system_email: "{{ default_system_email | combine(system_email | default({}, true), recursive=True) }}"

  - name: Merge application definitions
    set_fact:
      applications: "{{ defaults_applications | combine(applications | default({}, true), recursive=True) }}"

  - name: Merge current play applications
    set_fact:
      current_play_applications: >-
        {{ 
          applications |
          applications_if_group_and_deps(group_names)
        }}

  - name: Merge current play domain definitions
    set_fact:
      current_play_domains: >- 
        {{  current_play_applications |
            canonical_domains_map(primary_domain) | 
            combine(domains | default({}, true), recursive=True) 
        }}
  
  - name: Merge domain definitions for all domains
    set_fact:
      domains: >-
        {{ 
          defaults_applications | 
          canonical_domains_map(primary_domain) | 
          combine(domains | default({}, true), recursive=True) 
        }}

  - name: Merge redirect_domain_mappings
    set_fact:
      # The following mapping is necessary to define the exceptions for domains which are created, but which aren't used
      redirect_domain_mappings: "{{
        []  | 
        add_redirect_if_group('assets-server',    domains | get_domain('assets-server'), domains | get_domain('file-server'),  group_names) | 
        merge_mapping(redirect_domain_mappings, 'source')
      }}"

  - name: Set current play redirect domain mappings 
    set_fact:
      current_play_domain_mappings_redirect: >-
        {{ 
          current_play_applications |
          domain_mappings(primary_domain) | 
          merge_mapping(redirect_domain_mappings, 'source') 
        }}

  - name: Set current play all domains incl. www redirect if enabled
    set_fact:
      current_play_domains_all: >-
        {{
          (current_play_domains |
          combine(
            current_play_domain_mappings_redirect | 
            items2dict(key_name='target', value_name='source'),
            recursive=True
          )) |
          generate_all_domains(
            ('www_redirect' in group_names)
          )
        }}

  - name: Merge networks definitions
    set_fact:
      networks: "{{ defaults_networks | combine(networks | default({}, true), recursive=True) }}"
  
  - name: Merge oidc configuration
    set_fact:
      oidc: "{{ defaults_oidc | combine(oidc | default({}, true), recursive=True) }}"

  - name: Merge design configuration
    set_fact:
      design: "{{ defaults_design | combine(design | default({}, true), recursive=True) }}"

  - name: Merge service_provider configuration
    set_fact:
      service_provider: "{{ defaults_service_provider | combine(service_provider | default({}, true), recursive=True) }}"

- name: init root user
  include_role:
    name: user-root

- name: update device
  include_role:
    name: update
  when: mode_update | bool

- name: setup standard wireguard
  when: ("wireguard_server" in group_names)
  include_role:
    name: wireguard

# vpn setup
- name: setup wireguard client behind firewall\nat
  when: ("wireguard_behind_firewall" in group_names)
  include_role:
    name: client-wireguard-behind-firewall

- name: setup wireguard client
  when: ("wireguard_client" in group_names)
  include_role:
    name: client-wireguard

## backup setup
- name: setup replica backup hosts
  when: ("backup_remote_to_local" in group_names)
  include_role:
    name: backup-remote-to-local

- name: setup backup to swappable
  when: ("backup_to_usb" in group_names)
  include_role:
    name: backup-data-to-usb

## driver setup
- name: driver-intel
  when: ("intel" in group_names)
  include_role:
    name: driver-intel

- name: setup multiprinter hosts
  when: ("epson_multiprinter" in group_names)
  include_role:
    name: driver-epson-multiprinter


- name: setup hibernate lid switch
  when: ("driver-lid-switch" in group_names)
  include_role:
    name: driver-lid-switch

## system setup 
- name: setup swapfile hosts
  when: ("swapfile" in group_names)
  include_role:
    name: system-swapfile