#!/bin/bash

# NocoDB Auto-Setup Script
# This script automatically creates the necessary base and tables for the BNKops Map Viewer application using NocoDB.
# Based on requirements from README.md and using proper NocoDB column types
# 
# Creates three tables:
# 1. locations - Main table with GeoData, proper field types per README.md
# 2. login - Simple authentication table with Email, Name, Admin fields
# 3. settings - Configuration table with text fields only (no QR image storage)
#
# Updated: July 2025 - Always creates a new base, does not touch existing data

set -e  # Exit on any error

# 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
}

# Load environment variables
if [ -f ".env" ]; then
    # Use set -a to automatically export variables
    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"

print_status "Using NocoDB instance: $BASE_URL"

# 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
    
    print_status "Making $method request to: $full_url"
    
    if [ "$method" = "GET" ]; then
        response=$(curl -s -w "%{http_code}" -H "xc-token: $NOCODB_API_TOKEN" \
                       -H "Content-Type: application/json" \
                       --max-time 30 \
                       "$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 30 \
                       -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"
    print_status "Response preview: ${response_body:0:200}..."
    
    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 "Full URL: $full_url"
        print_error "Response: $response_body"
        return 1
    fi
}

# Function to create a project/base
create_project() {
    local project_name="$1"
    local project_data='{
        "title": "'"$project_name"'",
        "description": "Auto-generated project for NocoDB Map Viewer",
        "color": "#24716E"
    }'
    
    make_api_call "POST" "/meta/bases" "$project_data" "Creating project: $project_name" "v2"
}

# Function to create a table
create_table() {
    local base_id=$1
    local table_name=$2
    local table_data=$3
    local description=$4
    
    # Always create new table (no checking for existing)
    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
        # Extract table ID from response
        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"
            return 1
        fi
    else
        print_error "Failed to create table: $table_name"
        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}"
    local response_body="${test_response%???}"
    
    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"
        print_error "Response: $response_body"
        return 1
    fi
}

# Function to create new project with timestamp
create_new_project() {
    # Generate unique project name with timestamp
    local timestamp=$(date +"%Y%m%d_%H%M%S")
    local project_name="Map Viewer Project - $timestamp"
    
    # First test API connectivity
    if ! test_api_connectivity; then
        print_error "API connectivity test failed"
        exit 1
    fi
    
    print_status "Creating new base: $project_name"
    print_warning "This script will create a new base and will NOT touch any existing data"
    
    local new_base_response
    new_base_response=$(create_project "$project_name")
    
    if [[ $? -eq 0 ]]; then
        local new_base_id
        new_base_id=$(echo "$new_base_response" | grep -o '"id":"[^"]*"' | head -1 | sed 's/"id":"//;s/"//')
        
        if [ -n "$new_base_id" ]; then
            print_success "Created new base '$project_name' with ID: $new_base_id"
            echo "$new_base_id"
            return 0
        else
            print_error "Failed to extract base ID from response"
            exit 1
        fi
    else
        print_error "Failed to create new base"
        exit 1
    fi
}

