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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#!/bin/bash

# Local Development Setup Script
# Starts all services required for the misinformation heatmap application

set -e

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

# Configuration
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BACKEND_DIR="$PROJECT_ROOT/backend"
FRONTEND_DIR="$PROJECT_ROOT/frontend"
DATA_DIR="$PROJECT_ROOT/data"
LOGS_DIR="$PROJECT_ROOT/logs"

# Default configuration
MODE="local"
SKIP_DEPS=false
SKIP_DB_INIT=false
SKIP_FRONTEND=false
VERBOSE=false
DETACHED=false

# Function to print colored output
print_header() {
    echo -e "${PURPLE}================================${NC}"
    echo -e "${PURPLE}$1${NC}"
    echo -e "${PURPLE}================================${NC}"
}

print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

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

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

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

# Function to show usage
show_usage() {
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  -h, --help          Show this help message"
    echo "  -m, --mode MODE     Set deployment mode (local|cloud) [default: local]"
    echo "  -s, --skip-deps     Skip dependency installation"
    echo "  -d, --skip-db       Skip database initialization"
    echo "  -f, --skip-frontend Skip frontend serving"
    echo "  -v, --verbose       Enable verbose output"
    echo "  -D, --detached      Run services in detached mode"
    echo "  --stop              Stop all running services"
    echo "  --status            Show status of running services"
    echo "  --logs              Show logs from all services"
    echo ""
    echo "Examples:"
    echo "  $0                  Start all services in local mode"
    echo "  $0 --mode cloud     Start with cloud configuration"
    echo "  $0 --skip-deps      Start without installing dependencies"
    echo "  $0 --detached       Start services in background"
    echo "  $0 --stop           Stop all running services"
}

# Function to parse command line arguments
parse_args() {
    while [[ $# -gt 0 ]]; do
        case $1 in
            -h|--help)
                show_usage
                exit 0
                ;;
            -m|--mode)
                MODE="$2"
                shift 2
                ;;
            -s|--skip-deps)
                SKIP_DEPS=true
                shift
                ;;
            -d|--skip-db)
                SKIP_DB_INIT=true
                shift
                ;;
            -f|--skip-frontend)
                SKIP_FRONTEND=true
                shift
                ;;
            -v|--verbose)
                VERBOSE=true
                shift
                ;;
            -D|--detached)
                DETACHED=true
                shift
                ;;
            --stop)
                stop_services
                exit 0
                ;;
            --status)
                show_status
                exit 0
                ;;
            --logs)
                show_logs
                exit 0
                ;;
            *)
                print_error "Unknown option: $1"
                show_usage
                exit 1
                ;;
        esac
    done
}

# Function to check prerequisites
check_prerequisites() {
    print_status "Checking prerequisites..."
    
    # Check Python
    if ! command -v python3 &> /dev/null; then
        print_error "Python 3 is not installed. Please install Python 3.8 or higher."
        exit 1
    fi
    
    local python_version=$(python3 --version | cut -d' ' -f2)
    print_status "Python version: $python_version"
    
    # Check pip
    if ! command -v pip3 &> /dev/null; then
        print_error "pip3 is not installed. Please install pip3."
        exit 1
    fi
    
    # Check Node.js (for frontend)
    if [[ "$SKIP_FRONTEND" == false ]]; then
        if ! command -v node &> /dev/null; then
            print_warning "Node.js not found. Frontend serving will be disabled."
            SKIP_FRONTEND=true
        else
            local node_version=$(node --version)
            print_status "Node.js version: $node_version"
        fi
    fi
    
    # Check required directories
    for dir in "$BACKEND_DIR" "$FRONTEND_DIR" "$DATA_DIR"; do
        if [[ ! -d "$dir" ]]; then
            print_error "Required directory not found: $dir"
            exit 1
        fi
    done
    
    print_success "Prerequisites check completed"
}

# Function to create necessary directories
create_directories() {
    print_status "Creating necessary directories..."
    
    mkdir -p "$LOGS_DIR"
    mkdir -p "$DATA_DIR"
    mkdir -p "$PROJECT_ROOT/.env"
    
    print_success "Directories created"
}

