---
- name: Follow Tutorial
  hosts: debian
  become: true

  vars_files:
    - vars.yaml

  pre_tasks:
    - name: Update apt cache if needed
      ansible.builtin.apt:
        update_cache: true
        cache_valid_time: 3600

  handlers:
    - name: Restart apache
      ansible.builtin.service:
        name: apache2
        state: restarted

  tasks:
    - name: Get software for apt repository management
      ansible.builtin.apt:
        state: present
        name:
          - python3-apt
          - python3-pycurl

    - name: "Install Apache, MySQL, PHP, and other dependencies"
      ansible.builtin.apt:
        state: present
        name:
          - acl
          - git
          - curl
          - unzip
          - sendmail
          - apache2
          - php8.2-common
          - php8.2-cli
          - php8.2-dev
          - php8.2-gd
          - php8.2-curl
          - php8.2-opcache
          - php8.2-xml
          - php8.2-mbstring
          - php8.2-pdo
          - php8.2-mysql
          - php8.2-apcu
          - libpcre3-dev
          - libapache2-mod-php8.2
          - python3-mysqldb
          - default-mysql-server
          - composer

    - name: Install the firewall
      ansible.builtin.apt:
        name: ufw
        state: present

    - name: Disable the firewall (since this is for local dev only).
      ansible.builtin.service:
        name: ufw
        state: stopped

    - name: "Start Apache, MySQL, and PHP."
      ansible.builtin.service:
        name: "{{ item }}"
        state: started
        enabled: true
      loop:
        - apache2
        - mysql

    - name: Enable Apache rewrite module (required for Drupal).
      community.general.apache2_module:
        name: rewrite
        state: present
      notify: Restart apache

    - name: Add Apache virtualhost for Drupal.
      ansible.builtin.template:
        src: "templates/drupal.test.conf.j2"
        dest: "/etc/apache2/sites-available/{{ domain }}.test.conf"
        owner: root
        group: root
        mode: "0664"
      notify: Restart apache

    - name: Enable Drupal site.
      ansible.builtin.command:
        cmd: a2ensite {{ domain }}.test
        creates: /etc/apache2/sites-enabled/{{ domain }}.test.conf
      notify: Restart apache

    - name: Disable the default site.
      ansible.builtin.command:
        cmd: a2dissite 000-default
        removes: /etc/apache2/sites-enabled/000-default.conf
      notify: Restart apache

    - name: Adjust OpCache memory setting.
      ansible.builtin.lineinfile:
        dest: "/etc/php/8.2/apache2/conf.d/10-opcache.ini"
        regexp: "^opcache/memory_consumption"
        line: "opcache.memory_consumption = 96"
        state: present
      notify: Restart apache

    - name: Create a MySQL database for Drupal.
      community.mysql.mysql_db:
        db: "{{ domain }}"
        state: present

    - name: Create a MySQL user for Drupal.
      community.mysql.mysql_user:
        name: "{{ domain }}"
        password: "1234"
        priv: "{{ domain }}.*:ALL"
        host: localhost
        state: present

    - name: Ensure Drupal directory exists.
      ansible.builtin.file:
        path: "{{ drupal_core_path }}"
        state: directory
        owner: www-data
        group: www-data
        mode: "755"
    - name: Check if Drupal project already exists.
      ansible.builtin.stat:
        path: "{{ drupal_core_path }}/composer.json"
      register: drupal_composer_json

    - name: Create Drupal project.
      community.general.composer:
        command: create-project
        arguments: drupal/recommended-project:^10 "{{ drupal_core_path }}"
        working_dir: "{{ drupal_core_path }}"
        no_dev: true
      become_user: www-data
      become: true
      when: not drupal_composer_json.stat.exists

    - name: Ensure cache dir is writable by www-data.
      ansible.builtin.file:
        dest: "/var/www/.cache"
        state: directory
        group: www-data
        owner: www-data
        mode: "0755"

    - name: Add drush to the Drupal site with composer.
      community.general.composer:
        command: require
        arguments: "drush/drush"
        working_dir: "{{ drupal_core_path }}"
      become_user: www-data
      become: true
      when: not drupal_composer_json.stat.exists

    - name: Install Drupal
      ansible.builtin.command:
        argv:
          - vendor/bin/drush si -y --site-name="{{ drupal_site_name }}"
          - --account-name=admin
          - --account-pass=admin
          - --db-url=mysql://{{ domain }}:1234@localhost/{{ domain }}
          - --root={{ drupal_core_path }}/web
        chdir: "{{ drupal_core_path }}"
        creates: "{{ drupal_core_path }}/web/sites/default/settings.php"
      notify: Restart apache
      become_user: www-data
      become: true