# Function to create the main locations table
create_locations_table() {
    local base_id=$1
    
    local table_data='{
        "table_name": "locations",
        "title": "Locations",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "geo_location",
                "title": "Geo-Location",
                "uidt": "GeoData",
                "rqd": false
            },
            {
                "column_name": "latitude",
                "title": "latitude",
                "uidt": "Decimal",
                "rqd": false,
                "meta": {
                    "precision": 8,
                    "scale": 8
                }
            },
            {
                "column_name": "longitude",
                "title": "longitude",
                "uidt": "Decimal",
                "rqd": false,
                "meta": {
                    "precision": 8,
                    "scale": 8
                }
            },
            {
                "column_name": "first_name",
                "title": "First Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "last_name",
                "title": "Last Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "email",
                "title": "Email",
                "uidt": "Email",
                "rqd": false
            },
            {
                "column_name": "phone",
                "title": "Phone",
                "uidt": "PhoneNumber",
                "rqd": false
            },
            {
                "column_name": "unit_number",
                "title": "Unit Number",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "support_level",
                "title": "Support Level",
                "uidt": "SingleSelect",
                "rqd": false,
                "colOptions": {
                    "options": [
                        {"title": "1", "color": "#4CAF50"},
                        {"title": "2", "color": "#FFEB3B"},
                        {"title": "3", "color": "#FF9800"},
                        {"title": "4", "color": "#F44336"}
                    ]
                }
            },
            {
                "column_name": "address",
                "title": "Address",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "sign",
                "title": "Sign",
                "uidt": "Checkbox",
                "rqd": false
            },
            {
                "column_name": "sign_size",
                "title": "Sign Size",
                "uidt": "SingleSelect",
                "rqd": false,
                "colOptions": {
                    "options": [
                        {"title": "Regular", "color": "#2196F3"},
                        {"title": "Large", "color": "#4CAF50"},
                        {"title": "Unsure", "color": "#FF9800"}
                    ]
                }
            },
            {
                "column_name": "notes",
                "title": "Notes",
                "uidt": "LongText",
                "rqd": false
            },
            {
                "column_name": "title",
                "title": "title",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "category",
                "title": "category",
                "uidt": "SingleSelect",
                "rqd": false,
                "colOptions": {
                    "options": [
                        {"title": "Important", "color": "#F44336"},
                        {"title": "Event", "color": "#4CAF50"},
                        {"title": "Business", "color": "#2196F3"},
                        {"title": "Other", "color": "#FF9800"}
                    ]
                }
            },
            {
                "column_name": "created_at",
                "title": "Created At",
                "uidt": "DateTime",
                "rqd": false
            },
            {
                "column_name": "updated_at",
                "title": "Updated At",
                "uidt": "DateTime",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "locations" "$table_data" "Main locations table for map data"
}

# Function to create the login table
create_login_table() {
    local base_id=$1
    
    local table_data='{
        "table_name": "login",
        "title": "Login",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "email",
                "title": "Email",
                "uidt": "Email",
                "rqd": true
            },
            {
                "column_name": "password",
                "title": "Password",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "name",
                "title": "Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "admin",
                "title": "Admin",
                "uidt": "Checkbox",
                "rqd": false
            },
            {
                "column_name": "created_at",
                "title": "Created At",
                "uidt": "DateTime",
                "rqd": false
            },
            {
                "column_name": "last_login",
                "title": "Last Login",
                "uidt": "DateTime",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "login" "$table_data" "User authentication table"
}

