- name: Install and configure YOURLS
  block:
    - name: Install required packages
      ansible.builtin.apt:
        name:
          - php
          - php-cli
          - php-fpm
          - php-mysql
          - php-curl
          - php-mbstring
          - php-xml
          - php-gd
          - php-bcmath
          - php-json
          - mariadb-server
          - mariadb-client
          - python3-pymysql
          - git
        state: present
        update_cache: true

    - name: Gather package facts
      ansible.builtin.package_facts:
        manager: apt

    - name: Detect installed PHP-FPM version
      ansible.builtin.set_fact:
        yourls_php_fpm_version: "{{ item.key | regex_replace('^php(\\d+\\.\\d+)-fpm$', '\\1') }}"
      loop: "{{ ansible_facts.packages | dict2items }}"
      when: item.key is match('^php\\d+\\.\\d+-fpm$')

    - name: Start and enable MariaDB service
      ansible.builtin.systemd_service:
        name: mysql
        enabled: true
        state: started

    - name: Create YOURLS database # noqa: fqcn[canonical]
      community.mysql.mysql_db:
        name: "{{ yourls_db_name }}"
        state: present
        login_unix_socket: /var/run/mysqld/mysqld.sock

    - name: Create YOURLS database user # noqa: fqcn[canonical]
      community.mysql.mysql_user:
        name: "{{ yourls_db_user }}"
        password: "{{ yourls_db_password }}"
        priv: "{{ yourls_db_name }}.*:ALL"
        state: present
        login_unix_socket: /var/run/mysqld/mysqld.sock

    - name: Create YOURLS installation directory
      ansible.builtin.file:
        path: "{{ yourls_install_path }}"
        state: directory
        owner: "{{ yourls_sys_user }}"
        group: "{{ yourls_sys_group }}"
        mode: "0755"

    - name: Check if YOURLS is already cloned
      ansible.builtin.stat:
        path: "{{ yourls_install_path }}/.git"
      register: yourls_git_check

    - name: Clone YOURLS repository
      when: not yourls_git_check.stat.exists
      become: true
      become_user: "{{ yourls_sys_user }}"
      ansible.builtin.git:
        repo: https://github.com/YOURLS/YOURLS.git
        dest: "{{ yourls_install_path }}"
        version: "{{ yourls_version }}"
        depth: 1

    - name: Check for local modifications in YOURLS directory # noqa: command-instead-of-module
      when: yourls_git_check.stat.exists
      become: true
      become_user: "{{ yourls_sys_user }}"
      ansible.builtin.command: git status --porcelain
      args:
        chdir: "{{ yourls_install_path }}"
      register: yourls_git_status
      changed_when: false

    - name: Update YOURLS repository
      when:
        - yourls_git_check.stat.exists
        - yourls_git_status.stdout == ""
      become: true
      become_user: "{{ yourls_sys_user }}"
      ansible.builtin.git:
        repo: https://github.com/YOURLS/YOURLS.git
        dest: "{{ yourls_install_path }}"
        version: "{{ yourls_version }}"
        update: true
      register: yourls_update
      changed_when: yourls_update.before != yourls_update.after

    - name: Deploy YOURLS config.php
      ansible.builtin.template:
        src: templates/config.php.j2
        dest: "{{ yourls_install_path }}/user/config.php"
        owner: "{{ yourls_sys_user }}"
        group: "{{ yourls_sys_group }}"
        mode: "0640"
      notify: Restart php-fpm

    - name: Check if YOURLS tables exist # noqa: fqcn[canonical]
      community.mysql.mysql_query:
        login_unix_socket: /var/run/mysqld/mysqld.sock
        login_db: "{{ yourls_db_name }}"
        query: "SHOW TABLES LIKE '{{ yourls_db_prefix }}url'"
      register: yourls_table_check
      changed_when: false

    - name: Check if YOURLS options are initialized # noqa: fqcn[canonical]
      community.mysql.mysql_query:
        login_unix_socket: /var/run/mysqld/mysqld.sock
        login_db: "{{ yourls_db_name }}"
        query: "SELECT COUNT(*) as count FROM {{ yourls_db_prefix }}options WHERE option_name = 'version'"
      register: yourls_options_check
      changed_when: false
      when: yourls_table_check.rowcount[0] > 0

    - name: Initialize YOURLS database
      ansible.builtin.command:
        cmd: |
          php -r "
            define('YOURLS_INSTALLING', true);
            require '{{ yourls_install_path }}/includes/load.php';
            yourls_create_sql_tables();
            yourls_initialize_options();
          "
        chdir: "{{ yourls_install_path }}"
      when: yourls_table_check.rowcount[0] == 0 or (yourls_options_check.query_result is defined and yourls_options_check.query_result[0][0].count == 0)
      changed_when: true
      become: true
      become_user: "{{ yourls_sys_user }}"

    - name: Set ownership for YOURLS directory
      ansible.builtin.file:
        path: "{{ yourls_install_path }}"
        state: directory
        owner: "{{ yourls_sys_user }}"
        group: "{{ yourls_sys_group }}"
        mode: "0755"
        recurse: true

    - name: Deploy YOURLS Caddyfile
      ansible.builtin.template:
        src: templates/yourls.caddyfile.j2
        dest: "/etc/caddy/sites/{{ yourls_domain }}.caddyfile"
        owner: root
        group: root
        mode: "0644"
      notify: Restart caddy

    - name: Ensure Cloudflare DNS A record exists for {{ yourls_subdomain + '.' + domain }}
      ansible.builtin.include_role:
        name: dns_record
      vars:
        dns_record_subdomain: "{{ yourls_subdomain }}"
        dns_record_domain: "{{ domain }}"
        dns_record_cloudflare_api_token: "{{ cloudflare_dns_api_token }}"
