- name: Preflight checks and deploy wedding-site
  hosts: servers
  become: yes
  vars:
    env_file: $(pwd)/.env

  tasks:
    - name: Install wget
      apt:
        name: wget
        state: present
        update_cache: yes

    - name: Install Docker
      shell: curl -fsSL https://get.docker.com | sh
      args:
        creates: /usr/bin/docker

    - name: Install Docker Compose plugin
      apt:
        name: docker-compose-plugin
        state: present

    - name: Ensure /opt/infra exists
      file:
        path: /opt/infra
        state: directory
        owner: root
        group: root
        mode: '0755'

    - name: Copy .env to remote
      copy:
        src: "{{ env_file }}"
        dest: /opt/infra/.env
        owner: root
        group: root
        mode: '0600'

    - name: Download node-docker.yaml
      shell: wget -O /opt/infra/node-docker.yaml "{{ gitea_instance_url }}"
      args:
        creates: /opt/infra/node-docker.yaml

    - name: Start node-docker
      shell: docker compose -f /opt/infra/node-docker.yaml up -d
    
    - name: Docker system prune
      shell: docker system prune -a -f