version: '3'

tasks:
  dev:setup:
    desc: Set up local development environment
    cmds:
      - task create-local-config
      - task inject-wp-config
      - echo "Local development environment configured"
      - echo "API URL pre-configured to http://maplepress-backend-dev:8000"
      - echo "Go to Settings > MaplePress to enter your API key"

  create-local-config:
    desc: Create local development config file
    cmds:
      - |
        cat > wp-config.local.php <<'EOF'
        <?php
        /**
         * MaplePress Local Development Configuration
         *
         * This file is for LOCAL DEVELOPMENT ONLY.
         * DO NOT commit this file to git.
         * DO NOT include in production builds.
         */

        // MaplePress Local Development Settings
        // Uses Docker container hostname for inter-container communication
        define('MAPLEPRESS_API_URL', 'http://maplepress-backend-dev:8000');
        EOF
      - echo "Created wp-config.local.php"

  inject-wp-config:
    desc: Add local config loader to WordPress wp-config.php
    cmds:
      - |
        docker exec maple-wordpress-dev bash -c '
        CONFIG_FILE="/var/www/html/wp-config.php"
        LOADER_LINE="// MaplePress local development config\nif (file_exists(__DIR__ . '\''/wp-content/plugins/maplepress-plugin/wp-config.local.php'\'')) { require_once __DIR__ . '\''/wp-content/plugins/maplepress-plugin/wp-config.local.php'\''; }"

        # Check if already added
        if grep -q "maplepress-plugin/wp-config.local.php" "$CONFIG_FILE"; then
          echo "wp-config.php already configured"
        else
          # Insert before "That'"'"'s all, stop editing!"
          sed -i "/That.*s all, stop editing/i $LOADER_LINE" "$CONFIG_FILE"
          echo "Added loader to wp-config.php"
        fi
        '

  dev:reset:
    desc: Reset to production configuration (remove local dev settings)
    cmds:
      - rm -f wp-config.local.php
      - |
        docker exec maple-wordpress-dev bash -c '
        CONFIG_FILE="/var/www/html/wp-config.php"
        # Remove MaplePress local config lines
        sed -i "/MaplePress local development config/d" "$CONFIG_FILE"
        sed -i "/maplepress-plugin\/wp-config.local.php/d" "$CONFIG_FILE"
        '
      - echo "Reset to production configuration"
      - echo "Plugin will now use empty API URL (production behavior)"

  sync:
    desc: Sync plugin to local WordPress container
    cmds:
      - docker cp . maple-wordpress-dev:/var/www/html/wp-content/plugins/maplepress-plugin/
      - echo "Plugin synced to WordPress container"

  watch:
    desc: Watch for changes and auto-sync to WordPress
    cmds:
      - |
        echo "Watching for changes... (Press Ctrl+C to stop)"
        fswatch -o . | while read; do
          task sync
        done

  logs:
    desc: View WordPress debug logs
    cmds:
      - docker exec -it maple-wordpress-dev tail -f /var/www/html/wp-content/debug.log

  shell:
    desc: Open shell in WordPress container
    cmds:
      - docker exec -it maple-wordpress-dev /bin/bash

  lint:
    desc: Run PHP CodeSniffer (requires phpcs)
    cmds:
      - phpcs --standard=WordPress --extensions=php .

  lint:fix:
    desc: Auto-fix PHP CodeSniffer issues (requires phpcbf)
    cmds:
      - phpcbf --standard=WordPress --extensions=php .

  test:
    desc: Run PHPUnit tests (requires phpunit)
    cmds:
      - phpunit

  build:
    desc: Build plugin zip for distribution
    cmds:
      - rm -rf dist
      - mkdir -p dist
      - |
        rsync -av \
          --exclude='dist' \
          --exclude='.git' \
          --exclude='node_modules' \
          --exclude='Taskfile.yml' \
          --exclude='.phpcs.xml.dist' \
          --exclude='wp-config.local.php' \
          --exclude='composer.json' \
          --exclude='composer.lock' \
          --exclude='vendor' \
          --exclude='.gitignore' \
          . dist/maplepress-plugin/
      - cd dist && zip -r maplepress-plugin.zip maplepress-plugin
      - echo "Plugin built at dist/maplepress-plugin.zip"
      - echo "Local dev files excluded from build"

  clean:
    desc: Clean build artifacts
    cmds:
      - rm -rf dist
      - rm -rf vendor
      - rm -rf node_modules
      - echo "Cleaned build artifacts"

  install:
    desc: Install Composer dependencies
    cmds:
      - composer install
      - echo "Composer dependencies installed"

  update:
    desc: Update Composer dependencies
    cmds:
      - composer update
      - echo "Composer dependencies updated"
