#!/bin/bash
# Git Integration Test Script
# Tests the git integration features of HeliosDB Nano

set -e

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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BINARY="$SCRIPT_DIR/heliosdb-nano"
TEST_DIR="/tmp/heliosdb-git-test"
DB_PATH="$TEST_DIR/testdb"

echo "========================================"
echo "HeliosDB Nano Git Integration Test"
echo "========================================"

# Cleanup previous test
rm -rf "$TEST_DIR"
mkdir -p "$TEST_DIR"

# Initialize a test git repository
echo -e "\n${YELLOW}1. Setting up test git repository...${NC}"
cd "$TEST_DIR"
git init test-repo
cd test-repo
git config user.email "test@example.com"
git config user.name "Test User"
echo "Initial file" > README.md
git add README.md
git commit -m "Initial commit"
echo -e "${GREEN}✓ Test git repository created${NC}"

# Test git init command
echo -e "\n${YELLOW}2. Testing 'helios git init'...${NC}"
$BINARY git init --database "$DB_PATH" --repo-path "$(pwd)" 2>&1 || echo "Note: May fail if branch not in DB yet"
echo -e "${GREEN}✓ Git init command executed${NC}"

# Test git status command
echo -e "\n${YELLOW}3. Testing 'helios git status'...${NC}"
$BINARY git status --database "$DB_PATH" 2>&1 || echo "Note: Git status may show no links yet"
echo -e "${GREEN}✓ Git status command executed${NC}"

# Test branch creation
echo -e "\n${YELLOW}4. Testing branch creation...${NC}"
$BINARY branch create feature-test --database "$DB_PATH" 2>&1 || true
echo -e "${GREEN}✓ Branch creation command executed${NC}"

# Test branch list
echo -e "\n${YELLOW}5. Testing branch list...${NC}"
$BINARY branch list --database "$DB_PATH" 2>&1 || true
echo -e "${GREEN}✓ Branch list command executed${NC}"

# Test SQL execution with DIFF
echo -e "\n${YELLOW}6. Testing SQL execution...${NC}"
$BINARY sql --database "$DB_PATH" -e "SELECT 1 as test" 2>&1 || true
echo -e "${GREEN}✓ SQL execution test completed${NC}"

# Test git link command
echo -e "\n${YELLOW}7. Testing 'helios git link'...${NC}"
$BINARY git link --git-branch main --database "$DB_PATH" --auto-sync 2>&1 || true
echo -e "${GREEN}✓ Git link command executed${NC}"

# Test commit state recording
echo -e "\n${YELLOW}8. Testing commit state recording...${NC}"
COMMIT_SHA=$(git rev-parse HEAD)
$BINARY git commit record --sha "$COMMIT_SHA" --database "$DB_PATH" --message "Test commit state" 2>&1 || true
echo -e "${GREEN}✓ Commit state recording executed${NC}"

# Test commit state list
echo -e "\n${YELLOW}9. Testing commit state list...${NC}"
$BINARY git commit list --database "$DB_PATH" 2>&1 || true
echo -e "${GREEN}✓ Commit state list executed${NC}"

# Test hooks installation
echo -e "\n${YELLOW}10. Testing hooks installation...${NC}"
$BINARY git hooks install --database "$DB_PATH" --repo-path "$(pwd)" 2>&1 || true
ls -la .git/hooks/ 2>&1 || true
echo -e "${GREEN}✓ Hooks installation executed${NC}"

# Test hooks status
echo -e "\n${YELLOW}11. Testing hooks status...${NC}"
$BINARY git hooks status --database "$DB_PATH" --repo-path "$(pwd)" 2>&1 || true
echo -e "${GREEN}✓ Hooks status executed${NC}"

# Test migration commands
echo -e "\n${YELLOW}12. Testing migration status...${NC}"
$BINARY migration status --database "$DB_PATH" 2>&1 || true
echo -e "${GREEN}✓ Migration status executed${NC}"

# Cleanup
echo -e "\n${YELLOW}Cleaning up...${NC}"
cd /
rm -rf "$TEST_DIR"

echo ""
echo "========================================"
echo -e "${GREEN}All Git Integration Tests Completed!${NC}"
echo "========================================"
