---
# Common setup tasks - runs on all platforms

- name: Ensure required system packages are installed (Debian/Ubuntu)
  ansible.builtin.apt:
    name:
      - python3
      - python3-pip
      - git
      - curl
      - wget
      - build-essential
      - cmake
      - ninja-build
      - libssl-dev
      - pkg-config
      - zstd
    state: present
    update_cache: no
  when: ansible_os_family == "Debian"
  become: yes

- name: Ensure Homebrew is installed (macOS)
  ansible.builtin.homebrew:
    name:
      - python3
      - git
      - curl
      - cmake
      - ninja
    state: present
  when: ansible_os_family == "Darwin"

- name: Install Python virtual environment module (user space)
  ansible.builtin.pip:
    name: virtualenv
    state: present
    executable: pip3
    extra_args: "--break-system-packages"
  become: no
  when: ansible_os_family == "Debian"

- name: Create lab base directory structure
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    mode: '0755'
  loop:
    - "{{ llmlab_base }}/lab1"
    - "{{ llmlab_base }}/lab2"
    - "{{ llmlab_base }}/lab3"
    - "{{ llmlab_base }}/lab4"
    - "{{ llmlab_base }}/lab5"
    - "{{ llmlab_base }}/lab6"
    - "{{ llmlab_base }}/.llmlab"
    - "{{ llmlab_base }}/.llmlab/logs"
  become: no

- name: Create .local/bin directory
  ansible.builtin.file:
    path: "{{ llmlab_base }}/.local/bin"
    state: directory
    mode: '0755'
  become: no

- name: Copy common environment script
  ansible.builtin.copy:
    src: "{{ playbook_dir }}/roles/common/files/env"
    dest: "{{ llmlab_base }}/.local/bin/env"
    mode: '0755'
    force: yes
  notify: Update PATH in shell config

- name: Ensure .local/bin is in PATH
  ansible.builtin.lineinfile:
    path: "{{ llmlab_base }}/.bashrc"
    line: 'export PATH="$HOME/.local/bin:$PATH"'
    state: present
    insertafter: EOF
  notify: Shell updated

- name: Create logs directory
  ansible.builtin.file:
    path: "{{ llmlab_base }}/.llmlab/logs"
    state: directory
    mode: '0755'

- name: Create setup log file
  ansible.builtin.file:
    path: "{{ llmlab_base }}/.llmlab/logs/setup.log"
    state: touch
    mode: '0644'
