#!/bin/bash

# NocoDB Auto-Setup Script for BNKops Influence Campaign Tool
# Based on the successful map setup script
# This script creates tables in your existing NocoDB project
# 
# Updated: September 2025 - Added data migration option from existing NocoDB bases
# Usage: 
#   ./build-nocodb.sh                    # Create new base only
#   ./build-nocodb.sh --migrate-data     # Create new base with data migration option
#   ./build-nocodb.sh --help             # Show usage information

set -e  # Exit on any error

# Global variables for migration
MIGRATE_DATA=true
SOURCE_BASE_ID=""
SOURCE_TABLE_IDS=""

# Change to the influence root directory (parent of scripts directory)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INFLUENCE_ROOT="$(dirname "$SCRIPT_DIR")"
cd "$INFLUENCE_ROOT"

echo "Changed to influence root directory: $INFLUENCE_ROOT"

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

# Function to print colored output
print_status() {
    echo -e "${BLUE}[INFO]${NC} $1" >&2
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1" >&2
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1" >&2
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1" >&2
}

# Function to show usage information
show_usage() {
    cat << EOF
NocoDB Auto-Setup Script for BNKops Influence Campaign Tool

USAGE:
    $0 [OPTIONS]

OPTIONS:
    --migrate-data    Skip interactive prompt and enable data migration mode
    --help            Show this help message

DESCRIPTION:
    This script creates a new NocoDB base with the required tables for the Influence Campaign Tool.
    
    Interactive mode (default): Prompts you to choose between fresh installation or data migration.
    
    With --migrate-data option, skips the prompt and goes directly to migration setup, allowing
    you to select an existing base and migrate data from specific tables to the new base.

EXAMPLES:
    $0                     # Interactive mode - choose fresh or migration
    $0 --migrate-data      # Skip prompt, go directly to migration setup
    $0 --help              # Show this help

MIGRATION FEATURES:
    - Automatically detects current base from .env file settings
    - Interactive base and table selection with clear guidance
    - Filters out auto-generated columns (CreatedAt, UpdatedAt, etc.)
    - Preserves original data (creates new base, doesn't modify existing)
    - Progress tracking during import with detailed success/failure reporting

EOF
}

# Parse command line arguments
parse_arguments() {
    while [[ $# -gt 0 ]]; do
        case $1 in
            --migrate-data)
                MIGRATE_DATA=true
                shift
                ;;
            --help|-h)
                show_usage
                exit 0
                ;;
            *)
                print_error "Unknown option: $1"
                show_usage
                exit 1
                ;;
        esac
    done
}

