#!/bin/bash
# Test import folder validation
# Usage: ./test_import.sh
# Uses fresh copy of production DB in /tmp (auto-cleaned)

source "$(dirname "$0")/test_lib.sh"

echo "=== Testing import folder validation ==="
echo ""

check_binary

# Create fresh test database
DB_PATH=$(fresh_test_db)
trap "cleanup_test_db '$DB_PATH'" EXIT
echo "Using fresh test database: $DB_PATH"
echo ""

SKRAAK="$PROJECT_DIR/skraak"

# Get test IDs from database
DATASET_ID=$($SKRAAK sql --db "$DB_PATH" "SELECT id FROM dataset WHERE active = true LIMIT 1" 2>/dev/null | jq -r '.rows[0].id // empty')
LOCATION_ID=$($SKRAAK sql --db "$DB_PATH" "SELECT id FROM location WHERE active = true LIMIT 1" 2>/dev/null | jq -r '.rows[0].id // empty')
CLUSTER_ID=$($SKRAAK sql --db "$DB_PATH" "SELECT id FROM cluster WHERE active = true LIMIT 1" 2>/dev/null | jq -r '.rows[0].id // empty')

if [ -z "$DATASET_ID" ] || [ -z "$LOCATION_ID" ] || [ -z "$CLUSTER_ID" ]; then
    echo -e "${RED}Error: Could not find test entities in database${NC}"
    exit 1
fi
echo "  Dataset: $DATASET_ID"
echo "  Location: $LOCATION_ID"
echo "  Cluster: $CLUSTER_ID"
echo ""

# Test 1: Non-existent folder (should fail)
echo "Test 1: Non-existent folder (should fail)"
result=$($SKRAAK import folder --db "$DB_PATH" --dataset "$DATASET_ID" --location "$LOCATION_ID" --cluster "$CLUSTER_ID" --folder /nonexistent/folder 2>&1 || true)
if echo "$result" | grep -qi "error\|not accessible\|not found\|no such"; then
    echo -e "${GREEN}${NC} Reject non-existent folder"
    ((TESTS_RUN++)) || true
    ((TESTS_PASSED++)) || true
else
    echo -e "${RED}${NC} Should have rejected non-existent folder: $result"
    ((TESTS_RUN++)) || true
    ((TESTS_FAILED++)) || true
fi

# Test 2: Invalid location ID (should fail)
echo ""
echo "Test 2: Invalid location_id (should fail)"
result=$($SKRAAK import folder --db "$DB_PATH" --dataset "$DATASET_ID" --location "INVALID123456" --cluster "$CLUSTER_ID" --folder /tmp 2>&1 || true)
if echo "$result" | grep -qi "error\|not found\|invalid\|validation"; then
    echo -e "${GREEN}${NC} Reject invalid location_id"
    ((TESTS_RUN++)) || true
    ((TESTS_PASSED++)) || true
else
    echo -e "${RED}${NC} Should have rejected invalid location_id: $result"
    ((TESTS_RUN++)) || true
    ((TESTS_FAILED++)) || true
fi

# Test 3: Missing required flags (should fail)
echo ""
echo "Test 3: Missing --cluster flag (should fail)"
result=$($SKRAAK import folder --db "$DB_PATH" --dataset "$DATASET_ID" --location "$LOCATION_ID" --folder /tmp 2>&1 || true)
if echo "$result" | grep -qi "error\|required\|missing"; then
    echo -e "${GREEN}${NC} Reject missing required flag"
    ((TESTS_RUN++)) || true
    ((TESTS_PASSED++)) || true
else
    echo -e "${RED}${NC} Should have rejected missing flag: $result"
    ((TESTS_RUN++)) || true
    ((TESTS_FAILED++)) || true
fi

echo ""
print_summary

echo ""
echo "Note: These tests validate error handling only."
echo "Actual file import requires real WAV files and valid paths."
echo ""
echo "For bulk import, use the CLI tool:"
echo "  skraak import bulk --db ./db/skraak.duckdb --dataset abc123 --csv import.csv --log progress.log"