name: 'Setup enviPy Environment'
description: 'Shared setup for enviPy CI - installs dependencies and prepares environment'

inputs:
  skip-frontend:
    description: 'Skip frontend build steps (pnpm, tailwind)'
    required: false
    default: 'false'
  skip-playwright:
    description: 'Skip Playwright installation'
    required: false
    default: 'false'
  ssh-private-key:
    description: 'SSH private key for git access'
    required: true
  run-migrations:
    description: 'Run Django migrations after setup'
    required: false
    default: 'true'

runs:
  using: "composite"
  steps:
    - name: Setup ssh
      shell: bash
      run: |
        mkdir -p ~/.ssh
        echo "${{ inputs.ssh-private-key }}" > ~/.ssh/id_ed25519
        chmod 600 ~/.ssh/id_ed25519
        ssh-keyscan git.envipath.com >> ~/.ssh/known_hosts
        eval $(ssh-agent -s)
        ssh-add ~/.ssh/id_ed25519

    - name: Setup Python venv
      shell: bash
      run: |
        uv sync --locked --all-extras --dev

    - name: Install Playwright
      if: inputs.skip-playwright == 'false'
      shell: bash
      run: |
        source .venv/bin/activate
        playwright install --with-deps

    - name: Build Frontend
      if: inputs.skip-frontend == 'false'
      shell: bash
      run: |
        uv run python scripts/pnpm_wrapper.py install
        uv run python scripts/pnpm_wrapper.py run build

    - name: Wait for Postgres
      shell: bash
      run: |
        until pg_isready -h postgres -U ${{ env.POSTGRES_USER }}; do
          echo "Waiting for postgres..."
          sleep 2
        done
        echo "Postgres is ready!"

    - name: Run Django Migrations
      if: inputs.run-migrations == 'true'
      shell: bash
      run: |
        source .venv/bin/activate
        python manage.py migrate --noinput