# Function to create the settings table
create_settings_table() {
    local base_id=$1
    
    local table_data='{
        "table_name": "settings",
        "title": "Settings",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "created_at",
                "title": "created_at",
                "uidt": "DateTime",
                "rqd": false
            },
            {
                "column_name": "created_by",
                "title": "created_by",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "geo_location",
                "title": "Geo-Location",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "latitude",
                "title": "latitude",
                "uidt": "Decimal",
                "rqd": false,
                "meta": {
                    "precision": 8,
                    "scale": 8
                }
            },
            {
                "column_name": "longitude",
                "title": "longitude",
                "uidt": "Decimal",
                "rqd": false,
                "meta": {
                    "precision": 8,
                    "scale": 8
                }
            },
            {
                "column_name": "zoom",
                "title": "zoom",
                "uidt": "Number",
                "rqd": false
            },
            {
                "column_name": "walk_sheet_title",
                "title": "Walk Sheet Title",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "walk_sheet_subtitle",
                "title": "Walk Sheet Subtitle",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "walk_sheet_footer",
                "title": "Walk Sheet Footer",
                "uidt": "LongText",
                "rqd": false
            },
            {
                "column_name": "qr_code_1_url",
                "title": "QR Code 1 URL",
                "uidt": "URL",
                "rqd": false
            },
            {
                "column_name": "qr_code_1_label",
                "title": "QR Code 1 Label",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "qr_code_2_url",
                "title": "QR Code 2 URL",
                "uidt": "URL",
                "rqd": false
            },
            {
                "column_name": "qr_code_2_label",
                "title": "QR Code 2 Label",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "qr_code_3_url",
                "title": "QR Code 3 URL",
                "uidt": "URL",
                "rqd": false
            },
            {
                "column_name": "qr_code_3_label",
                "title": "QR Code 3 Label",
                "uidt": "SingleLineText",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "settings" "$table_data" "System configuration with walk sheet text fields"
}
# Function to create the shifts table
create_shifts_table() {
    local base_id=$1
    local table_data='{
        "table_name": "shifts",
        "title": "Shifts",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "title",
                "title": "Title",
                "uidt": "SingleLineText",
                "rqd": true
            },
            {
                "column_name": "description",
                "title": "Description",
                "uidt": "LongText",
                "rqd": false
            },
            {
                "column_name": "date",
                "title": "Date",
                "uidt": "Date",
                "rqd": true
            },
            {
                "column_name": "start_time",
                "title": "Start Time",
                "uidt": "Time",
                "rqd": true
            },
            {
                "column_name": "end_time",
                "title": "End Time",
                "uidt": "Time",
                "rqd": true
            },
            {
                "column_name": "location",
                "title": "Location",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "max_volunteers",
                "title": "Max Volunteers",
                "uidt": "Number",
                "rqd": true
            },
            {
                "column_name": "current_volunteers",
                "title": "Current Volunteers",
                "uidt": "Number",
                "rqd": false
            },
            {
                "column_name": "status",
                "title": "Status",
                "uidt": "SingleSelect",
                "rqd": false,
                "colOptions": {
                    "options": [
                        {"title": "Open", "color": "#4CAF50"},
                        {"title": "Full", "color": "#FF9800"},
                        {"title": "Cancelled", "color": "#F44336"}
                    ]
                }
            },
            {
                "column_name": "created_by",
                "title": "Created By",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "created_at",
                "title": "Created At",
                "uidt": "DateTime",
                "rqd": false
            },
            {
                "column_name": "updated_at",
                "title": "Updated At",
                "uidt": "DateTime",
                "rqd": false
            }
        ]
    }'
    
    create_table "$base_id" "shifts" "$table_data" "shifts table"
}

# Function to create the shift signups table
create_shift_signups_table() {
    local base_id=$1
    local table_data='{
        "table_name": "shift_signups",
        "title": "Shift Signups",
        "columns": [
            {
                "column_name": "id",
                "title": "ID",
                "uidt": "ID",
                "pk": true,
                "ai": true,
                "rqd": true
            },
            {
                "column_name": "shift_id",
                "title": "Shift ID",
                "uidt": "Number",
                "rqd": true
            },
            {
                "column_name": "shift_title",
                "title": "Shift Title",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "user_email",
                "title": "User Email",
                "uidt": "Email",
                "rqd": true
            },
            {
                "column_name": "user_name",
                "title": "User Name",
                "uidt": "SingleLineText",
                "rqd": false
            },
            {
                "column_name": "signup_date",
                "title": "Signup Date",
                "uidt": "DateTime",
                "rqd": false
            },
            {
                "column_name": "status",
                "title": "Status",
                "uidt": "SingleSelect",
                "rqd": false,
                "colOptions": {
                    "options": [
                        {"title": "Confirmed", "color": "#4CAF50"},
                        {"title": "Cancelled", "color": "#F44336"}
                    ]
                }
            }
        ]
    }'
    
    create_table "$base_id" "shift_signups" "$table_data" "shift signups table"
}


# Function to create default admin user
create_default_admin() {
    local base_id=$1
    local login_table_id=$2
    
    print_status "Creating default admin user..."

    local admin_data='{
        "email": "admin@thebunkerops.ca",
        "password": "admin123",
        "name": "Administrator",
        "admin": true,
        "created_at": "'"$(date -u +"%Y-%m-%d %H:%M:%S")"'"
    }'
    
    make_api_call "POST" "/tables/$login_table_id/records" "$admin_data" "Creating default admin user" "v2"
    
    print_warning "Default admin user created:"
    print_warning "  Email: admin@thebunkerops.ca"
    print_warning "  Name: Administrator"
    print_warning "  Admin: true"
    print_warning "  Note: This is a simplified login table for demonstration."
    print_warning "  You may need to implement proper authentication separately."
}

