---
-
  name: "Expanding System Disk (C:)"
  block:
    -
      name: "Extending the System Drive to maximum capacity"
      ansible.windows.win_powershell:
        script: |
          # Get the system drive (typically C:)
          $systemDrive = $env:SystemDrive.TrimEnd(':')
          
          # Check if the partition can be extended
          try {
            $size = Get-PartitionSupportedSize -DriveLetter $systemDrive -ErrorAction Stop
            
            if ($size.SizeMax -gt (Get-Partition -DriveLetter $systemDrive).Size) {
              # Extend the partition to maximum supported size
              Resize-Partition -DriveLetter $systemDrive -Size $size.SizeMax
              $changed = $true
              $message = "System drive $systemDrive extended successfully"
            } else {
              $changed = $false
              $message = "System drive $systemDrive already at maximum size"
            }
            $errorMsg = $null
          } catch {
            $changed = $false
            $message = "Unable to extend system drive"
            $errorMsg = $_.Exception.Message
          }
          
          # Return structured output
          $result = @{ 
            changed = $changed
            message = $message
            error = $errorMsg
          }
          
          return $result
      register: extend_system_drive
      changed_when: extend_system_drive.result.changed | default(false)
      failed_when: extend_system_drive.result.error is defined and extend_system_drive.result.error != null
-
  name: "Configuring Additional Disks"
  when:
    - disks.additional_disks is defined
    - disks.additional_disks is not none
    - disks.additional_disks | length > 0
  block:
    -
      name: "Gathering Windows disk information"
      community.windows.win_disk_facts:
      register: windows_disk_facts

    -
      name: "Debugging: Windows disk facts structure"
      ansible.builtin.debug:
        var: windows_disk_facts

    -
      name: "Identifying available uninitialized disks"
      ansible.builtin.set_fact:
        available_uninitialized_disks: >-
          {{
            windows_disk_facts.ansible_facts.ansible_disks |
            selectattr('system_disk', 'equalto', false) |
            selectattr('partition_style', 'equalto', 'RAW') |
            map(attribute='number') |
            list
          }}

    -
      name: "Debugging: Available uninitialized disks"
      ansible.builtin.debug:
        var: available_uninitialized_disks

    -
      name: "Initializing additional disks"
      when: 
        - available_uninitialized_disks | length > 0
        - index < available_uninitialized_disks | length
      community.windows.win_initialize_disk:
        disk_number: "{{ available_uninitialized_disks[index] }}"
        style: gpt
      loop: "{{ disks.additional_disks | default([], true) }}"
      loop_control:
        index_var: index

    -
      name: "Creating partitions on additional disks"
      when: 
        - available_uninitialized_disks | length > 0
        - index < available_uninitialized_disks | length
      community.windows.win_partition:
        disk_number: "{{ available_uninitialized_disks[index] }}"
        drive_letter: "{{ item.drive_letter | default('auto') }}"
        partition_size: "{{ item.size | default('-1') | regex_replace('([GT])$', '\\1B') }}"
        state: present
      loop: "{{ disks.additional_disks | default([], true) }}"
      loop_control:
        index_var: index
      register: created_partitions

    -
      name: "Formatting additional disk partitions"
      when: 
        - available_uninitialized_disks | length > 0
        - index < available_uninitialized_disks | length
        - created_partitions is defined
        - created_partitions.results[index] is defined
        - created_partitions.results[index].changed
      community.windows.win_format:
        drive_letter: "{{ created_partitions.results[index].drive_letter | default(item.drive_letter | default('auto')) }}"
        file_system: "{{ (item.filesystem | default('NTFS')).replace('ext4', 'NTFS') | upper }}"
        new_label: "{{ item.label | default(item.volume_name | default('Data' + (index + 1) | string)) }}"
        full: false
      loop: "{{ disks.additional_disks | default([], true) }}"
      loop_control:
        index_var: index

    -
      name: "Displaying configured additional disks summary"
      ansible.builtin.debug:
        msg: >-
          Configured disk {{ index + 1 }}:
          Drive Letter: {{ created_partitions.results[index].drive_letter | default(item.drive_letter | default('auto-assigned')) }},
          Size: {{ item.size | default('Maximum available') }},
          Filesystem: {{ item.filesystem | default('NTFS') }},
          Label: {{ item.label | default('Data' + (index + 1) | string) }}
      when:
        - available_uninitialized_disks | length > 0
        - index < available_uninitialized_disks | length
        - created_partitions is defined
        - created_partitions.results[index] is defined
      loop: "{{ disks.additional_disks | default([], true) }}"
      loop_control:
        index_var: index
