#!/bin/bash
set -e

echo "=== MarkBase LUN Mapping Script ==="

USER_ID="${1:-demo}"
DB_PATH="data/users/$USER_ID.sqlite"

if [ ! -f "$DB_PATH" ]; then
    echo "ERROR: Database not found: $DB_PATH"
    echo "Run: cargo run -- scan --user $USER_ID --dir <directory>"
    exit 1
fi

echo "Reading file nodes from database..."
NODES=$(sqlite3 "$DB_PATH" "SELECT node_id FROM file_nodes WHERE node_type='file' LIMIT 100")

LUN_ID=1
for node_id in $NODES; do
    echo "Mapping LUN $LUN_ID -> node_id $node_id"
    sqlite3 "$DB_PATH" "INSERT OR REPLACE INTO lun_mapping (lun, node_id) VALUES ($LUN_ID, '$node_id')"
    LUN_ID=$((LUN_ID + 1))
done

echo ""
echo "Total mappings: $((LUN_ID - 1))"
echo "Query example: SELECT * FROM lun_mapping WHERE lun = 1"