#!/bin/bash
# Setup symlinks for test directories
# Enables sharing test directories across different services on the same filesystem
#
# This script should be run on the AWS server after deployment
# It creates symlinks to allow ward/tester to access test data from amar_django_back_contracts
#
# Usage:
#   ./setup-symlinks.sh [--dry-run]

set -e

DRY_RUN=""
if [ "$1" == "--dry-run" ]; then
    DRY_RUN="echo [DRY-RUN]"
fi

echo "=== Setting up Test Directory Symlinks ==="
echo ""

# Check if we're on the server
if [ ! -d "$HOME/core_room" ]; then
    echo "Error: ~/core_room directory not found"
    echo "This script should run on the AWS server after deployment"
    exit 1
fi

cd "$HOME/core_room"

# =============================================================================
# Test Directory Symlinks
# =============================================================================
echo "Step 1: Creating symlinks for test directories..."
echo ""

# Ward tester tests directory
WARD_TESTS_DIR="soleprint/src/ward/tools/tester/tests"
CONTRACTS_SOURCE="amar/src/back/tests/contracts"

# Create ward tests directory if it doesn't exist
if [ ! -d "$WARD_TESTS_DIR" ]; then
    $DRY_RUN mkdir -p "$WARD_TESTS_DIR"
    echo "  Created $WARD_TESTS_DIR"
fi

# Check if source contracts directory exists
if [ ! -d "$CONTRACTS_SOURCE" ]; then
    echo "  ⚠ Warning: Source contracts directory not found: $CONTRACTS_SOURCE"
    echo "  Skipping test symlinks"
else
    # Create symlinks for each test domain
    for domain_dir in "$CONTRACTS_SOURCE"/*; do
        if [ -d "$domain_dir" ]; then
            domain_name=$(basename "$domain_dir")

            # Skip __pycache__ and other Python artifacts
            if [[ "$domain_name" == "__pycache__" ]] || [[ "$domain_name" == "*.pyc" ]]; then
                continue
            fi

            target_link="$WARD_TESTS_DIR/$domain_name"

            # Remove existing symlink or directory
            if [ -L "$target_link" ]; then
                $DRY_RUN rm "$target_link"
                echo "  Removed existing symlink: $target_link"
            elif [ -d "$target_link" ]; then
                echo "  ⚠ Warning: $target_link exists as directory, not symlink"
                echo "    To replace with symlink, manually remove: rm -rf $target_link"
                continue
            fi

            # Create relative symlink
            # From: soleprint/src/ward/tools/tester/tests/
            # To:   amar/src/back/tests/contracts/
            # Relative path: ../../../../../amar/src/back/tests/contracts/
            $DRY_RUN ln -s "../../../../../$CONTRACTS_SOURCE/$domain_name" "$target_link"
            echo "  ✓ Created symlink: $target_link -> $domain_name"
        fi
    done

    # Also symlink shared test utilities
    for shared_file in "endpoints.py" "helpers.py" "base.py" "conftest.py"; do
        source_file="$CONTRACTS_SOURCE/$shared_file"
        target_file="$WARD_TESTS_DIR/$shared_file"

        if [ -f "$source_file" ]; then
            if [ -L "$target_file" ]; then
                $DRY_RUN rm "$target_file"
            fi

            if [ ! -e "$target_file" ]; then
                $DRY_RUN ln -s "../../../../../$source_file" "$target_file"
                echo "  ✓ Created symlink: $target_file"
            fi
        fi
    done
fi

echo ""

# =============================================================================
# Bare Metal Symlinks (if bare metal path exists)
# =============================================================================
if [ -d "$HOME/soleprint" ]; then
    echo "Step 2: Creating bare metal symlinks..."
    echo ""

    BARE_WARD_TESTS="$HOME/soleprint/ward/tools/tester/tests"

    if [ ! -d "$BARE_WARD_TESTS" ]; then
        $DRY_RUN mkdir -p "$BARE_WARD_TESTS"
        echo "  Created $BARE_WARD_TESTS"
    fi

    # For bare metal, we can symlink to the docker contract source if it's synced
    # Or we can sync tests separately (handled by sync-tests.sh)
    echo "  ℹ Bare metal tests managed by sync-tests.sh"
    echo "  Run: $HOME/core_room/ctrl/sync-tests.sh"
else
    echo "Step 2: Bare metal path not found, skipping"
fi

echo ""

# =============================================================================
# Verification
# =============================================================================
echo "=== Verification ==="
echo ""

if [ -d "$WARD_TESTS_DIR" ]; then
    echo "Ward tester tests:"
    ls -lah "$WARD_TESTS_DIR" | grep -E "^l|^d" || echo "  No directories or symlinks found"
else
    echo "  ⚠ Ward tests directory not found"
fi

echo ""

# =============================================================================
# Done
# =============================================================================
if [ -n "$DRY_RUN" ]; then
    echo "=== Dry run complete (no changes made) ==="
else
    echo "=== Symlink Setup Complete ==="
fi

echo ""
echo "Next steps:"
echo "  1. Verify symlinks are working:"
echo "       ls -lah $WARD_TESTS_DIR"
echo ""
echo "  2. Restart ward container to pick up changes:"
echo "       cd ~/core_room/ctrl && docker compose restart ward"
echo ""
echo "  3. Test in browser:"
echo "       https://ward.mcrn.ar/tools/tester/"
echo ""
