- hosts: webservers
  become: yes

  vars:
    project_path: /home/ubuntu/{{ project_name }}
    website_domain: {{ website_domain|default('example.com') }}
    certbot_email: {{ certbot_email|default('your-email@example.com') }}
    env_file_src: ../../backend/.env
    docker_compose_src: ../../docker-compose.prod.yml
    nginx_conf_src: ../../nginx/backend.conf

  tasks:
    - name: Update apt packages
      apt:
        update_cache: yes
        upgrade: dist

    - name: Install prerequisites for Docker
      apt:
        name:
          - ca-certificates
          - curl
          - gnupg
        state: present

    - name: Add Docker GPG key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Add Docker repository
      apt_repository:
        repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
        state: present

    - name: Install dependencies
      apt:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-buildx-plugin
          - docker-compose-plugin
          - nginx
          - certbot
          - python3-certbot-nginx
        state: present
        update_cache: yes

    - name: Add ubuntu user to docker group
      user:
        name: ubuntu
        groups: docker
        append: yes

    - name: Create project folder
      file:
        path: "{{ project_path }}"
        state: directory
        mode: "0755"
        owner: ubuntu
        group: ubuntu

    - name: Create backend folder
      file:
        path: "{{ project_path }}/backend"
        state: directory
        mode: "0755"
        owner: ubuntu
        group: ubuntu

    - name: Copy .env file
      copy:
        src: "{{ env_file_src }}"
        dest: "{{ project_path }}/backend/.env"
        mode: "0600"
        owner: ubuntu
        group: ubuntu

    - name: Copy docker-compose.prod.yml
      copy:
        src: "{{ docker_compose_src }}"
        dest: "{{ project_path }}/docker-compose.prod.yml"
        mode: "0644"
        owner: ubuntu
        group: ubuntu
      notify:
        - restart docker-compose

    - name: Set permissions on project folder recursively
      file:
        path: "{{ project_path }}"
        state: directory
        recurse: yes
        owner: ubuntu
        group: ubuntu

    - name: Copy Nginx config to EC2
      copy:
        src: "{{ nginx_conf_src }}"
        dest: /etc/nginx/sites-available/backend
        mode: "0644"
      notify:
        - reload nginx

    - name: Enable Nginx site
      file:
        src: /etc/nginx/sites-available/backend
        dest: /etc/nginx/sites-enabled/backend
        state: link
      notify:
        - reload nginx

    - name: Remove default Nginx site
      file:
        path: /etc/nginx/sites-enabled/default
        state: absent
      notify:
        - reload nginx

    - name: Obtain SSL certificate via Certbot
      command: >
        certbot --nginx
        -d {{ website_domain }}
        --non-interactive
        --agree-tos
        -m {{ certbot_email }}
      args:
        creates: "/etc/letsencrypt/live/{{ website_domain }}/fullchain.pem"
      ignore_errors: yes

    - name: Ensure Docker Compose is running
      command: docker compose -f docker-compose.prod.yml up -d
      become: yes
      become_user: ubuntu
      args:
        chdir: "{{ project_path }}"

  handlers:
    - name: reload nginx
      service:
        name: nginx
        state: reloaded

    - name: restart docker-compose
      command: docker compose -f docker-compose.prod.yml up -d
      become: yes
      become_user: ubuntu
      args:
        chdir: "{{ project_path }}"
