---
- name: 个人开发环境一键配置
  hosts: localhost
  connection: local
  become: true
  
  vars:
    go_version: "1.25.6"
    user_home: "/home/{{ ansible_env.SUDO_USER }}"

  tasks:
    - name: 更新 apt 缓存
      apt:
        update_cache: yes

    - name: 安装基础工具
      apt:
        name:
          - curl
          - git
          - wget
          - vim
          - build-essential
          - nano
        state: present

    - name: 下载 Go 语言包
      get_url:
        url: "https://golang.google.cn/dl/go{{ go_version }}.linux-amd64.tar.gz"
        dest: "/tmp/go.tar.gz"

    - name: 解压 Go 到 /usr/local
      unarchive:
        src: "/tmp/go.tar.gz"
        dest: /usr/local
        remote_src: yes
        creates: "/usr/local/go/bin/go" # 如果存在就不重复解压

    - name: 配置 GOPROXY (写入 .bashrc)
      lineinfile:
        path: "{{ user_home }}/.bashrc"
        line: "{{ item }}"
        create: yes
      loop:
        - 'export PATH=$PATH:/usr/local/go/bin'
        - 'export GOPROXY=https://goproxy.cn,direct'

    - name: 下载并运行 NodeSource 安装脚本 (安装 Node 20 LTS)
      shell: curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
      args:
        creates: /etc/apt/sources.list.d/nodesource.list 

    - name: 安装 Node.js (包含 npm)
      apt:
        name: nodejs
        state: present

    # ---------------------------------------------------
    # 5. Python 极速包管理器：UV (Rust编写)
    # ---------------------------------------------------
    - name: 安装 UV
      shell: curl -LsSf https://astral.sh/uv/install.sh | sh
      args:
        creates: "{{ ansible_env.HOME }}/.cargo/bin/uv"
        # UV 默认装在 ~/.cargo/bin 下，如果没有 rust 会装在 ~/.local/bin

    - name: 配置 UV 国内镜像 (写入 .bashrc)
      lineinfile:
        path: "{{ ansible_env.HOME }}/.bashrc"
        line: "{{ item }}"
        create: yes
      loop:
        - '# UV 配置'
        - 'export UV_PYPI_MIRROR=https://pypi.tuna.tsinghua.edu.cn/simple'
        # 让 UV 安装 Python 版本时也走国内/加速镜像 (如果有的话，目前 UV 主要是 PyPI 镜像)

    # ---------------------------------------------------
    # 6. Mamba (通过 Miniforge 安装，代替 Anaconda)
    # ---------------------------------------------------
    - name: 下载 Miniforge 安装脚本
      get_url:
        url: "https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh"
        dest: "/tmp/Miniforge3.sh"
        mode: '0755'

    - name: 自动安装 Miniforge (Mamba)
      shell: /tmp/Miniforge3.sh -b -p "{{ ansible_env.HOME }}/miniforge3"
      args:
        creates: "{{ ansible_env.HOME }}/miniforge3"

    - name: 初始化 Conda/Mamba Shell
      shell: "{{ ansible_env.HOME }}/miniforge3/bin/conda init bash"
      register: conda_init_result
      # 只有当输出中包含 "modified" 字样时，Ansible 才认为系统被修改了
      # 如果 Conda 说 "no change"，Ansible 就会显示绿色 (OK)
      changed_when: "'modified' in conda_init_result.stdout"

    - name: 配置 Conda/Mamba 国内源 (.condarc)
      copy:
        dest: "{{ ansible_env.HOME }}/.condarc"
        content: |
          channels:
            - defaults
          show_channel_urls: true
          default_channels:
            - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
            - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
            - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
          custom_channels:
            conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
            msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
            bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
            menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
            pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
            pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
            simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

    # ---------------------------------------------------
    # 7. Pixi (下一代 Conda 项目管理工具)
    # ---------------------------------------------------
    - name: 安装 Pixi
      shell: curl -fsSL https://pixi.sh/install.sh | bash
      args:
        creates: "{{ ansible_env.HOME }}/.pixi/bin/pixi"

    - name: 创建 Pixi 全局配置目录
      file:
        path: "{{ ansible_env.HOME }}/.config/pixi"
        state: directory

    - name: 配置 Pixi 镜像 (config.toml)
      copy:
        dest: "{{ ansible_env.HOME }}/.config/pixi/config.toml"
        content: |
          # 强制 Pixi 使用国内 Conda Forge 镜像
          default_channels = ["https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge"]

    # ---------------------------------------------------
    # 8. R 语言环境及全局换源
    # ---------------------------------------------------
    - name: 安装 R 基础包
      apt:
        name: 
          - r-base
          - r-base-dev
        state: present

    - name: 配置 R 全局 CRAN 镜像 (清华源)
      lineinfile:
        path: /etc/R/Rprofile.site
        line: 'local({r <- getOption("repos"); r["CRAN"] <- "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"; options(repos = r)})'
        create: yes
        state: present
      # 注意：这步需要 sudo 权限，become: true 会处理