#!/bin/bash

# Test script for write tools (create_dataset, create_location, create_cluster, create_cyclic_recording_pattern)
# Tests both valid and invalid inputs
# USES TEST DATABASE BY DEFAULT to preserve production data integrity

DB_PATH="${1:-../db/test.duckdb}"
SERVER_PATH="../skraak_mcp"

echo "=== Testing Write Tools for Skraak MCP Server ==="
echo "Database: $DB_PATH"
echo ""

# Initialize connection
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}'

echo ""
echo "=== Test 1: Create Cyclic Recording Pattern (Valid) ==="
echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"create_cyclic_recording_pattern","arguments":{"record_seconds":30,"sleep_seconds":90}}}'

echo ""
echo "=== Test 2: Create Cyclic Recording Pattern (Invalid - negative values) ==="
echo '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"create_cyclic_recording_pattern","arguments":{"record_seconds":-10,"sleep_seconds":90}}}'

echo ""
echo "=== Test 3: Create Dataset (Valid - organise type) ==="
echo '{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"create_dataset","arguments":{"name":"Test Dataset 2026","description":"Created by automated test script","type":"organise"}}}'

echo ""
echo "=== Test 4: Create Dataset (Valid - test type, no description) ==="
echo '{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"create_dataset","arguments":{"name":"Test Dataset ML","type":"test"}}}'

echo ""
echo "=== Test 5: Create Dataset (Invalid - empty name) ==="
echo '{"jsonrpc":"2.0","id":6,"method":"tools/call","params":{"name":"create_dataset","arguments":{"name":"  ","type":"test"}}}'

echo ""
echo "=== Test 6: Create Dataset (Invalid - bad type) ==="
echo '{"jsonrpc":"2.0","id":7,"method":"tools/call","params":{"name":"create_dataset","arguments":{"name":"Bad Type Dataset","type":"invalid_type"}}}'

echo ""
echo "=== Test 7: Query recently created datasets to get IDs ==="
echo '{"jsonrpc":"2.0","id":8,"method":"tools/call","params":{"name":"execute_sql","arguments":{"query":"SELECT id, name, type FROM dataset WHERE name LIKE '\''Test Dataset%'\'' ORDER BY created_at DESC LIMIT 2"}}}'

echo ""
echo "=== Test 8: Create Location (Valid) ==="
echo "NOTE: Replace DATASET_ID_HERE with actual ID from Test 7 results"
echo '{"jsonrpc":"2.0","id":9,"method":"tools/call","params":{"name":"create_location","arguments":{"dataset_id":"DATASET_ID_HERE","name":"Test Location Auckland","latitude":-36.8485,"longitude":174.7633,"timezone_id":"Pacific/Auckland","description":"Test location in Auckland"}}}'

echo ""
echo "=== Test 9: Create Location (Invalid - bad coordinates) ==="
echo '{"jsonrpc":"2.0","id":10,"method":"tools/call","params":{"name":"create_location","arguments":{"dataset_id":"DATASET_ID_HERE","name":"Invalid Coords","latitude":999,"longitude":174.7633,"timezone_id":"Pacific/Auckland"}}}'

echo ""
echo "=== Test 10: Create Location (Invalid - bad timezone) ==="
echo '{"jsonrpc":"2.0","id":11,"method":"tools/call","params":{"name":"create_location","arguments":{"dataset_id":"DATASET_ID_HERE","name":"Bad Timezone","latitude":-36.8485,"longitude":174.7633,"timezone_id":"Invalid/Timezone"}}}'

echo ""
echo "=== Test 11: Create Location (Invalid - non-existent dataset) ==="
echo '{"jsonrpc":"2.0","id":12,"method":"tools/call","params":{"name":"create_location","arguments":{"dataset_id":"NONEXISTENT1","name":"Orphan Location","latitude":-36.8485,"longitude":174.7633,"timezone_id":"Pacific/Auckland"}}}'

echo ""
echo "=== Test 12: Query recently created locations to get IDs ==="
echo '{"jsonrpc":"2.0","id":13,"method":"tools/call","params":{"name":"execute_sql","arguments":{"query":"SELECT id, name, dataset_id FROM location WHERE name LIKE '\''Test Location%'\'' ORDER BY created_at DESC LIMIT 1"}}}'

echo ""
echo "=== Test 13: Create Cluster (Valid) ==="
echo "NOTE: Replace DATASET_ID_HERE and LOCATION_ID_HERE with actual IDs from previous results"
echo '{"jsonrpc":"2.0","id":14,"method":"tools/call","params":{"name":"create_cluster","arguments":{"dataset_id":"DATASET_ID_HERE","location_id":"LOCATION_ID_HERE","name":"Test Cluster Alpha","sample_rate":44100,"description":"Test cluster with 44.1kHz sample rate"}}}'

echo ""
echo "=== Test 14: Create Cluster (Invalid - sample rate zero) ==="
echo '{"jsonrpc":"2.0","id":15,"method":"tools/call","params":{"name":"create_cluster","arguments":{"dataset_id":"DATASET_ID_HERE","location_id":"LOCATION_ID_HERE","name":"Bad Sample Rate","sample_rate":0}}}'

echo ""
echo "=== Test 15: Create Cluster (Invalid - location/dataset mismatch) ==="
echo "NOTE: Uses wrong dataset_id for the location"
echo '{"jsonrpc":"2.0","id":16,"method":"tools/call","params":{"name":"create_cluster","arguments":{"dataset_id":"WRONG_DATASET","location_id":"LOCATION_ID_HERE","name":"Mismatched Cluster","sample_rate":48000}}}'

echo ""
echo "=== Test 16: Query recently created clusters ==="
echo '{"jsonrpc":"2.0","id":17,"method":"tools/call","params":{"name":"execute_sql","arguments":{"query":"SELECT c.id, c.name, c.sample_rate, l.name as location_name, d.name as dataset_name FROM cluster c JOIN location l ON c.location_id = l.id JOIN dataset d ON c.dataset_id = d.id WHERE c.name LIKE '\''Test Cluster%'\'' ORDER BY c.created_at DESC LIMIT 1"}}}'

echo ""
echo "=== End of Write Tools Tests ==="
echo ""
echo "MANUAL STEPS REQUIRED:"
echo "1. Run this script and capture output: ./test_write_tools.sh > test_write_output.txt 2>&1"
echo "2. Extract IDs from Test 7 results (dataset IDs)"
echo "3. Extract ID from Test 12 results (location ID)"
echo "4. Edit Tests 8-16 to replace DATASET_ID_HERE and LOCATION_ID_HERE with actual IDs"
echo "5. Run individual tests with correct IDs to verify write operations"
echo ""
echo "VERIFICATION COMMANDS:"
echo "  rg '\"result\"' test_write_output.txt | wc -l    # Count successful responses"
echo "  rg 'error' test_write_output.txt                # Check for errors"
echo "  rg 'Successfully created' test_write_output.txt # Check success messages"