# Function to create default start location setting
create_default_start_location() {
    local base_id=$1
    local settings_table_id=$2
    
    print_status "Creating default settings row with start location..."
    
    local start_location_data='{
        "created_at": "'"$(date -u +"%Y-%m-%d %H:%M:%S")"'",
        "created_by": "system",
        "geo_location": "'"${DEFAULT_LAT:-53.5461}"';'"${DEFAULT_LNG:--113.4938}"'",
        "latitude": '"${DEFAULT_LAT:-53.5461}"',
        "longitude": '"${DEFAULT_LNG:--113.4938}"',
        "zoom": '"${DEFAULT_ZOOM:-11}"',
        "walk_sheet_title": "Campaign Walk Sheet",
        "walk_sheet_subtitle": "Door-to-Door Canvassing Form",
        "walk_sheet_footer": "Thank you for your participation in our campaign!",
        "qr_code_1_url": "https://example.com/signup",
        "qr_code_1_label": "Sign Up",
        "qr_code_2_url": "https://example.com/donate",
        "qr_code_2_label": "Donate",
        "qr_code_3_url": "https://example.com/volunteer",
        "qr_code_3_label": "Volunteer"
    }'
    
    make_api_call "POST" "/tables/$settings_table_id/records" "$start_location_data" "Creating default settings row" "v2"
}

# Main execution
main() {
    print_status "Starting NocoDB Auto-Setup..."
    print_status "================================"
    
    # Always create a new project
    print_status "Creating new base..."
    print_warning "This script creates a NEW base and does NOT modify existing data"
    BASE_ID=$(create_new_project)
    
    if [ -z "$BASE_ID" ]; then
        print_error "Failed to create new base"
        exit 1
    fi
    
    print_status "Working with new base ID: $BASE_ID"
    
    # Create tables
    print_status "Creating tables..."
    
    # Create locations table
    LOCATIONS_TABLE_ID=$(create_locations_table "$BASE_ID")
    
    # Create login table  
    LOGIN_TABLE_ID=$(create_login_table "$BASE_ID")
    
    # Create settings table
    SETTINGS_TABLE_ID=$(create_settings_table "$BASE_ID")
    
    # Create shifts table
    SHIFTS_TABLE_ID=$(create_shifts_table "$BASE_ID")
    
    # Create shift signups table
    SHIFT_SIGNUPS_TABLE_ID=$(create_shift_signups_table "$BASE_ID")
    
    # Wait a moment for tables to be fully created
    sleep 3
    
    # Create default data
    print_status "Setting up default data..."
    
    # Create default admin user
    create_default_admin "$BASE_ID" "$LOGIN_TABLE_ID"
    
    # Create default settings row (includes both start location and walk sheet config)
    create_default_start_location "$BASE_ID" "$SETTINGS_TABLE_ID"
    
    print_status "================================"
    print_success "NocoDB Auto-Setup completed successfully!"
    print_status "================================"
    
    print_status "Base ID: $BASE_ID"
    print_status ""
    print_status "Next steps:"
    print_status "1. Login to your NocoDB instance at: $BASE_URL"
    print_status "2. Find your new base and navigate to each table"
    print_status "3. For each table, copy the view URL and update your .env file:"
    print_status "   - NOCODB_VIEW_URL (for locations table)"
    print_status "   - NOCODB_LOGIN_SHEET (for login table)"
    print_status "   - NOCODB_SETTINGS_SHEET (for settings table)"
    print_status "   - NOCODB_SHIFTS_SHEET (for shifts table)"
    print_status "   - NOCODB_SHIFT_SIGNUPS_SHEET (for shift signups table)"
    print_status "4. The default admin user is: admin@thebunkerops.ca with password: admin123"
    print_status "5. IMPORTANT: Change the default password after first login!"
    print_status "6. Start adding your location data!"
    
    print_warning ""
    print_warning "IMPORTANT: This script created a NEW base. Your existing data was NOT modified."
    print_warning "Please update your .env file with the new table URLs from the newly created base."
    print_warning "SECURITY: Change the default admin password immediately after first login!"

    
    print_warning ""
    print_warning "IMPORTANT: This script created a NEW base. Your existing data was NOT modified."
    print_warning "Please update your .env file with the new table URLs from the newly created base."
}

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