# Check for required dependencies
check_dependencies() {
    local missing_deps=()
    
    # Check for jq (required for JSON parsing in migration)
    if ! command -v jq &> /dev/null; then
        missing_deps+=("jq")
    fi
    
    # Check for curl (should be available but let's verify)
    if ! command -v curl &> /dev/null; then
        missing_deps+=("curl")
    fi
    
    if [[ ${#missing_deps[@]} -gt 0 ]]; then
        print_error "Missing required dependencies: ${missing_deps[*]}"
        print_error "Please install the missing dependencies before running this script"
        print_status "On Ubuntu/Debian: sudo apt-get install ${missing_deps[*]}"
        print_status "On CentOS/RHEL: sudo yum install ${missing_deps[*]}"
        print_status "On macOS: brew install ${missing_deps[*]}"
        exit 1
    fi
}

# Check dependencies
check_dependencies

# Load environment variables
if [ -f ".env" ]; then
    set -a
    source .env
    set +a
    print_success "Environment variables loaded from .env"
else
    print_error ".env file not found!"
    exit 1
fi

# Validate required environment variables
if [ -z "$NOCODB_API_URL" ] || [ -z "$NOCODB_API_TOKEN" ]; then
    print_error "Required environment variables NOCODB_API_URL and NOCODB_API_TOKEN not set!"
    exit 1
fi

# Extract base URL from API URL and set up v2 API endpoints
BASE_URL=$(echo "$NOCODB_API_URL" | sed 's|/api/v1||')
API_BASE_V1="$NOCODB_API_URL"
API_BASE_V2="${BASE_URL}/api/v2"

# We'll create a new base for the influence campaign
BASE_ID=""

print_status "Using NocoDB instance: $BASE_URL"
print_status "Will create a new base for the Influence Campaign Tool"

# Function to make API calls with proper error handling
make_api_call() {
    local method=$1
    local endpoint=$2
    local data=$3
    local description=$4
    local api_version=${5:-"v2"}  # Default to v2
    
    print_status "$description"
    
    local response
    local http_code
    local full_url
    
    if [[ "$api_version" == "v1" ]]; then
        full_url="$API_BASE_V1$endpoint"
    else
        full_url="$API_BASE_V2$endpoint"
    fi
    
    if [ "$method" = "GET" ]; then
        response=$(curl -s -w "%{http_code}" -H "xc-token: $NOCODB_API_TOKEN" \
                       -H "Content-Type: application/json" \
                       --max-time 60 \
                       "$full_url" 2>/dev/null)
        curl_exit_code=$?
    else
        response=$(curl -s -w "%{http_code}" -X "$method" \
                       -H "xc-token: $NOCODB_API_TOKEN" \
                       -H "Content-Type: application/json" \
                       --max-time 60 \
                       -d "$data" \
                       "$full_url" 2>/dev/null)
        curl_exit_code=$?
    fi
    
    if [[ $curl_exit_code -ne 0 ]]; then
        print_error "Network error occurred while making API call (curl exit code: $curl_exit_code)"
        return 1
    fi
    
    if [[ -z "$response" ]]; then
        print_error "Empty response from API call"
        return 1
    fi
    
    http_code="${response: -3}"
    response_body="${response%???}"
    
    print_status "HTTP Code: $http_code"
    
    if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
        print_success "$description completed successfully"
        echo "$response_body"
    else
        print_error "$description failed with HTTP code: $http_code"
        print_error "Response: $response_body"
        return 1
    fi
}

# Function to test API connectivity
test_api_connectivity() {
    print_status "Testing API connectivity..."
    
    # Test basic connectivity first
    if ! curl -s --max-time 10 -I "$BASE_URL" > /dev/null 2>&1; then
        print_error "Cannot reach NocoDB instance at $BASE_URL"
        return 1
    fi
    
    # Test API with token using v2 endpoint
    local test_response
    test_response=$(curl -s --max-time 10 -w "%{http_code}" -H "xc-token: $NOCODB_API_TOKEN" \
                        -H "Content-Type: application/json" \
                        "$API_BASE_V2/meta/bases" 2>/dev/null || echo "CURL_ERROR")
    
    if [[ "$test_response" == "CURL_ERROR" ]]; then
        print_error "Network error when testing API"
        return 1
    fi
    
    local http_code="${test_response: -3}"
    
    if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
        print_success "API connectivity test successful"
        return 0
    else
        print_error "API test failed with HTTP code: $http_code"
        return 1
    fi
}

# Function to list all available bases
list_available_bases() {
    print_status "Fetching available NocoDB bases..."
    
    local response
    response=$(make_api_call "GET" "/meta/bases" "" "Fetching bases list" "v2")
    
    if [[ $? -eq 0 && -n "$response" ]]; then
        echo "$response"
        return 0
    else
        print_error "Failed to fetch bases list"
        return 1
    fi
}

# Function to list tables in a specific base
list_base_tables() {
    local base_id=$1
    
    print_status "Fetching tables for base: $base_id"
    
    local response
    response=$(make_api_call "GET" "/meta/bases/$base_id/tables" "" "Fetching tables list" "v2")
    
    if [[ $? -eq 0 && -n "$response" ]]; then
        echo "$response"
        return 0
    else
        print_error "Failed to fetch tables list"
        return 1
    fi
}

# Function to export data from a table
export_table_data() {
    local base_id=$1
    local table_id=$2
    local table_name=$3
    
    print_status "Exporting data from table: $table_name (ID: $table_id)"
    
    # First, get total count of records using a minimal request
    local count_response
    count_response=$(make_api_call "GET" "/tables/$table_id/records?limit=1" "" "Getting record count for $table_name" "v2")
    
    if [[ $? -ne 0 ]]; then
        print_warning "Failed to get record count for table: $table_name"
        return 1
    fi
    
    # Extract total count from pageInfo
    local total_count
    total_count=$(echo "$count_response" | jq -r '.pageInfo.totalRows // 0' 2>/dev/null)
    
    if [[ -z "$total_count" || "$total_count" == "null" || "$total_count" -eq 0 ]]; then
        print_warning "No records found in table: $table_name"
        echo '{"list": [], "pageInfo": {"totalRows": 0}}'
        return 0
    fi
    
    print_status "Found $total_count records in table: $table_name"
    
    # If we have a small number of records, get them all at once
    if [[ "$total_count" -le 100 ]]; then
        local data_response
        data_response=$(make_api_call "GET" "/tables/$table_id/records?limit=$total_count" "" "Exporting all records from $table_name" "v2")
        
        if [[ $? -eq 0 ]]; then
            echo "$data_response"
            return 0
        else
            print_error "Failed to export data from table: $table_name"
            return 1
        fi
    else
        # For larger datasets, implement pagination
        local all_records="[]"
        local offset=0
        local limit=100
        
        while [[ $offset -lt $total_count ]]; do
            print_status "Fetching records $((offset+1))-$((offset+limit)) of $total_count from $table_name"
            
            local batch_response
            batch_response=$(make_api_call "GET" "/tables/$table_id/records?limit=$limit&offset=$offset" "" "Fetching batch from $table_name" "v2")
            
            if [[ $? -eq 0 ]]; then
                local batch_records
                batch_records=$(echo "$batch_response" | jq -r '.list' 2>/dev/null)
                
                if [[ -n "$batch_records" && "$batch_records" != "null" ]]; then
                    all_records=$(echo "$all_records" | jq ". + $batch_records" 2>/dev/null)
                fi
            else
                print_warning "Failed to fetch batch from table: $table_name (offset: $offset)"
            fi
            
            offset=$((offset + limit))
        done
        
        # Return the compiled results
        echo "{\"list\": $all_records, \"pageInfo\": {\"totalRows\": $total_count}}"
        return 0
    fi
}

# Function to import data into a table
import_table_data() {
    local base_id=$1
    local table_id=$2
    local table_name=$3
    local data=$4
    
    # Check if data contains records
    local record_count=$(echo "$data" | grep -o '"list":\[' | wc -l)
    
    if [[ $record_count -eq 0 ]]; then
        print_warning "No data to import for table: $table_name"
        return 0
    fi
    
    # Extract the records array from the response
    local records_array
    records_array=$(echo "$data" | jq -r '.list' 2>/dev/null)
    
    if [[ -z "$records_array" || "$records_array" == "[]" || "$records_array" == "null" ]]; then
        print_warning "No valid records found in data for table: $table_name"
        return 0
    fi
    
    print_status "Importing data into table: $table_name (ID: $table_id)"
    
    # Count total records first
    local total_records
    total_records=$(echo "$records_array" | jq 'length' 2>/dev/null)
    print_status "Found $total_records records to import into $table_name"
    
    local import_count=0
    local success_count=0
    
    # Create temporary file to track results across subshell
    local temp_file="/tmp/nocodb_import_$$"
    echo "0" > "$temp_file"
    
    # Add progress reporting for large datasets
    local progress_interval=25
    if [[ $total_records -gt 200 ]]; then
        progress_interval=50
    fi
    if [[ $total_records -gt 1000 ]]; then
        progress_interval=100
    fi
    
    # Parse records and import them one by one (to handle potential ID conflicts)
    echo "$records_array" | jq -c '.[]' 2>/dev/null | while read -r record; do
        import_count=$((import_count + 1))
        
        # Show progress for large datasets
        if [[ $((import_count % progress_interval)) -eq 0 ]]; then
            print_status "Progress: $import_count/$total_records records processed for $table_name"
        fi
        
        # Remove auto-generated and system columns that can cause conflicts
        local cleaned_record
        cleaned_record=$(echo "$record" | jq '
            del(.Id) |
            del(.id) |
            del(.ID) |
            del(.CreatedAt) |
            del(.UpdatedAt) |
            del(.created_at) |
            del(.updated_at) |
            del(.ncRecordId) |
            del(.ncRecordHash)
        ' 2>/dev/null)
        
        if [[ -z "$cleaned_record" || "$cleaned_record" == "{}" || "$cleaned_record" == "null" ]]; then
            print_warning "Skipping empty record $import_count in $table_name"
            continue
        fi
        
        # Import the record
        local import_response
        import_response=$(make_api_call "POST" "/tables/$table_id/records" "$cleaned_record" "Importing record $import_count to $table_name" "v2" 2>/dev/null)
        
        if [[ $? -eq 0 ]]; then
            local current_success=$(cat "$temp_file" 2>/dev/null || echo "0")
            echo $((current_success + 1)) > "$temp_file"
        else
            print_warning "Failed to import record $import_count to $table_name"
        fi
    done
    
    # Read final success count
    local final_success_count=$(cat "$temp_file" 2>/dev/null || echo "0")
    rm -f "$temp_file"
    
    print_success "Data import completed for table: $table_name ($final_success_count/$total_records records imported)"
}

# Function to prompt user for base selection
select_source_base() {
    print_status "Fetching available bases for migration..."
    
    local bases_response
    bases_response=$(list_available_bases)
    
    if [[ $? -ne 0 ]]; then
        print_error "Failed to fetch available bases"
        return 1
    fi
    
    # Debug the response structure
    print_status "Raw response preview: ${bases_response:0:200}..."
    
    # Parse bases from the .list array - use direct approach since we know the structure
    local bases_info=""
    bases_info=$(echo "$bases_response" | jq -r '.list[] | "\(.id)|\(.title)|\(.description // "No description")"' 2>&1)
    
    # Check if jq failed
    local jq_exit_code=$?
    if [[ $jq_exit_code -ne 0 ]]; then
        print_error "jq parsing failed with exit code: $jq_exit_code"
        print_error "jq error output: $bases_info"
        return 1
    fi
    
    if [[ -z "$bases_info" ]]; then
        print_error "No bases found or parsing returned empty result"
        print_error "Response structure: $(echo "$bases_response" | jq -r 'keys' 2>/dev/null || echo "Invalid JSON")"
        # Show a sample of the data for debugging
        print_status "Sample data: $(echo "$bases_response" | jq -r '.list[0] // "No data found"' 2>/dev/null)"
        return 1
    fi
    
    # Try to detect current base from .env file
    local current_base_id=""
    if [[ -n "$NOCODB_PROJECT_ID" ]]; then
        current_base_id="$NOCODB_PROJECT_ID"
    fi
    
    echo ""
    print_status "Available bases for data migration:"
    print_status "====================================="
    
    local counter=1
    local suggested_option=""
    
    echo "$bases_info" | while IFS='|' read -r base_id title description; do
        if [[ "$base_id" == "$current_base_id" ]]; then
            echo "  $counter) $title (ID: $base_id) [CURRENT] - $description"
            suggested_option="$counter"
        else
            echo "  $counter) $title (ID: $base_id) - $description"
        fi
        counter=$((counter + 1))
    done
    
    echo ""
    if [[ -n "$current_base_id" ]]; then
        print_warning "Detected current base ID from .env: $current_base_id"
        echo -n "Enter the number of the base to migrate from (or 'skip'): "
    else
        echo -n "Enter the number of the base you want to migrate from (or 'skip'): "
    fi
    
    read -r selection
    
    if [[ "$selection" == "skip" ]]; then
        print_status "Skipping data migration"
        return 1
    fi
    
    if ! [[ "$selection" =~ ^[0-9]+$ ]]; then
        print_error "Invalid selection: $selection"
        return 1
    fi
    
    # Get the selected base ID
    local selected_base_id
    selected_base_id=$(echo "$bases_info" | sed -n "${selection}p" | cut -d'|' -f1)
    
    if [[ -z "$selected_base_id" ]]; then
        print_error "Invalid selection number: $selection"
        return 1
    fi
    
    SOURCE_BASE_ID="$selected_base_id"
    print_success "Selected base ID: $SOURCE_BASE_ID"
    return 0
}

# Function to select tables for migration
select_migration_tables() {
    local source_base_id=$1
    
    print_status "Fetching tables from source base..."
    
    local tables_response
    tables_response=$(list_base_tables "$source_base_id")
    
    if [[ $? -ne 0 ]]; then
        print_error "Failed to fetch tables from source base"
        return 1
    fi
    
    # Parse and display available tables
    local tables_info=""
    tables_info=$(echo "$tables_response" | jq -r '.list[] | "\(.id)|\(.title)|\(.table_name)"' 2>&1)
    
    # Check if jq failed
    local jq_exit_code=$?
    if [[ $jq_exit_code -ne 0 ]]; then
        print_error "jq parsing failed for tables with exit code: $jq_exit_code"
        print_error "jq error output: $tables_info"
        return 1
    fi
    
    if [[ -z "$tables_info" ]]; then
        print_error "No tables found or parsing returned empty result"
        print_error "Tables response structure: $(echo "$tables_response" | jq -r 'keys' 2>/dev/null || echo "Invalid JSON")"
        return 1
    fi
    
    echo ""
    print_status "Available tables in source base:"
    print_status "================================"
    
    local counter=1
    echo "$tables_info" | while IFS='|' read -r table_id title table_name; do
        echo "  $counter) $title (table: $table_name, ID: $table_id)"
        counter=$((counter + 1))
    done
    
    echo ""
    print_status "Select tables to migrate (comma-separated numbers, or 'all' for all tables):"
    echo -n "Selection: "
    read -r table_selection
    
    if [[ "$table_selection" == "all" ]]; then
        SOURCE_TABLE_IDS=$(echo "$tables_info" | cut -d'|' -f1 | tr '\n' ',' | sed 's/,$//')
    else
        # Parse comma-separated numbers
        local selected_ids=""
        IFS=',' read -ra SELECTIONS <<< "$table_selection"
        for selection in "${SELECTIONS[@]}"; do
            selection=$(echo "$selection" | xargs) # trim whitespace
            if [[ "$selection" =~ ^[0-9]+$ ]]; then
                local table_id
                table_id=$(echo "$tables_info" | sed -n "${selection}p" | cut -d'|' -f1)
                if [[ -n "$table_id" ]]; then
                    selected_ids="$selected_ids$table_id,"
                fi
            fi
        done
        SOURCE_TABLE_IDS=$(echo "$selected_ids" | sed 's/,$//') 
    fi
    
    if [[ -z "$SOURCE_TABLE_IDS" ]]; then
        print_error "No valid tables selected"
        return 1
    fi
    
    print_success "Selected table IDs: $SOURCE_TABLE_IDS"
    return 0
}

# Function to migrate data from source to destination
migrate_table_data() {
    local source_base_id=$1
    local dest_base_id=$2
    local source_table_id=$3
    local dest_table_id=$4
    local table_name=$5
    
    print_status "Migrating data from $table_name..."
    
    # Export data from source table
    local exported_data
    exported_data=$(export_table_data "$source_base_id" "$source_table_id" "$table_name")
    
    if [[ $? -ne 0 ]]; then
        print_error "Failed to export data from source table: $table_name"
        return 1
    fi
    
    # Import data to destination table
    import_table_data "$dest_base_id" "$dest_table_id" "$table_name" "$exported_data"
    
    if [[ $? -eq 0 ]]; then
        print_success "Successfully migrated data for table: $table_name"
        return 0
    else
        print_error "Failed to migrate data for table: $table_name"
        return 1
    fi
}

# Function to extract base ID from URL
extract_base_id_from_url() {
    local url="$1"
    echo "$url" | grep -o '/nc/[^/]*' | sed 's|/nc/||'
}

# Function to prompt user about data migration
prompt_migration_choice() {
    print_status "NocoDB Auto-Setup - Migration Options"
    print_status "====================================="
    echo ""
    print_status "This script will create a new NocoDB base with fresh tables."
    echo ""
    print_status "Migration Options:"
    print_status "  1) Fresh installation (create new base with default data)"
    print_status "  2) Migrate from existing base (preserve your current data)"
    echo ""
    
    # Check if we have existing project ID in .env to suggest migration
    if [[ -n "$NOCODB_PROJECT_ID" ]]; then
        print_warning "Detected existing project ID in .env: $NOCODB_PROJECT_ID"
        print_warning "You may want to migrate data from your current base."
    fi
    
    echo ""
    echo -n "Choose option (1 or 2): "
    read -r choice
    
    case $choice in
        1)
            print_status "Selected: Fresh installation"
            MIGRATE_DATA=false
            ;;
        2)
            print_status "Selected: Data migration"
            MIGRATE_DATA=true
            ;;
        *)
            print_error "Invalid choice: $choice"
            print_error "Please choose 1 or 2"
            exit 1
            ;;
    esac
}

# Function to create a table
create_table() {
    local base_id=$1
    local table_name=$2
    local table_data=$3
    local description=$4
    
    local response
    response=$(make_api_call "POST" "/meta/bases/$base_id/tables" "$table_data" "Creating table: $table_name ($description)" "v2")
    
    if [[ $? -eq 0 && -n "$response" ]]; then
        local table_id
        table_id=$(echo "$response" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
        
        if [[ -n "$table_id" ]]; then
            print_success "Table '$table_name' created with ID: $table_id"
            echo "$table_id"
        else
            print_error "Failed to extract table ID from response"
            print_error "Response was: $response"
            return 1
        fi
    else
        print_error "Failed to create table: $table_name"
        return 1
    fi
}

# Function to validate table creation
validate_table_ids() {
    local tables=("$@")
    for table_id in "${tables[@]}"; do
        if [[ -z "$table_id" || "$table_id" == "null" ]]; then
            print_error "Invalid table ID detected: '$table_id'"
            return 1
        fi
    done
    return 0
}



# Function to create the representatives table
create_representatives_table() {
    local base_id=$1
    
    local table_data='{
        "table_name": "influence_representatives",
        "title": "Influence Representatives",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "postal_code",
                "title": "Postal Code",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "name",
                "title": "Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "email",
                "title": "Email",
                "uidt": "Email",
                "rqd": false
            },
            {
                "column_name": "district_name",
                "title": "District Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "elected_office",
                "title": "Elected Office",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "party_name",
                "title": "Party Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "representative_set_name",
                "title": "Representative Set Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "url",
                "title": "Profile URL",
                "uidt": "URL",
                "rqd": false
            },
            {
                "column_name": "photo_url",
                "title": "Photo URL",
                "uidt": "URL",
                "rqd": false
            },
            {
                "column_name": "offices",
                "title": "Offices",
                "uidt": "LongText",
                "rqd": false
            },
            {
                "column_name": "cached_at",
                "title": "Cached At",
                "uidt": "DateTime",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "influence_representatives" "$table_data" "Representatives data from Represent API"
}

# Function to create the email logs table
create_email_logs_table() {
    local base_id=$1
    
    local table_data='{
        "table_name": "influence_email_logs",
        "title": "Influence Email Logs",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "recipient_email",
                "title": "Recipient Email",
                "uidt": "Email",
                "rqd": true
            },
            {
                "column_name": "sender_name",
                "title": "Sender Name",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "sender_email",
                "title": "Sender Email",
                "uidt": "Email",
                "rqd": true
            },
            {
                "column_name": "subject",
                "title": "Subject",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "message",
                "title": "Message",
                "uidt": "LongText",
                "rqd": false
            },
            {
                "column_name": "postal_code",
                "title": "Postal Code",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "status",
                "title": "Status",
                "uidt": "SingleSelect",
                "rqd": false,
                "colOptions": {
                    "options": [
                        {"title": "sent", "color": "#00ff00"},
                        {"title": "failed", "color": "#ff0000"},
                        {"title": "previewed", "color": "#0080ff"}
                    ]
                }
            },
            {
                "column_name": "sender_ip",
                "title": "Sender IP",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "sent_at",
                "title": "Sent At",
                "uidt": "DateTime",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "influence_email_logs" "$table_data" "Email sending logs"
}

# Function to create the postal codes table
create_postal_codes_table() {
    local base_id=$1
    
    local table_data='{
        "table_name": "influence_postal_codes",
        "title": "Influence Postal Codes",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "postal_code",
                "title": "Postal Code",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "city",
                "title": "City",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "province",
                "title": "Province",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "centroid_lat",
                "title": "Centroid Latitude",
                "uidt": "Decimal",
                "rqd": false,
                "meta": {
                    "precision": 10,
                    "scale": 8
                }
            },
            {
                "column_name": "centroid_lng",
                "title": "Centroid Longitude",
                "uidt": "Decimal",
                "rqd": false,
                "meta": {
                    "precision": 11,
                    "scale": 8
                }
            },
            {
                "column_name": "last_updated",
                "title": "Last Updated",
                "uidt": "DateTime",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "influence_postal_codes" "$table_data" "Postal code information cache"
}

# Function to create the campaigns table
create_campaigns_table() {
    local base_id="$1"
    
    local table_data='{
        "table_name": "influence_campaigns",
        "title": "Campaigns",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "slug",
                "title": "Campaign Slug",
                "uidt": "SingleLineText",
                "unique": true,
                "rqd": true
            },
            {
                "column_name": "title",
                "title": "Campaign Title",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "description",
                "title": "Description",
                "uidt": "LongText"
            },
            {
                "column_name": "email_subject",
                "title": "Email Subject",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "email_body",
                "title": "Email Body",
                "uidt": "LongText",
                "rqd": true
            },
            {
                "column_name": "call_to_action",
                "title": "Call to Action",
                "uidt": "LongText"
            },
            {
                "column_name": "cover_photo",
                "title": "Cover Photo",
                "uidt": "SingleLineText"
            },
            {
                "column_name": "status",
                "title": "Status",
                "uidt": "SingleSelect",
                "rqd": true,
                "colOptions": {
                    "options": [
                        {"title": "draft", "color": "#d0f1fd"},
                        {"title": "active", "color": "#c2f5e8"},
                        {"title": "paused", "color": "#ffdce5"},
                        {"title": "archived", "color": "#ffeab6"}
                    ]
                }
            },
            {
                "column_name": "allow_smtp_email",
                "title": "Allow SMTP Email",
                "uidt": "Checkbox",
                "cdf": "true"
            },
            {
                "column_name": "allow_mailto_link",
                "title": "Allow Mailto Link",
                "uidt": "Checkbox",
                "cdf": "true"
            },
            {
                "column_name": "collect_user_info",
                "title": "Collect User Info",
                "uidt": "Checkbox",
                "cdf": "true"
            },
            {
                "column_name": "show_email_count",
                "title": "Show Email Count",
                "uidt": "Checkbox",
                "cdf": "true"
            },
            {
                "column_name": "show_call_count",
                "title": "Show Call Count",
                "uidt": "Checkbox",
                "cdf": "true"
            },
            {
                "column_name": "allow_email_editing",
                "title": "Allow Email Editing",
                "uidt": "Checkbox",
                "cdf": "false"
            },
            {
                "column_name": "target_government_levels",
                "title": "Target Government Levels",
                "uidt": "MultiSelect",
                "colOptions": {
                    "options": [
                        {"title": "Federal", "color": "#cfdffe"},
                        {"title": "Provincial", "color": "#c2f5e8"},
                        {"title": "Municipal", "color": "#ffdaf6"},
                        {"title": "School Board", "color": "#ffeab6"}
                    ]
                }
            },
            {
                "column_name": "created_by_user_id",
                "title": "Created By User ID",
                "uidt": "Number",
                "rqd": false
            },
            {
                "column_name": "created_by_user_email",
                "title": "Created By User Email",
                "uidt": "Email",
                "rqd": false
            },
            {
                "column_name": "created_by_user_name",
                "title": "Created By User Name",
                "uidt": "SingleLineText",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "influence_campaigns" "$table_data" "Campaign definitions and settings"
}

# Function to create the campaign emails table
create_campaign_emails_table() {
    local base_id="$1"
    
    local table_data='{
        "table_name": "influence_campaign_emails",
        "title": "Campaign Emails",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "campaign_id",
                "title": "Campaign ID",
                "uidt": "Number",
                "rqd": true
            },
            {
                "column_name": "campaign_slug",
                "title": "Campaign Slug",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "user_email",
                "title": "User Email",
                "uidt": "Email"
            },
            {
                "column_name": "user_name",
                "title": "User Name",
                "uidt": "SingleLineText"
            },
            {
                "column_name": "user_postal_code",
                "title": "User Postal Code",
                "uidt": "SingleLineText"
            },
            {
                "column_name": "recipient_email",
                "title": "Recipient Email",
                "uidt": "Email",
                "rqd": true
            },
            {
                "column_name": "recipient_name",
                "title": "Recipient Name",
                "uidt": "SingleLineText"
            },
            {
                "column_name": "recipient_title",
                "title": "Recipient Title",
                "uidt": "SingleLineText"
            },
            {
                "column_name": "recipient_level",
                "title": "Government Level",
                "uidt": "SingleSelect",
                "colOptions": {
                    "options": [
                        {"title": "Federal", "color": "#cfdffe"},
                        {"title": "Provincial", "color": "#c2f5e8"},
                        {"title": "Municipal", "color": "#ffdaf6"},
                        {"title": "School Board", "color": "#ffeab6"},
                        {"title": "Other", "color": "#d1f7c4"}
                    ]
                }
            },
            {
                "column_name": "email_method",
                "title": "Email Method",
                "uidt": "SingleSelect",
                "rqd": true,
                "colOptions": {
                    "options": [
                        {"title": "smtp", "color": "#c2f5e8"},
                        {"title": "mailto", "color": "#ffdaf6"}
                    ]
                }
            },
            {
                "column_name": "subject",
                "title": "Subject",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "message",
                "title": "Message",
                "uidt": "LongText",
                "rqd": true
            },
            {
                "column_name": "status",
                "title": "Status",
                "uidt": "SingleSelect",
                "rqd": true,
                "colOptions": {
                    "options": [
                        {"title": "sent", "color": "#c2f5e8"},
                        {"title": "failed", "color": "#ffdce5"},
                        {"title": "clicked", "color": "#cfdffe"},
                        {"title": "user_info_captured", "color": "#f0e68c"}
                    ]
                }
            },
            {
                "column_name": "sent_at",
                "title": "Sent At",
                "uidt": "DateTime",
                "cdf": "now()",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "influence_campaign_emails" "$table_data" "Campaign email tracking"
}

# Function to create the call logs table
create_call_logs_table() {
    local base_id=$1
    
    local table_data='{
        "table_name": "influence_call_logs",
        "title": "Influence Call Logs",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "representative_name",
                "title": "Representative Name",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "representative_title",
                "title": "Representative Title",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "phone_number",
                "title": "Phone Number",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "office_type",
                "title": "Office Type",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "caller_name",
                "title": "Caller Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "caller_email",
                "title": "Caller Email",
                "uidt": "Email",
                "rqd": false
            },
            {
                "column_name": "postal_code",
                "title": "Postal Code",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "campaign_id",
                "title": "Campaign ID",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "campaign_slug",
                "title": "Campaign Slug",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "caller_ip",
                "title": "Caller IP",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "called_at",
                "title": "Called At",
                "uidt": "DateTime",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "influence_call_logs" "$table_data" "Phone call tracking logs"
}

# Function to create the representative responses table
create_representative_responses_table() {
    local base_id=$1
    
    local table_data='{
        "table_name": "influence_representative_responses",
        "title": "Representative Responses",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "campaign_id",
                "title": "Campaign ID",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "campaign_slug",
                "title": "Campaign Slug",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "representative_name",
                "title": "Representative Name",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "representative_title",
                "title": "Representative Title",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "representative_level",
                "title": "Representative Level",
                "uidt": "SingleSelect",
                "rqd": true,
                "colOptions": {
                    "options": [
                        {"title": "Federal", "color": "#e74c3c"},
                        {"title": "Provincial", "color": "#3498db"},
                        {"title": "Municipal", "color": "#2ecc71"},
                        {"title": "School Board", "color": "#f39c12"}
                    ]
                }
            },
            {
                "column_name": "response_type",
                "title": "Response Type",
                "uidt": "SingleSelect",
                "rqd": true,
                "colOptions": {
                    "options": [
                        {"title": "Email", "color": "#3498db"},
                        {"title": "Letter", "color": "#9b59b6"},
                        {"title": "Phone Call", "color": "#1abc9c"},
                        {"title": "Meeting", "color": "#e67e22"},
                        {"title": "Social Media", "color": "#34495e"},
                        {"title": "Other", "color": "#95a5a6"}
                    ]
                }
            },
            {
                "column_name": "response_text",
                "title": "Response Text",
                "uidt": "LongText",
                "rqd": true
            },
            {
                "column_name": "user_comment",
                "title": "User Comment",
                "uidt": "LongText",
                "rqd": false
            },
            {
                "column_name": "screenshot_url",
                "title": "Screenshot URL",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "submitted_by_name",
                "title": "Submitted By Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "submitted_by_email",
                "title": "Submitted By Email",
                "uidt": "Email",
                "rqd": false
            },
            {
                "column_name": "submitted_by_user_id",
                "title": "Submitted By User ID",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "is_anonymous",
                "title": "Is Anonymous",
                "uidt": "Checkbox",
                "cdf": "false"
            },
            {
                "column_name": "status",
                "title": "Status",
                "uidt": "SingleSelect",
                "cdf": "pending",
                "colOptions": {
                    "options": [
                        {"title": "pending", "color": "#f39c12"},
                        {"title": "approved", "color": "#2ecc71"},
                        {"title": "rejected", "color": "#e74c3c"}
                    ]
                }
            },
            {
                "column_name": "is_verified",
                "title": "Is Verified",
                "uidt": "Checkbox",
                "cdf": "false"
            },
            {
                "column_name": "upvote_count",
                "title": "Upvote Count",
                "uidt": "Number",
                "cdf": "0"
            },
            {
                "column_name": "submitted_ip",
                "title": "Submitted IP",
                "uidt": "SingleLineText",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "influence_representative_responses" "$table_data" "Community responses from representatives"
}

# Function to create the response upvotes table
create_response_upvotes_table() {
    local base_id=$1
    
    local table_data='{
        "table_name": "influence_response_upvotes",
        "title": "Response Upvotes",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "response_id",
                "title": "Response ID",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "user_id",
                "title": "User ID",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "user_email",
                "title": "User Email",
                "uidt": "Email",
                "rqd": false
            },
            {
                "column_name": "upvoted_ip",
                "title": "Upvoted IP",
                "uidt": "SingleLineText",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "influence_response_upvotes" "$table_data" "Track upvotes on responses"
}

# Function to create the users table
create_users_table() {
    local base_id="$1"

    local table_data='{
        "table_name": "influence_users",
        "title": "Users",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "email",
                "title": "Email",
                "uidt": "Email",
                "unique": true,
                "rqd": true
            },
            {
                "column_name": "name",
                "title": "Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "password",
                "title": "Password",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "phone",
                "title": "Phone",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "admin",
                "title": "Admin",
                "uidt": "Checkbox",
                "cdf": "false"
            },
            {
                "column_name": "user_type",
                "title": "User Type",
                "uidt": "SingleSelect",
                "cdf": "user",
                "colOptions": {
                    "options": [
                        {"title": "user", "color": "#3498db"},
                        {"title": "admin", "color": "#e74c3c"},
                        {"title": "temp", "color": "#f39c12"}
                    ]
                }
            },
            {
                "column_name": "expires_at",
                "title": "ExpiresAt",
                "uidt": "DateTime",
                "rqd": false
            },
            {
                "column_name": "expire_days",
                "title": "ExpireDays",
                "uidt": "Number",
                "rqd": false
            },
            {
                "column_name": "last_login",
                "title": "Last Login",
                "uidt": "DateTime",
                "rqd": false
            }
        ]
    }'

    create_table "$base_id" "influence_users" "$table_data" "User authentication and management"
}

