#!/bin/bash

# Simple ProofMode Docker Test
# This script demonstrates the current state of the Docker image

set -e

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

print_header() {
    echo -e "\n${BLUE}=== $1 ===${NC}"
}

print_success() {
    echo -e "${GREEN}✓ $1${NC}"
}

print_error() {
    echo -e "${RED}✗ $1${NC}"
}

print_info() {
    echo -e "${YELLOW}ℹ $1${NC}"
}

# Docker image to test
DOCKER_IMAGE="${PROOFMODE_IMAGE:-registry.gitlab.com/guardianproject/proofmode/proofmode-rust:latest}"

print_header "ProofMode Docker Test"

# Test 1: Check if Docker is available
print_header "Testing Docker availability"
if ! command -v docker &> /dev/null; then
    print_error "Docker is not installed"
    exit 1
fi
print_success "Docker is available"

# Test 2: Pull the image
print_header "Pulling ProofMode Docker image"
if docker pull "$DOCKER_IMAGE"; then
    print_success "Successfully pulled $DOCKER_IMAGE"
else
    print_error "Failed to pull $DOCKER_IMAGE"
    exit 1
fi

# Test 3: Check the help command
print_header "Testing ProofMode help command"
if docker run --rm "$DOCKER_IMAGE" --help; then
    print_success "Help command works"
else
    print_error "Help command failed"
fi

# Test 4: Check available commands
print_header "Testing ProofMode generate help"
docker run --rm "$DOCKER_IMAGE" generate --help || true

print_header "Testing ProofMode check help"
docker run --rm "$DOCKER_IMAGE" check --help || true

# Test 5: Create test directories
print_header "Setting up test environment"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
INPUT_DIR="${SCRIPT_DIR}/input"
OUTPUT_DIR="${SCRIPT_DIR}/output"

mkdir -p "$INPUT_DIR" "$OUTPUT_DIR"

# Create a test image
TEST_IMAGE="$INPUT_DIR/test.jpg"
if [ ! -f "$TEST_IMAGE" ]; then
    # Create a minimal JPEG using base64
    echo "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAABAAEDAREAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAX/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwCwAAH/2Q==" | base64 -d > "$TEST_IMAGE"
    print_success "Created test image: $TEST_IMAGE"
fi

# Test 6: Try to generate a proof
print_header "Testing proof generation"
if docker run --rm \
    -v "$INPUT_DIR:/input:ro" \
    -v "$OUTPUT_DIR:/output" \
    "$DOCKER_IMAGE" \
    generate -f /input/test.jpg 2>&1; then
    
    print_success "Generate command completed"
    
    # Check if any files were created
    if ls "$OUTPUT_DIR"/* >/dev/null 2>&1; then
        print_success "Files created in output directory:"
        ls -la "$OUTPUT_DIR"/
    else
        print_info "No files created in output directory"
    fi
    
    # Check if files were created in the default location
    if docker run --rm \
        -v "$INPUT_DIR:/input:ro" \
        "$DOCKER_IMAGE" \
        sh -c "ls -la /app/ 2>/dev/null || ls -la / 2>/dev/null | head -20" 2>/dev/null; then
        print_info "Checked container filesystem"
    fi
    
else
    print_error "Generate command failed"
fi

# Test 7: Interactive exploration
print_header "Container exploration"
print_info "Exploring the container structure..."

docker run --rm "$DOCKER_IMAGE" \
    sh -c "echo 'ProofMode binary location:'; which proofmode 2>/dev/null || echo 'proofmode not in PATH'; \
    echo 'Current directory:'; pwd; \
    echo 'Directory contents:'; ls -la; \
    echo 'App directory:'; ls -la /app/ 2>/dev/null || echo 'No /app directory'; \
    echo 'Environment variables:'; env | grep -i proof || echo 'No ProofMode env vars'"

print_header "Summary"
print_info "The current Docker image appears to be a work-in-progress"
print_info "Some functionality may not be fully implemented yet"
print_success "Basic Docker setup is working"

echo -e "\n${BLUE}To build a local image with current code:${NC}"
echo "  docker build -t proofmode-rust:local ."
echo "  PROOFMODE_IMAGE=proofmode-rust:local $0"