- hosts: localhost
  gather_facts: true
  vars_files:
    - ../group_vars/all.yml
  tasks:
    - name: Stop managed services
      command:
        argv:
          - "{{ courseware_root }}/scripts/service_manager.sh"
          - stop
          - all
      changed_when: false
      failed_when: false

    - name: Stat managed Unsloth symlink
      stat:
        path: "{{ ansible_env.HOME }}/.unsloth"
        follow: false
      register: courseware_down_unsloth

    - name: Stat managed Unsloth marker
      stat:
        path: "{{ ansible_env.HOME }}/.unsloth/.courseware-managed"
      register: courseware_down_unsloth_marker

    - name: Stat courseware-managed Ollama install marker
      stat:
        path: "{{ courseware_ollama_install_marker }}"
      register: courseware_down_ollama_marker

    - name: Stat Ollama systemd unit
      stat:
        path: /etc/systemd/system/ollama.service
      register: courseware_down_ollama_systemd_unit

    - name: Stat managed Unsloth launcher symlink
      stat:
        path: "{{ ansible_env.HOME }}/.local/bin/unsloth"
        follow: false
      register: courseware_down_unsloth_launcher

    - name: Stat managed Unsloth desktop entry
      stat:
        path: "{{ ansible_env.HOME }}/.local/share/applications/unsloth-studio.desktop"
      register: courseware_down_unsloth_desktop

    - name: Stat managed Unsloth desktop shortcut
      stat:
        path: "{{ ansible_env.HOME }}/Desktop/unsloth-studio.desktop"
      register: courseware_down_unsloth_desktop_shortcut

    - name: Stat managed Unsloth share directory
      stat:
        path: "{{ ansible_env.HOME }}/.local/share/unsloth/studio.conf"
      register: courseware_down_unsloth_share_conf

    - name: Stat managed llama.cpp PATH symlinks
      stat:
        path: "/usr/local/bin/{{ item }}"
        follow: false
      loop:
        - llama-cli
        - llama-quantize
        - llama-perplexity
        - llama-server
      register: courseware_down_llama_path_slots
      when: ansible_system == "Linux"

    - name: Stop and disable courseware-managed Ollama systemd service
      become: true
      systemd:
        name: ollama
        state: stopped
        enabled: false
      when:
        - ansible_service_mgr == "systemd"
        - courseware_down_ollama_marker.stat.exists
        - courseware_down_ollama_systemd_unit.stat.exists
      failed_when: false

    - name: Remove courseware-managed Ollama systemd unit
      become: true
      file:
        path: /etc/systemd/system/ollama.service
        state: absent
      when:
        - ansible_system == "Linux"
        - courseware_down_ollama_marker.stat.exists
        - courseware_down_ollama_systemd_unit.stat.exists
      failed_when: false

    - name: Reload systemd after Ollama unit removal
      become: true
      command: systemctl daemon-reload
      when:
        - ansible_system == "Linux"
        - ansible_service_mgr == "systemd"
        - courseware_down_ollama_marker.stat.exists
        - courseware_down_ollama_systemd_unit.stat.exists
      changed_when: false
      failed_when: false

    - name: Remove courseware-managed Ollama Linux install paths
      become: true
      file:
        path: "{{ item }}"
        state: absent
      loop:
        - /usr/local/bin/ollama
        - /usr/bin/ollama
        - /bin/ollama
        - /usr/local/lib/ollama
        - /usr/lib/ollama
        - /lib/ollama
        - /usr/share/ollama
        - /var/lib/ollama
      when:
        - ansible_system == "Linux"
        - courseware_down_ollama_marker.stat.exists
      failed_when: false

    - name: Remove courseware-managed Ollama Linux user
      become: true
      user:
        name: ollama
        state: absent
        remove: true
      when:
        - ansible_system == "Linux"
        - courseware_down_ollama_marker.stat.exists
      failed_when: false

    - name: Remove courseware-managed Ollama Linux group
      become: true
      group:
        name: ollama
        state: absent
      when:
        - ansible_system == "Linux"
        - courseware_down_ollama_marker.stat.exists
      failed_when: false

    - name: Stop courseware-managed Ollama macOS app if running
      command: pkill -x Ollama
      when:
        - ansible_system == "Darwin"
        - courseware_down_ollama_marker.stat.exists
      changed_when: false
      failed_when: false

    - name: Uninstall courseware-managed Ollama Homebrew formula
      command: brew uninstall ollama
      when:
        - ansible_system == "Darwin"
        - courseware_down_ollama_marker.stat.exists
      changed_when: false
      failed_when: false

    - name: Remove managed Unsloth path
      file:
        path: "{{ ansible_env.HOME }}/.unsloth"
        state: absent
      when:
        - courseware_down_unsloth.stat.exists
        - >
          (courseware_down_unsloth.stat.islnk and
           courseware_down_unsloth.stat.lnk_source == courseware_unsloth_home)
          or courseware_down_unsloth_marker.stat.exists
      failed_when: false

    - name: Remove managed Unsloth launcher symlink
      file:
        path: "{{ ansible_env.HOME }}/.local/bin/unsloth"
        state: absent
      when:
        - courseware_down_unsloth_launcher.stat.exists
        - courseware_down_unsloth_launcher.stat.islnk
        - courseware_down_unsloth_launcher.stat.lnk_source == (courseware_unsloth_home ~ "/studio/unsloth_studio/bin/unsloth")
      failed_when: false

    - name: Remove managed Unsloth desktop entry
      file:
        path: "{{ ansible_env.HOME }}/.local/share/applications/unsloth-studio.desktop"
        state: absent
      when: courseware_down_unsloth_desktop.stat.exists
      failed_when: false

    - name: Remove managed Unsloth desktop shortcut
      file:
        path: "{{ ansible_env.HOME }}/Desktop/unsloth-studio.desktop"
        state: absent
      when: courseware_down_unsloth_desktop_shortcut.stat.exists
      failed_when: false

    - name: Remove managed Unsloth share directory
      file:
        path: "{{ ansible_env.HOME }}/.local/share/unsloth"
        state: absent
      when:
        - courseware_down_unsloth_share_conf.stat.exists
        - lookup('file', ansible_env.HOME ~ '/.local/share/unsloth/studio.conf', errors='ignore') is search(courseware_unsloth_home ~ '/studio/unsloth_studio/bin/unsloth')
      failed_when: false

    - name: Remove managed Unsloth Windows shortcuts
      command: >
        powershell.exe -NoProfile -Command
        "$locations = @([Environment]::GetFolderPath('Desktop'), (Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs'));
        foreach ($dir in $locations) {
          if (-not $dir -or -not (Test-Path $dir)) { continue }
          $linkPath = Join-Path $dir 'Unsloth Studio.lnk';
          if (Test-Path $linkPath) { Remove-Item -LiteralPath $linkPath -Force -ErrorAction SilentlyContinue }
        }"
      when:
        - ansible_system == "Linux"
        - "'microsoft' in ansible_kernel | lower or 'wsl' in ansible_kernel | lower"
      changed_when: false
      failed_when: false

    - name: Remove managed llama.cpp PATH symlinks
      become: true
      file:
        path: "/usr/local/bin/{{ item.item }}"
        state: absent
      loop: "{{ courseware_down_llama_path_slots.results | default([]) }}"
      when:
        - ansible_system == "Linux"
        - item.stat.exists
        - item.stat.islnk
        - item.stat.lnk_source == (courseware_llama_cpp_bin_dir ~ '/' ~ item.item)
      failed_when: false

    - name: Remove project-owned state directory
      file:
        path: "{{ courseware_state_dir }}"
        state: absent