# Function to install dependencies
install_dependencies() {
    if [[ "$SKIP_DEPS" == true ]]; then
        print_warning "Skipping dependency installation"
        return
    fi
    
    print_status "Installing Python dependencies..."
    
    cd "$BACKEND_DIR"
    
    # Create virtual environment if it doesn't exist
    if [[ ! -d "venv" ]]; then
        print_status "Creating Python virtual environment..."
        python3 -m venv venv
    fi
    
    # Activate virtual environment
    source venv/bin/activate
    
    # Upgrade pip
    pip install --upgrade pip
    
    # Install requirements
    if [[ -f "requirements.txt" ]]; then
        pip install -r requirements.txt
    else
        print_error "requirements.txt not found in backend directory"
        exit 1
    fi
    
    print_success "Python dependencies installed"
    
    # Install frontend dependencies if needed
    if [[ "$SKIP_FRONTEND" == false && -f "$FRONTEND_DIR/package.json" ]]; then
        print_status "Installing frontend dependencies..."
        cd "$FRONTEND_DIR"
        npm install
        print_success "Frontend dependencies installed"
    fi
}

# Function to initialize database
initialize_database() {
    if [[ "$SKIP_DB_INIT" == true ]]; then
        print_warning "Skipping database initialization"
        return
    fi
    
    print_status "Initializing database..."
    
    cd "$BACKEND_DIR"
    source venv/bin/activate
    
    # Run database initialization script
    if [[ -f "init_db.py" ]]; then
        python init_db.py --mode "$MODE"
        print_success "Database initialized"
    else
        print_warning "Database initialization script not found, skipping..."
    fi
}

# Function to setup environment variables
setup_environment() {
    print_status "Setting up environment variables..."
    
    # Create .env file if it doesn't exist
    local env_file="$PROJECT_ROOT/.env"
    if [[ ! -f "$env_file" ]]; then
        print_status "Creating .env file..."
        cat > "$env_file" << EOF
# Misinformation Heatmap Configuration
MODE=$MODE
DEBUG=true
LOG_LEVEL=INFO

# Database Configuration
DATABASE_URL=sqlite:///./data/heatmap.db

# API Configuration
API_HOST=0.0.0.0
API_PORT=8000
CORS_ORIGINS=["http://localhost:3000", "http://localhost:8000"]

# NLP Configuration
HUGGINGFACE_TOKEN=
MODEL_CACHE_DIR=./models

# Pub/Sub Configuration (Local)
PUBSUB_EMULATOR_HOST=localhost:8085
PUBSUB_PROJECT_ID=misinformation-heatmap-local

# Google Cloud Configuration (for cloud mode)
GOOGLE_CLOUD_PROJECT=
GOOGLE_APPLICATION_CREDENTIALS=

# IBM Watson Configuration (for cloud mode)
WATSON_DISCOVERY_API_KEY=
WATSON_DISCOVERY_URL=

# Monitoring Configuration
ENABLE_METRICS=true
METRICS_PORT=9090
EOF
        print_success ".env file created"
    else
        print_status ".env file already exists"
    fi
}

# Function to start Pub/Sub emulator
start_pubsub_emulator() {
    if [[ "$MODE" != "local" ]]; then
        return
    fi
    
    print_status "Starting Pub/Sub emulator..."
    
    # Check if gcloud is installed
    if ! command -v gcloud &> /dev/null; then
        print_warning "gcloud CLI not found. Installing Pub/Sub emulator..."
        # Install using pip as fallback
        pip install google-cloud-pubsub
    fi
    
    # Start emulator in background
    local pubsub_log="$LOGS_DIR/pubsub-emulator.log"
    
    if [[ "$DETACHED" == true ]]; then
        nohup gcloud beta emulators pubsub start --host-port=localhost:8085 > "$pubsub_log" 2>&1 &
        local pubsub_pid=$!
        echo $pubsub_pid > "$LOGS_DIR/pubsub.pid"
        print_success "Pub/Sub emulator started (PID: $pubsub_pid)"
    else
        print_status "Starting Pub/Sub emulator (press Ctrl+C to stop all services)"
        gcloud beta emulators pubsub start --host-port=localhost:8085 &
        local pubsub_pid=$!
        echo $pubsub_pid > "$LOGS_DIR/pubsub.pid"
    fi
    
    # Wait for emulator to start
    sleep 3
    
    # Create topics and subscriptions
    export PUBSUB_EMULATOR_HOST=localhost:8085
    python3 -c "
import os
from google.cloud import pubsub_v1

os.environ['PUBSUB_EMULATOR_HOST'] = 'localhost:8085'
project_id = 'misinformation-heatmap-local'

publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()

# Create topics
topics = ['events-raw', 'events-processed', 'events-validated']
for topic_name in topics:
    topic_path = publisher.topic_path(project_id, topic_name)
    try:
        publisher.create_topic(request={'name': topic_path})
        print(f'Created topic: {topic_name}')
    except Exception as e:
        print(f'Topic {topic_name} may already exist: {e}')

# Create subscriptions
subscriptions = [
    ('events-raw', 'events-raw-sub'),
    ('events-processed', 'events-processed-sub'),
    ('events-validated', 'events-validated-sub')
]

for topic_name, sub_name in subscriptions:
    topic_path = publisher.topic_path(project_id, topic_name)
    subscription_path = subscriber.subscription_path(project_id, sub_name)
    try:
        subscriber.create_subscription(
            request={'name': subscription_path, 'topic': topic_path}
        )
        print(f'Created subscription: {sub_name}')
    except Exception as e:
        print(f'Subscription {sub_name} may already exist: {e}')
"
}

