#!/bin/bash

# Test the deployment script locally without Docker
# This script validates the deployment script functionality

set -e

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

echo -e "${BLUE}=== ORLY Deployment Script Local Test ===${NC}"
echo ""

# Check if we're in the right directory
if [[ ! -f "go.mod" ]] || ! grep -q "next.orly.dev" go.mod; then
    echo -e "${RED}ERROR: This script must be run from the next.orly.dev project root${NC}"
    exit 1
fi

echo -e "${YELLOW}1. Testing help functionality...${NC}"
if ./scripts/deploy.sh --help >/dev/null 2>&1; then
    echo -e "${GREEN}✓ Help functionality works${NC}"
else
    echo -e "${RED}✗ Help functionality failed${NC}"
    exit 1
fi

echo -e "${YELLOW}2. Testing script validation...${NC}"
required_files=(
    "go.mod"
    "scripts/update-embedded-web.sh"
    "app/web/package.json"
)

for file in "${required_files[@]}"; do
    if [[ -f "$file" ]]; then
        echo -e "${GREEN}✓ Required file exists: $file${NC}"
    else
        echo -e "${RED}✗ Missing required file: $file${NC}"
        exit 1
    fi
done

echo -e "${YELLOW}3. Testing script permissions...${NC}"
required_scripts=(
    "scripts/deploy.sh"
    "scripts/update-embedded-web.sh"
)

for script in "${required_scripts[@]}"; do
    if [[ -x "$script" ]]; then
        echo -e "${GREEN}✓ Script is executable: $script${NC}"
    else
        echo -e "${RED}✗ Script is not executable: $script${NC}"
        exit 1
    fi
done

echo -e "${YELLOW}4. Testing Go download URL validation...${NC}"
GO_VERSION="1.23.1"
arch=$(uname -m)
case $arch in
    x86_64) arch="amd64" ;;
    aarch64|arm64) arch="arm64" ;;
    armv7l) arch="armv6l" ;;
    *) echo -e "${RED}Unsupported architecture: $arch${NC}"; exit 1 ;;
esac

go_archive="go${GO_VERSION}.linux-${arch}.tar.gz"
download_url="https://golang.org/dl/${go_archive}"

echo "  Checking URL: $download_url"
if curl --output /dev/null --silent --head --fail "$download_url" 2>/dev/null; then
    echo -e "${GREEN}✓ Go download URL is accessible${NC}"
else
    echo -e "${YELLOW}⚠ Go download URL check skipped (no internet or curl not available)${NC}"
fi

echo -e "${YELLOW}5. Testing environment file generation...${NC}"
temp_dir=$(mktemp -d)
GOROOT="$temp_dir/.local/go"
GOPATH="$temp_dir"
GOBIN="$temp_dir/.local/bin"
GOENV_FILE="$temp_dir/.goenv"

mkdir -p "$temp_dir/.local/bin"

cat > "$GOENV_FILE" << EOF
# Go environment configuration
export GOROOT="$GOROOT"
export GOPATH="$GOPATH"
export GOBIN="$GOBIN"
export PATH="\$GOBIN:\$GOROOT/bin:\$PATH"
EOF

if [[ -f "$GOENV_FILE" ]]; then
    echo -e "${GREEN}✓ .goenv file created successfully${NC}"
else
    echo -e "${RED}✗ Failed to create .goenv file${NC}"
    exit 1
fi

echo -e "${YELLOW}6. Testing systemd service file generation...${NC}"
SERVICE_NAME="orly"
BINARY_NAME="orly"
working_dir=$(pwd)
USER=$(whoami)

service_content="[Unit]
Description=ORLY Nostr Relay
After=network.target
Wants=network.target

[Service]
Type=simple
User=$USER
Group=$USER
WorkingDirectory=$working_dir
ExecStart=$GOBIN/$BINARY_NAME
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=$SERVICE_NAME

# Security settings
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=$working_dir $HOME/.local/share/ORLY $HOME/.cache/ORLY
PrivateTmp=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true

# Network settings
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target"

service_file="$temp_dir/test-orly.service"
echo "$service_content" > "$service_file"

if [[ -f "$service_file" ]]; then
    echo -e "${GREEN}✓ Systemd service file generated successfully${NC}"
else
    echo -e "${RED}✗ Failed to generate systemd service file${NC}"
    exit 1
fi

echo -e "${YELLOW}7. Testing Go module validation...${NC}"
if grep -q "module next.orly.dev" go.mod; then
    echo -e "${GREEN}✓ Go module is correctly configured${NC}"
else
    echo -e "${RED}✗ Go module configuration is incorrect${NC}"
    exit 1
fi

echo -e "${YELLOW}8. Testing build capability...${NC}"
if CGO_ENABLED=0 go build -o "$temp_dir/test-orly" . >/dev/null 2>&1; then
    echo -e "${GREEN}✓ Project builds successfully (pure Go + purego)${NC}"
    # Copy libsecp256k1.so if available (runtime optional)
    if [[ -f "pkg/crypto/p8k/libsecp256k1.so" ]]; then
        cp pkg/crypto/p8k/libsecp256k1.so "$temp_dir/"
        echo -e "${GREEN}✓ libsecp256k1.so copied (runtime optional)${NC}"
    fi
    if [[ -x "$temp_dir/test-orly" ]]; then
        echo -e "${GREEN}✓ Binary is executable${NC}"
    else
        echo -e "${RED}✗ Binary is not executable${NC}"
        exit 1
    fi
else
    echo -e "${YELLOW}⚠ Build test skipped (Go not available or build dependencies missing)${NC}"
fi

# Clean up temp directory
rm -rf "$temp_dir"

echo ""
echo -e "${GREEN}=== All deployment script tests passed! ===${NC}"
echo ""
echo -e "${BLUE}The deployment script is ready for use.${NC}"
echo ""
echo "To deploy ORLY on a server:"
echo "  1. Clone the repository"
echo "  2. Run: ./scripts/deploy.sh"
echo "  3. Configure environment variables"
echo "  4. Start the service: sudo systemctl start orly"
echo ""
echo "For Docker testing (if Docker is available):"
echo "  Run: ./scripts/test-deploy-docker.sh"
echo ""

# Create a summary report
echo "=== DEPLOYMENT TEST SUMMARY ===" > deployment-test-report.txt
echo "Date: $(date)" >> deployment-test-report.txt
echo "Architecture: $(uname -m)" >> deployment-test-report.txt
echo "OS: $(uname -s) $(uname -r)" >> deployment-test-report.txt
echo "User: $(whoami)" >> deployment-test-report.txt
echo "Working Directory: $(pwd)" >> deployment-test-report.txt
echo "Go Module: $(head -1 go.mod)" >> deployment-test-report.txt
echo "" >> deployment-test-report.txt
echo "✅ Deployment script validation: PASSED" >> deployment-test-report.txt
echo "✅ Required files check: PASSED" >> deployment-test-report.txt
echo "✅ Script permissions check: PASSED" >> deployment-test-report.txt
echo "✅ Environment setup simulation: PASSED" >> deployment-test-report.txt
echo "✅ Systemd service generation: PASSED" >> deployment-test-report.txt
echo "✅ Go module validation: PASSED" >> deployment-test-report.txt
echo "" >> deployment-test-report.txt
echo "The deployment script is ready for production use." >> deployment-test-report.txt

echo -e "${GREEN}Test report saved to: deployment-test-report.txt${NC}"
