- name: Setup Arch Linux
  hosts: localhost
  become: yes

  tasks:
    - name: Check internet connectivity
      shell: ping -c 1 archlinux.org
      register: internet_check
      ignore_errors: yes

    - name: Prompt user if no internet connection
      pause:
        prompt: "No internet detected. Please connect to a network and press Enter to continue."
      when: internet_check.rc != 0

    - name: Set the timezone to America/Montreal
      command: timedatectl set-timezone America/Montreal
      changed_when: false  # Prevents Ansible from reporting this task as "changed" unnecessarily

    - name: Enable NTP synchronization
      command: timedatectl set-ntp true
      changed_when: false

    - name: Ensure systemd-timesyncd is running and enabled
      systemd:
        name: systemd-timesyncd
        state: started
        enabled: yes

    - name: Sync hardware clock with system time
      command: hwclock --systohc
      changed_when: false

    - name: Install base packages
      pacman:
        name:
          - base
          - base-devel
          - linux
          - linux-firmware
          - sof-firmware
          - efibootmgr
          - grub
        state: present

    - name: Enable essential services
      systemd:
        name: "{{ item }}"
        enabled: yes
      loop:
        - NetworkManager
        - bluetooth.service

    - name: Install yay (AUR helper)
      shell: |
        sudo pacman -S --needed git base-devel && \
        git clone https://aur.archlinux.org/yay-bin.git && \
        cd yay-bin && makepkg -si --noconfirm
      args:
        creates: /usr/bin/yay

    - name: Install core utilities
      pacman:
        name:
          - networkmanager
          - openssh
          - git
          - wget
          - neofetch
          - ripgrep
          - eza
          - fd
          - stow
          - zoxide
          - bat
        state: present

    - name: Install desktop environment packages
      pacman:
        name:
          - hyprland
          - hyprlock
          - hypridle
          - waybar
          - swaync
          - wofi
          - firefox
          - hyprshot
          - power-profiles-daemon
          - swww
          - nvidia-lts
          - brightnessctl
          - pavucontrol
          - pipewire
          - pipewire-alsa
          - pipewire-pulse
          - bluez
          - bluez-utils
          - udiskie
          - obsidian
        state: present

    - name: Install TUI utilities
      pacman:
        name:
          - bluetuith
          - lazydocker
          - lazygit
          - lazysql
          - fzf
          - sshs
          - yazi
          - taskwarrior-tui
        state: present

    - name: Install terminal applications
      pacman:
        name:
          - kitty
          - zsh
          - starship
          - tmux
          - tmux-plugin-manager
          - neovim
        state: present

    - name: Set default shell to zsh
      command: chsh -s /usr/bin/zsh

    - name: Install fonts
      pacman:
        name:
          - ttf-cascadia-code-nerd
          - ttf-font-awesome
          - ttf-jetbrains-mono-nerd
          - ttf-meslo-nerd
        state: present

    - name: Install AI stack
      pacman:
        name:
          - ollama
          - aichat
          - pandoc-cli
        state: present

    - name: Install TTS stack
      pacman:
        name:
          - piper-tts
          - paplay
        state: present

    - name: Install theming tools
      pacman:
        name:
          - pywal16
          - pywalfox
          - nwg-look
        state: present

    - name: Clone dotfiles repository
      git:
        repo: "https://github.com/yourusername/dotfiles.git"
        dest: "/home/{{ ansible_user }}/dotfiles"
        update: yes

    - name: Stow dotfiles
      shell: |
        cd /home/{{ ansible_user }}/dotfiles && stow */
      args:
        executable: /bin/bash

    - name: Apply pywal theme
      command: wal -i ~/.config/wallpapers/green_yellow_forest.jpg -o ~/.config/waybar/launch.sh
      args:
        executable: /bin/zsh