# Function to start backend API
start_backend() {
    print_status "Starting backend API server..."
    
    cd "$BACKEND_DIR"
    source venv/bin/activate
    
    local api_log="$LOGS_DIR/api.log"
    
    if [[ "$DETACHED" == true ]]; then
        nohup python api.py > "$api_log" 2>&1 &
        local api_pid=$!
        echo $api_pid > "$LOGS_DIR/api.pid"
        print_success "Backend API started (PID: $api_pid)"
    else
        print_status "Starting backend API (press Ctrl+C to stop all services)"
        python api.py &
        local api_pid=$!
        echo $api_pid > "$LOGS_DIR/api.pid"
    fi
}

# Function to start frontend server
start_frontend() {
    if [[ "$SKIP_FRONTEND" == true ]]; then
        print_warning "Skipping frontend server"
        return
    fi
    
    print_status "Starting frontend server..."
    
    cd "$FRONTEND_DIR"
    
    local frontend_log="$LOGS_DIR/frontend.log"
    
    # Use Python's built-in HTTP server for simplicity
    if [[ "$DETACHED" == true ]]; then
        nohup python3 -m http.server 3000 > "$frontend_log" 2>&1 &
        local frontend_pid=$!
        echo $frontend_pid > "$LOGS_DIR/frontend.pid"
        print_success "Frontend server started (PID: $frontend_pid)"
    else
        print_status "Starting frontend server (press Ctrl+C to stop all services)"
        python3 -m http.server 3000 &
        local frontend_pid=$!
        echo $frontend_pid > "$LOGS_DIR/frontend.pid"
    fi
}

# Function to perform health checks
perform_health_checks() {
    print_status "Performing health checks..."
    
    # Wait a moment for services to start
    sleep 5
    
    # Check backend API
    if curl -s http://localhost:8000/health > /dev/null; then
        print_success "Backend API is healthy"
    else
        print_warning "Backend API health check failed"
    fi
    
    # Check frontend
    if [[ "$SKIP_FRONTEND" == false ]]; then
        if curl -s http://localhost:3000 > /dev/null; then
            print_success "Frontend server is healthy"
        else
            print_warning "Frontend server health check failed"
        fi
    fi
    
    # Check Pub/Sub emulator (local mode only)
    if [[ "$MODE" == "local" ]]; then
        if curl -s http://localhost:8085 > /dev/null; then
            print_success "Pub/Sub emulator is healthy"
        else
            print_warning "Pub/Sub emulator health check failed"
        fi
    fi
}

# Function to show running services status
show_status() {
    print_header "Service Status"
    
    # Check API
    if [[ -f "$LOGS_DIR/api.pid" ]]; then
        local api_pid=$(cat "$LOGS_DIR/api.pid")
        if ps -p $api_pid > /dev/null; then
            print_success "Backend API running (PID: $api_pid)"
        else
            print_warning "Backend API not running (stale PID file)"
        fi
    else
        print_warning "Backend API not started"
    fi
    
    # Check frontend
    if [[ -f "$LOGS_DIR/frontend.pid" ]]; then
        local frontend_pid=$(cat "$LOGS_DIR/frontend.pid")
        if ps -p $frontend_pid > /dev/null; then
            print_success "Frontend server running (PID: $frontend_pid)"
        else
            print_warning "Frontend server not running (stale PID file)"
        fi
    else
        print_warning "Frontend server not started"
    fi
    
    # Check Pub/Sub emulator
    if [[ -f "$LOGS_DIR/pubsub.pid" ]]; then
        local pubsub_pid=$(cat "$LOGS_DIR/pubsub.pid")
        if ps -p $pubsub_pid > /dev/null; then
            print_success "Pub/Sub emulator running (PID: $pubsub_pid)"
        else
            print_warning "Pub/Sub emulator not running (stale PID file)"
        fi
    else
        print_warning "Pub/Sub emulator not started"
    fi
}

