#!/bin/bash

# Legacy build notification script - now delegates to unified notification system
# This script is kept for backward compatibility but uses the new unified system

set -e

# Get the directory of this script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Find the main plugin file using PLUGIN_NAME environment variable
PLUGIN_FILE=""
if [ -n "$PLUGIN_NAME" ]; then
    # Use PLUGIN_NAME to construct the expected file path
    EXPECTED_FILE="${PLUGIN_NAME}.php"

    # Try current directory first
    if [ -f "$EXPECTED_FILE" ]; then
        PLUGIN_FILE="$EXPECTED_FILE"
    fi

    # If not found, try parent directory
    if [ -z "$PLUGIN_FILE" ] && [ -f "../$EXPECTED_FILE" ]; then
        PLUGIN_FILE="../$EXPECTED_FILE"
    fi

    # If still not found, try grandparent directory
    if [ -z "$PLUGIN_FILE" ] && [ -f "../../$EXPECTED_FILE" ]; then
        PLUGIN_FILE="../../$EXPECTED_FILE"
    fi

    # Final fallback: search recursively up to 2 levels deep
    if [ -z "$PLUGIN_FILE" ]; then
        PLUGIN_FILE=$(find . -maxdepth 2 -name "$EXPECTED_FILE" | head -1)
    fi
else
    # Fallback to generic search if PLUGIN_NAME is not set
    PLUGIN_FILE=$(find . -maxdepth 2 -name "woocommerce-cloudxm-*.php" | head -1)
fi

# Use the unified notification system
case "${1:-success}" in
    "success")
        "$SCRIPT_DIR/unified-notification.sh" build success "$PLUGIN_FILE"
        ;;
    "failure")
        "$SCRIPT_DIR/unified-notification.sh" build failure "$PLUGIN_FILE" "$2"
        ;;
    *)
        echo "Usage: $0 [success|failure] [error_message]"
        echo ""
        echo "This script now uses the unified notification system."
        echo "For more advanced features, use: unified-notification.sh"
        exit 1
        ;;
esac