#!/bin/bash

# Test export dataset functionality
# Usage: ./test_export.sh [db_path]

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
SKRAAK="$PROJECT_DIR/skraak"

DB_PATH="${1:-$PROJECT_DIR/db/test.duckdb}"
EXPORT_DB="/tmp/skraak_export_test_$$.duckdb"

echo "=== Testing Export Dataset ==="
echo "Database: $DB_PATH"
echo ""

# Clean up any existing export
rm -f "$EXPORT_DB" "$EXPORT_DB.events.jsonl"

# Get a dataset ID to export
echo "Test 1: Get dataset ID..."
DATASET_ID=$("$SKRAAK" sql --db "$DB_PATH" "SELECT id FROM dataset WHERE active = true LIMIT 1" | jq -r '.rows[0].id')
if [ -z "$DATASET_ID" ] || [ "$DATASET_ID" = "null" ]; then
    echo "ERROR: No active dataset found"
    exit 1
fi
echo "  Dataset ID: $DATASET_ID"

# Test dry-run
echo ""
echo "Test 2: Dry-run export..."
OUTPUT=$("$SKRAAK" export dataset --db "$DB_PATH" --id "$DATASET_ID" --output "$EXPORT_DB" --dry-run)
echo "$OUTPUT" | jq -r '.message'
DRY_RUN=$(echo "$OUTPUT" | jq -r '.dry_run')
if [ "$DRY_RUN" != "true" ]; then
    echo "ERROR: dry_run should be true"
    exit 1
fi
echo "  ✓ Dry-run works"

# Verify no file created
if [ -f "$EXPORT_DB" ]; then
    echo "ERROR: Export file should not exist after dry-run"
    exit 1
fi
echo "  ✓ No file created in dry-run mode"

# Test actual export
echo ""
echo "Test 3: Export dataset..."
OUTPUT=$("$SKRAAK" export dataset --db "$DB_PATH" --id "$DATASET_ID" --output "$EXPORT_DB" --force)
echo "$OUTPUT" | jq -r '.message'

# Verify export file exists
if [ ! -f "$EXPORT_DB" ]; then
    echo "ERROR: Export file not created"
    exit 1
fi
echo "  ✓ Export file created"

# Verify event log file exists
if [ ! -f "$EXPORT_DB.events.jsonl" ]; then
    echo "ERROR: Event log file not created"
    exit 1
fi
echo "  ✓ Event log file created"

# Verify row counts
echo ""
echo "Test 4: Verify row counts..."
FILE_COUNT=$(echo "$OUTPUT" | jq -r '.row_counts.file')
EXPORTED_COUNT=$("$SKRAAK" sql --db "$EXPORT_DB" "SELECT COUNT(*) as count FROM file" | jq -r '.rows[0].count')
if [ "$FILE_COUNT" != "$EXPORTED_COUNT" ]; then
    echo "ERROR: File count mismatch: expected $FILE_COUNT, got $EXPORTED_COUNT"
    exit 1
fi
echo "  ✓ Row counts match ($FILE_COUNT files)"

# Verify dataset
echo ""
echo "Test 5: Verify dataset..."
DATASET_COUNT=$("$SKRAAK" sql --db "$EXPORT_DB" "SELECT COUNT(*) as count FROM dataset WHERE id = '$DATASET_ID'" | jq -r '.rows[0].count')
if [ "$DATASET_COUNT" != "1" ]; then
    echo "ERROR: Dataset not found in export"
    exit 1
fi
echo "  ✓ Dataset found in export"

# Test error handling - dataset not found
echo ""
echo "Test 6: Test error handling..."
ERROR=$("$SKRAAK" export dataset --db "$DB_PATH" --id "NOTAREALID" --output "$EXPORT_DB" 2>&1 || true)
if [[ ! "$ERROR" =~ "dataset not found" ]]; then
    echo "ERROR: Should report dataset not found"
    echo "$ERROR"
    exit 1
fi
echo "  ✓ Error handling works for missing dataset"

# Test --force overwrite
echo ""
echo "Test 7: Test --force overwrite..."
OUTPUT=$("$SKRAAK" export dataset --db "$DB_PATH" --id "$DATASET_ID" --output "$EXPORT_DB" --force 2>&1)
if [[ "$OUTPUT" =~ "error" ]]; then
    echo "ERROR: Should not error with --force"
    echo "$OUTPUT"
    exit 1
fi
echo "  ✓ --force overwrite works"

# Test error without --force
echo ""
echo "Test 8: Test error without --force..."
ERROR=$("$SKRAAK" export dataset --db "$DB_PATH" --id "$DATASET_ID" --output "$EXPORT_DB" 2>&1 || true)
if [[ ! "$ERROR" =~ "file exists" ]]; then
    echo "ERROR: Should report file exists"
    echo "$ERROR"
    exit 1
fi
echo "  ✓ Error handling works for existing file"

# Clean up
rm -f "$EXPORT_DB" "$EXPORT_DB.events.jsonl"

echo ""
echo "=== All tests passed ==="