# Function to show logs
show_logs() {
    print_header "Service Logs"
    
    if [[ -f "$LOGS_DIR/api.log" ]]; then
        echo -e "${BLUE}=== Backend API Logs ===${NC}"
        tail -n 20 "$LOGS_DIR/api.log"
        echo ""
    fi
    
    if [[ -f "$LOGS_DIR/frontend.log" ]]; then
        echo -e "${BLUE}=== Frontend Server Logs ===${NC}"
        tail -n 20 "$LOGS_DIR/frontend.log"
        echo ""
    fi
    
    if [[ -f "$LOGS_DIR/pubsub-emulator.log" ]]; then
        echo -e "${BLUE}=== Pub/Sub Emulator Logs ===${NC}"
        tail -n 20 "$LOGS_DIR/pubsub-emulator.log"
        echo ""
    fi
}

# Function to stop all services
stop_services() {
    print_header "Stopping Services"
    
    # Stop API
    if [[ -f "$LOGS_DIR/api.pid" ]]; then
        local api_pid=$(cat "$LOGS_DIR/api.pid")
        if ps -p $api_pid > /dev/null; then
            kill $api_pid
            print_success "Backend API stopped"
        fi
        rm -f "$LOGS_DIR/api.pid"
    fi
    
    # Stop frontend
    if [[ -f "$LOGS_DIR/frontend.pid" ]]; then
        local frontend_pid=$(cat "$LOGS_DIR/frontend.pid")
        if ps -p $frontend_pid > /dev/null; then
            kill $frontend_pid
            print_success "Frontend server stopped"
        fi
        rm -f "$LOGS_DIR/frontend.pid"
    fi
    
    # Stop Pub/Sub emulator
    if [[ -f "$LOGS_DIR/pubsub.pid" ]]; then
        local pubsub_pid=$(cat "$LOGS_DIR/pubsub.pid")
        if ps -p $pubsub_pid > /dev/null; then
            kill $pubsub_pid
            print_success "Pub/Sub emulator stopped"
        fi
        rm -f "$LOGS_DIR/pubsub.pid"
    fi
    
    print_success "All services stopped"
}

# Function to handle cleanup on exit
cleanup() {
    if [[ "$DETACHED" == false ]]; then
        print_status "Cleaning up..."
        stop_services
    fi
}

# Function to display service URLs
show_service_urls() {
    print_header "Service URLs"
    echo -e "${GREEN}Backend API:${NC}      http://localhost:8000"
    echo -e "${GREEN}API Documentation:${NC} http://localhost:8000/docs"
    echo -e "${GREEN}Health Check:${NC}     http://localhost:8000/health"
    
    if [[ "$SKIP_FRONTEND" == false ]]; then
        echo -e "${GREEN}Frontend:${NC}         http://localhost:3000"
    fi
    
    if [[ "$MODE" == "local" ]]; then
        echo -e "${GREEN}Pub/Sub Emulator:${NC}  http://localhost:8085"
    fi
    
    echo ""
    echo -e "${YELLOW}Logs Directory:${NC}   $LOGS_DIR"
    echo -e "${YELLOW}Data Directory:${NC}   $DATA_DIR"
}

# Main execution function
main() {
    print_header "Misinformation Heatmap - Local Development Setup"
    
    parse_args "$@"
    
    # Set up signal handlers for cleanup
    trap cleanup EXIT INT TERM
    
    check_prerequisites
    create_directories
    setup_environment
    install_dependencies
    initialize_database
    
    # Start services
    if [[ "$MODE" == "local" ]]; then
        start_pubsub_emulator
    fi
    
    start_backend
    start_frontend
    
    # Perform health checks
    perform_health_checks
    
    # Show service information
    show_service_urls
    
    if [[ "$DETACHED" == true ]]; then
        print_success "All services started in detached mode"
        print_status "Use '$0 --status' to check service status"
        print_status "Use '$0 --logs' to view service logs"
        print_status "Use '$0 --stop' to stop all services"
    else
        print_success "All services started successfully!"
        print_status "Press Ctrl+C to stop all services"
        
        # Wait for user interrupt
        while true; do
            sleep 1
        done
    fi
}

# Run main function
main "$@"