name: 'Setup Environment'
description: 'Sets up the environment for Frappe development'
inputs:
  python-version:
    description: 'Python version to use'
    required: false
    default: '3.14'
  node-version:
    description: 'Node.js version to use'
    required: false
    default: '24'
  build-assets:
    required: false
    description: 'Whether to build assets'
    default: true
  enable-coverage:
    required: false
    default: false
  disable-web:
    required: false
    default: false
  disable-socketio:
    required: false
    default: false
  db:
    required: false
    default: mariadb
  db-root-password:
    required: true

runs:
  using: "composite"
  steps:
  - shell: bash -e {0}
    run: |
      # Add 'test_site' to /etc/hosts & setup git config
      echo "127.0.0.1 test_site" | sudo tee -a /etc/hosts
      git config --global init.defaultBranch main
      git config --global advice.detachedHead false

  - name: Clone
    uses: actions/checkout@v6
    with:
      path: frappe-src

  - name: Setup Python
    uses: actions/setup-python@v6
    with:
      python-version: ${{ inputs.python-version }}

  - shell: bash -e {0}
    run: |
      # Check for valid Python & Merge Conflicts
      python -m compileall -q -f "${GITHUB_WORKSPACE}/frappe-src"
      if grep -lr --exclude-dir=node_modules "^<<<<<<< " "${GITHUB_WORKSPACE}/frappe-src"
          then echo "Found merge conflicts"
          exit 1
      fi

  - uses: actions/setup-node@v6
    with:
      node-version: ${{ inputs.node-version }}
      check-latest: true

  - uses: astral-sh/setup-uv@v6

  - name: Cache pip
    uses: actions/cache@v4
    with:
      path: ~/.cache/pip
      key: ${{ runner.os }}-pip-${{ hashFiles('**/*requirements.txt', '**/pyproject.toml', '**/setup.py') }}
      restore-keys: |
        ${{ runner.os }}-pip-
        ${{ runner.os }}-

  - id: yarn-cache-dir-path
    shell: bash -e {0}
    run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
  - uses: actions/cache@v4
    id: yarn-cache
    with:
      path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
      key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
      restore-keys: |
        ${{ runner.os }}-yarn-

  - shell: bash -e {0}
    run: |
      # Install System Dependencies
      start_time=$(date +%s)
      curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash -s -- --mariadb-server-version="mariadb-11.8.5" --skip-maxscale

      sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
      wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

      sudo apt -qq update
      sudo apt -qq remove mysql-server mysql-client
      sudo apt -qq remove -y postgresql-client postgresql-client-16 postgresql-client-common
      sudo apt -qq install libcups2-dev redis-server mariadb-client libmariadb-dev postgresql-client-18 libpq-dev
      echo "/usr/lib/postgresql/18/bin" >> $GITHUB_PATH

      wget -q -O /tmp/wkhtmltox.deb https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_amd64.deb
      sudo apt install /tmp/wkhtmltox.deb

      end_time=$(date +%s)
      echo -e "\033[33mInstall System Dependencies: $((end_time - start_time)) seconds\033[0m"

  - shell: bash -e {0}
    run: |
      # Init Bench
      start_time=$(date +%s)
      uv tool install frappe-bench
      bench init ${GITHUB_WORKSPACE} \
        --ignore-exist \
        --frappe-path "${GITHUB_WORKSPACE}/frappe-src" \
        --skip-assets \
        --no-backups \
        --python "$(which python)"

      # bench init sets origin to the local checkout path, fix it to point at GitHub
      git -C apps/frappe remote set-url upstream "https://github.com/${{ github.repository }}"

      # Trim Procfile for CI
      sed -i '/^watch:/d' Procfile
      sed -i '/^schedule:/d' Procfile
      if [ "${{ inputs.disable-web }}" == "true" ]; then
        sed -i '/^web:/d' Procfile
      elif [ "${{ inputs.enable-coverage }}" == "true" ]; then
        sed -i 's|^web: bench serve|web: bench serve --with-coverage|' Procfile
      fi
      if [ "${{ inputs.disable-socketio }}" == "true" ]; then
        sed -i '/^socketio:/d' Procfile
      fi

      end_time=$(date +%s)
      echo -e "\033[33mInit Bench: $((end_time - start_time)) seconds\033[0m"

  - shell: bash -e {0}
    run: |
      # Install dev/test dependencies
      start_time=$(date +%s)
      bench setup requirements --dev
      if [ "${{ inputs.build-assets }}" == "true" ]; then
        bench setup requirements --node
      fi

      end_time=$(date +%s)
      echo -e "\033[33mInstall dev/test deps: $((end_time - start_time)) seconds\033[0m"

  - shell: bash -e {0}
    env:
      DB: ${{ inputs.db }}
    run: |
      # Create Site
      start_time=$(date +%s)
      if [ "$DB" == "postgres" ]; then
        DB_ROOT_USER="postgres"
        DB_ROOT_PWD="travis"
      else
        DB_ROOT_USER="root"
        DB_ROOT_PWD="${{ inputs.db-root-password }}"
      fi

      bench set-config -g root_login "$DB_ROOT_USER"
      bench set-config -g root_password "$DB_ROOT_PWD"
      bench set-config -g admin_password admin

      bench new-site test_site \
        --db-type "$DB" \
        --db-host 127.0.0.1 \
        --db-name test_frappe \
        --db-password test_frappe \
        --verbose
      bench --site test_site set-config allow_tests 1 --parse
      bench --site test_site set-config server_script_enabled 1 --parse
      bench --site test_site set-config host_name "http://test_site:8000"
      bench --site test_site set-config auto_email_id "test@example.com"
      bench --site test_site set-config mail_server localhost
      bench --site test_site set-config mail_port 2525 --parse
      bench --site test_site set-config mail_login "test@example.com"
      bench --site test_site set-config mail_password test
      bench --site test_site set-config disable_mail_smtp_authentication 1 --parse

      if [ "$DB" == "mariadb" ]; then
        bench --site test_site set-config monitor 1 --parse
        bench --site test_site set-config use_mysqlclient 1 --parse
      fi

      end_time=$(date +%s)
      echo -e "\033[33mCreate Site: $((end_time - start_time)) seconds\033[0m"

  - shell: bash -e {0}
    run: |
      # Start Bench
      bench start &> ${GITHUB_WORKSPACE}/bench_start.log &

  - shell: bash -e {0}
    if: ${{ inputs.build-assets == 'true' }}
    run: |
      # Build Assets
      start_time=$(date +%s)
      CI=Yes bench build --force --production

      end_time=$(date +%s)
      echo -e "\033[33mBuild Assets: $((end_time - start_time)) seconds\033[0m"