# Function to create a new base
create_base() {
    local base_data='{
        "title": "BNKops Influence Campaign Tool",
        "type": "database"
    }'
    
    local response
    response=$(make_api_call "POST" "/meta/bases" "$base_data" "Creating new base: BNKops Influence Campaign Tool" "v2")
    
    if [[ $? -eq 0 && -n "$response" ]]; then
        local base_id
        base_id=$(echo "$response" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
        
        if [[ -n "$base_id" ]]; then
            print_success "Base 'BNKops Influence Campaign Tool' created with ID: $base_id"
            echo "$base_id"
        else
            print_error "Failed to extract base ID from response"
            return 1
        fi
    else
        print_error "Failed to create base"
        return 1
    fi
}

# Function to update .env file with table IDs
update_env_with_table_ids() {
    local base_id=$1
    local representatives_table_id=$2
    local email_logs_table_id=$3
    local postal_codes_table_id=$4
    local campaigns_table_id=$5
    local campaign_emails_table_id=$6
    local users_table_id=$7
    local call_logs_table_id=$8
    local representative_responses_table_id=$9
    local response_upvotes_table_id=${10}
    
    print_status "Updating .env file with NocoDB project and table IDs..."
    
    # Create backup of .env file
    if [ -f ".env" ]; then
        cp ".env" ".env.backup.$(date +%Y%m%d_%H%M%S)"
        print_status "Created backup of .env file"
    fi
    
    # Function to update or add environment variable
    update_env_var() {
        local var_name=$1
        local var_value=$2
        local env_file=${3:-".env"}
        
        if grep -q "^${var_name}=" "$env_file"; then
            # Variable exists, update it
            sed -i "s|^${var_name}=.*|${var_name}=${var_value}|" "$env_file"
            print_status "Updated ${var_name} in .env"
        else
            # Variable doesn't exist, add it
            echo "${var_name}=${var_value}" >> "$env_file"
            print_status "Added ${var_name} to .env"
        fi
    }
    
    # Update all environment variables
    update_env_var "NOCODB_PROJECT_ID" "$base_id"
    update_env_var "NOCODB_TABLE_REPRESENTATIVES" "$representatives_table_id"
    update_env_var "NOCODB_TABLE_EMAILS" "$email_logs_table_id"
    update_env_var "NOCODB_TABLE_POSTAL_CODES" "$postal_codes_table_id"
    update_env_var "NOCODB_TABLE_CAMPAIGNS" "$campaigns_table_id"
    update_env_var "NOCODB_TABLE_CAMPAIGN_EMAILS" "$campaign_emails_table_id"
    update_env_var "NOCODB_TABLE_USERS" "$users_table_id"
    update_env_var "NOCODB_TABLE_CALLS" "$call_logs_table_id"
    update_env_var "NOCODB_TABLE_REPRESENTATIVE_RESPONSES" "$representative_responses_table_id"
    update_env_var "NOCODB_TABLE_RESPONSE_UPVOTES" "$response_upvotes_table_id"
    
    print_success "Successfully updated .env file with all table IDs"
    
    # Display the updated values
    print_status ""
    print_status "Updated .env with the following values:"
    print_status "NOCODB_PROJECT_ID=$base_id"
    print_status "NOCODB_TABLE_REPRESENTATIVES=$representatives_table_id"
    print_status "NOCODB_TABLE_EMAILS=$email_logs_table_id"
    print_status "NOCODB_TABLE_POSTAL_CODES=$postal_codes_table_id"
    print_status "NOCODB_TABLE_CAMPAIGNS=$campaigns_table_id"
    print_status "NOCODB_TABLE_CAMPAIGN_EMAILS=$campaign_emails_table_id"
    print_status "NOCODB_TABLE_USERS=$users_table_id"
    print_status "NOCODB_TABLE_CALLS=$call_logs_table_id"
    print_status "NOCODB_TABLE_REPRESENTATIVE_RESPONSES=$representative_responses_table_id"
    print_status "NOCODB_TABLE_RESPONSE_UPVOTES=$response_upvotes_table_id"
}



# Main execution
main() {
    # Parse command line arguments
    parse_arguments "$@"
    
    print_status "Starting NocoDB Setup for BNKops Influence Campaign Tool..."
    print_status "============================================================"
    
    # First test API connectivity
    if ! test_api_connectivity; then
        print_error "API connectivity test failed"
        exit 1
    fi
    
    # Always prompt for migration choice unless --migrate-data was explicitly passed
    if [[ "$MIGRATE_DATA" != "true" ]]; then
        prompt_migration_choice
    fi
    
    # Handle data migration setup if requested
    if [[ "$MIGRATE_DATA" == "true" ]]; then
        print_status "Setting up data migration..."
        
        if ! select_source_base; then
            print_warning "Migration setup failed or skipped. Proceeding with fresh installation."
            MIGRATE_DATA=false
        elif ! select_migration_tables "$SOURCE_BASE_ID"; then
            print_warning "Table selection failed. Proceeding with fresh installation."
            MIGRATE_DATA=false
        else
            print_success "Migration setup completed successfully"
            print_status "Will migrate data from base: $SOURCE_BASE_ID"
            print_status "Selected tables: $SOURCE_TABLE_IDS"
        fi
        print_status ""
    fi
    
    print_status ""
    print_status "Creating new base for Influence Campaign Tool..."
    
    # Create a new base
    BASE_ID=$(create_base)
    if [[ $? -ne 0 || -z "$BASE_ID" ]]; then
        print_error "Failed to create base"
        exit 1
    fi
    
    print_status "Created base with ID: $BASE_ID"
    print_warning "This created a new NocoDB project for the Influence Campaign Tool"
    
    # Create tables
    print_status ""
    print_status "Creating tables..."
    
    # Create representatives table
    REPRESENTATIVES_TABLE_ID=$(create_representatives_table "$BASE_ID")
    if [[ $? -ne 0 ]]; then
        print_error "Failed to create representatives table"
        exit 1
    fi
    
    # Create email logs table  
    EMAIL_LOGS_TABLE_ID=$(create_email_logs_table "$BASE_ID")
    if [[ $? -ne 0 ]]; then
        print_error "Failed to create email logs table"
        exit 1
    fi
    
    # Create postal codes table
    POSTAL_CODES_TABLE_ID=$(create_postal_codes_table "$BASE_ID")
    if [[ $? -ne 0 ]]; then
        print_error "Failed to create postal codes table"
        exit 1
    fi
    
    # Create campaigns table
    CAMPAIGNS_TABLE_ID=$(create_campaigns_table "$BASE_ID")
    if [[ $? -ne 0 ]]; then
        print_error "Failed to create campaigns table"
        exit 1
    fi
    
    # Create campaign emails table
    CAMPAIGN_EMAILS_TABLE_ID=$(create_campaign_emails_table "$BASE_ID")
    if [[ $? -ne 0 ]]; then
        print_error "Failed to create campaign emails table"
        exit 1
    fi
    
    # Create users table
    USERS_TABLE_ID=$(create_users_table "$BASE_ID")
    if [[ $? -ne 0 ]]; then
        print_error "Failed to create users table"
        exit 1
    fi
    
    # Create call logs table
    CALL_LOGS_TABLE_ID=$(create_call_logs_table "$BASE_ID")
    if [[ $? -ne 0 ]]; then
        print_error "Failed to create call logs table"
        exit 1
    fi
    
    # Create representative responses table
    REPRESENTATIVE_RESPONSES_TABLE_ID=$(create_representative_responses_table "$BASE_ID")
    if [[ $? -ne 0 ]]; then
        print_error "Failed to create representative responses table"
        exit 1
    fi
    
    # Create response upvotes table
    RESPONSE_UPVOTES_TABLE_ID=$(create_response_upvotes_table "$BASE_ID")
    if [[ $? -ne 0 ]]; then
        print_error "Failed to create response upvotes table"
        exit 1
    fi
    
    # Validate all table IDs were created successfully
    if ! validate_table_ids "$REPRESENTATIVES_TABLE_ID" "$EMAIL_LOGS_TABLE_ID" "$POSTAL_CODES_TABLE_ID" "$CAMPAIGNS_TABLE_ID" "$CAMPAIGN_EMAILS_TABLE_ID" "$USERS_TABLE_ID" "$CALL_LOGS_TABLE_ID" "$REPRESENTATIVE_RESPONSES_TABLE_ID" "$RESPONSE_UPVOTES_TABLE_ID"; then
        print_error "One or more table IDs are invalid"
        exit 1
    fi
    
    # Wait a moment for tables to be fully created
    sleep 3
    
    # Handle data migration if enabled
    if [[ "$MIGRATE_DATA" == "true" && -n "$SOURCE_BASE_ID" && -n "$SOURCE_TABLE_IDS" ]]; then
        print_status "Starting data migration..."
        print_status "========================="
        
        # Create a mapping of table names to new table IDs for migration
        declare -A table_mapping
        table_mapping["influence_representatives"]="$REPRESENTATIVES_TABLE_ID"
        table_mapping["influence_email_logs"]="$EMAIL_LOGS_TABLE_ID"
        table_mapping["influence_postal_codes"]="$POSTAL_CODES_TABLE_ID" 
        table_mapping["influence_campaigns"]="$CAMPAIGNS_TABLE_ID"
        table_mapping["influence_campaign_emails"]="$CAMPAIGN_EMAILS_TABLE_ID"
        table_mapping["influence_users"]="$USERS_TABLE_ID"
        table_mapping["influence_call_logs"]="$CALL_LOGS_TABLE_ID"
        table_mapping["influence_representative_responses"]="$REPRESENTATIVE_RESPONSES_TABLE_ID"
        table_mapping["influence_response_upvotes"]="$RESPONSE_UPVOTES_TABLE_ID"
        
        # Get source table information
        local source_tables_response
        source_tables_response=$(list_base_tables "$SOURCE_BASE_ID")
        
        if [[ $? -eq 0 ]]; then
            # Process each selected table for migration
            IFS=',' read -ra TABLE_IDS <<< "$SOURCE_TABLE_IDS"
            for source_table_id in "${TABLE_IDS[@]}"; do
                # Get table info from source
                local table_info
                table_info=$(echo "$source_tables_response" | jq -r ".list[] | select(.id == \"$source_table_id\") | \"\(.table_name)|\(.title)\"" 2>/dev/null)
                
                if [[ -n "$table_info" ]]; then
                    local table_name=$(echo "$table_info" | cut -d'|' -f1)
                    local table_title=$(echo "$table_info" | cut -d'|' -f2)
                    
                    # Find corresponding destination table
                    local dest_table_id="${table_mapping[$table_name]}"
                    
                    if [[ -n "$dest_table_id" ]]; then
                        migrate_table_data "$SOURCE_BASE_ID" "$BASE_ID" "$source_table_id" "$dest_table_id" "$table_name"
                    else
                        print_warning "No destination table found for: $table_name (skipping)"
                    fi
                else
                    print_warning "Could not find table info for ID: $source_table_id (skipping)"
                fi
            done
        else
            print_error "Failed to get source table information for migration"
        fi
        
        print_status "Data migration completed"
        print_status "========================"
    fi
    
    print_status ""
    print_status "============================================================"
    print_success "NocoDB Setup completed successfully!"
    print_status "============================================================"
    
    print_status ""
    print_status "Created new base: BNKops Influence Campaign Tool (ID: $BASE_ID)"
    print_status "Created tables:"
    print_status "  - influence_representatives (ID: $REPRESENTATIVES_TABLE_ID)"
    print_status "  - influence_email_logs (ID: $EMAIL_LOGS_TABLE_ID)" 
    print_status "  - influence_postal_codes (ID: $POSTAL_CODES_TABLE_ID)"
    print_status "  - influence_campaigns (ID: $CAMPAIGNS_TABLE_ID)"
    print_status "  - influence_campaign_emails (ID: $CAMPAIGN_EMAILS_TABLE_ID)"
    print_status "  - influence_users (ID: $USERS_TABLE_ID)"
    print_status "  - influence_call_logs (ID: $CALL_LOGS_TABLE_ID)"
    print_status "  - influence_representative_responses (ID: $REPRESENTATIVE_RESPONSES_TABLE_ID)"
    print_status "  - influence_response_upvotes (ID: $RESPONSE_UPVOTES_TABLE_ID)"
    
    # Automatically update .env file with new project ID
    print_status ""
    print_status "Updating .env file with new project ID..."
    
    if [ -f ".env" ]; then
        # Update existing .env file
        if grep -q "NOCODB_PROJECT_ID=" .env; then
            # Replace existing NOCODB_PROJECT_ID
            sed -i "s/NOCODB_PROJECT_ID=.*/NOCODB_PROJECT_ID=$BASE_ID/" .env
            print_success "Updated NOCODB_PROJECT_ID in .env file"
        else
            # Add new NOCODB_PROJECT_ID
            echo "NOCODB_PROJECT_ID=$BASE_ID" >> .env
            print_success "Added NOCODB_PROJECT_ID to .env file"
        fi
    else
        print_error ".env file not found - please create one from .env.example"
        exit 1
    fi
    
    # Update .env file with table IDs
    update_env_with_table_ids "$BASE_ID" "$REPRESENTATIVES_TABLE_ID" "$EMAIL_LOGS_TABLE_ID" "$POSTAL_CODES_TABLE_ID" "$CAMPAIGNS_TABLE_ID" "$CAMPAIGN_EMAILS_TABLE_ID" "$USERS_TABLE_ID" "$CALL_LOGS_TABLE_ID" "$REPRESENTATIVE_RESPONSES_TABLE_ID" "$RESPONSE_UPVOTES_TABLE_ID"
    
    print_status ""
    print_status "============================================================"
    print_success "Automated setup completed successfully!"
    print_status "============================================================"
    
    print_status ""
    print_status "Created new base: BNKops Influence Campaign Tool (ID: $BASE_ID)"
    print_status "Updated .env file with project ID and all table IDs"
    print_status ""
    print_status "Next steps:"
    print_status "1. Check your NocoDB instance at: $BASE_URL"
    print_status "2. Verify the tables were created successfully"
    print_status "3. Start your influence campaign application with: docker compose up"
    print_status "4. The application will be available at: http://localhost:3333"
    print_status "5. Access the admin panel at: http://localhost:3333/admin.html"
    
    print_status ""
    print_warning "IMPORTANT: This script created a NEW base. Your existing data was NOT modified."
    print_warning "Updated .env file with new project ID and table IDs."
    print_warning "A backup of your previous .env file was created with a timestamp."
    
    if [[ "$MIGRATE_DATA" == "true" && -n "$SOURCE_BASE_ID" && -n "$SOURCE_TABLE_IDS" ]]; then
        print_warning "Data migration completed. Please verify your data in the new base."
        print_warning "The original base remains unchanged as a backup."
    fi
    
    print_status ""
    print_success "Your BNKops Influence Campaign Tool is ready to use!"
}

# Check if script is being run directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi