File size: 21,241 Bytes
7c18a0a
aae749f
92fb852
1595043
37bd66e
92fb852
37bd66e
7cf67c9
0a391cf
d7b0863
1d92311
e1a482c
1595043
c3f896e
1d92311
02881ef
2079a51
7c18a0a
02881ef
a9cd437
9b14533
92fb852
 
 
9b14533
173a005
92fb852
 
 
 
 
 
d7b0863
9b14533
 
ce9fd39
 
d7b0863
9b14533
d7b0863
 
b8263fb
926e3b0
2079a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3f896e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926e3b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02881ef
7c18a0a
02881ef
90ccd1d
 
37bd66e
926e3b0
90ccd1d
7cf67c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3f896e
7cf67c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3f896e
7cf67c9
 
 
c3f896e
7cf67c9
c3f896e
 
 
7cf67c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3f896e
7cf67c9
c3f896e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cf67c9
c3f896e
 
 
 
 
 
 
7cf67c9
 
c3f896e
 
 
 
 
 
 
 
 
7cf67c9
 
 
 
c3f896e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cf67c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3f896e
7cf67c9
c3f896e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cf67c9
c3f896e
 
 
 
 
 
 
 
 
 
 
7cf67c9
c3f896e
 
 
 
 
 
 
 
 
 
 
 
 
 
7cf67c9
 
c3f896e
 
7cf67c9
 
 
 
 
 
 
 
c3f896e
 
 
 
 
 
 
 
 
 
7cf67c9
 
 
 
 
c3f896e
7cf67c9
c3f896e
7cf67c9
 
c3f896e
 
 
7cf67c9
c3f896e
 
 
 
 
 
7cf67c9
c3f896e
 
 
 
7cf67c9
 
 
c3f896e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cf67c9
 
9b14533
90ccd1d
 
 
92fb852
 
 
 
 
 
90ccd1d
 
 
 
 
 
 
 
 
 
 
 
92fb852
 
90ccd1d
 
 
 
926e3b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b14533
926e3b0
 
 
 
 
 
 
c3f896e
1595043
90ccd1d
02881ef
926e3b0
 
 
1595043
8237ec2
1d92311
7cf67c9
9b14533
d7b0863
f820b38
 
 
7cf67c9
f820b38
c3f896e
926e3b0
 
 
 
 
 
7cf67c9
 
 
 
 
 
c3f896e
 
 
7cf67c9
c3f896e
7cf67c9
c3f896e
 
 
 
 
7cf67c9
 
f820b38
7cf67c9
 
f820b38
7cf67c9
c3f896e
7cf67c9
c3f896e
 
7cf67c9
 
 
 
 
c3f896e
7cf67c9
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
import requests
import os
import json
import streamlit as st
from datetime import datetime, timedelta
import time
import uuid
import streamlit.components.v1 as components

# Page configuration
st.set_page_config(
    page_title="Chat Flow ๐Ÿ•ท",
    page_icon="๐Ÿ’ฌ",
    initial_sidebar_state="expanded"
)

# Enhanced CSS with chat history styling and BLACK NEW CHAT BUTTON
st.markdown("""
<style>
    .stApp {
        background: white;
    }
    
    .main .block-container {
        max-width: 800px;
    }
    
    #MainMenu {visibility: hidden;}
    footer {visibility: hidden;}
    header {visibility: hidden;}
    .stDeployButton {display: none;}
    
    .model-id {
        color: #28a745;
        font-family: monospace;
    }
    
    .model-attribution {
        color: #28a745;
        font-size: 0.8em;
        font-style: italic;
    }
    
    /* NEW CHAT BUTTON - Black background, white text */
    .stButton > button[kind="primary"] {
        background-color: #000000 !important;
        border-color: #000000 !important;
        color: #ffffff !important;
    }
    
    .stButton > button[kind="primary"]:hover {
        background-color: #333333 !important;
        border-color: #333333 !important;
        color: #ffffff !important;
    }
    
    .stButton > button[kind="primary"]:active {
        background-color: #1a1a1a !important;
        border-color: #1a1a1a !important;
        color: #ffffff !important;
    }
    
    .stButton > button[kind="primary"]:focus {
        background-color: #000000 !important;
        border-color: #000000 !important;
        color: #ffffff !important;
        box-shadow: 0 0 0 0.2rem rgba(0, 0, 0, 0.25) !important;
    }
    
    /* Location styling */
    .location-info {
        background: #f8f9fa;
        padding: 8px 12px;
        border-radius: 6px;
        margin: 4px 0;
        border-left: 3px solid #28a745;
    }
    
    .location-flag {
        font-size: 1.2em;
        margin-right: 6px;
    }
    
    .distance-info {
        color: #666;
        font-size: 0.85em;
        font-style: italic;
    }
    
    /* Chat history styling */
    .chat-history-item {
        padding: 8px 12px;
        margin: 4px 0;
        border-radius: 8px;
        border: 1px solid #e0e0e0;
        background: #f8f9fa;
        cursor: pointer;
        transition: all 0.2s;
    }
    
    .chat-history-item:hover {
        background: #e9ecef;
        border-color: #28a745;
    }
    
    .chat-history-item.active {
        background: #28a745;
        color: white;
        border-color: #28a745;
    }
    
    .chat-title {
        font-weight: 500;
        font-size: 0.9em;
        margin-bottom: 2px;
    }
    
    .chat-date {
        font-size: 0.75em;
        opacity: 0.7;
    }
    
    .new-chat-btn {
        width: 100%;
        margin-bottom: 16px;
    }
</style>
""", unsafe_allow_html=True)

# File to store chat history
HISTORY_FILE = "chat_history.json"
USERS_FILE = "online_users.json"
SESSIONS_FILE = "chat_sessions.json"

# ================= REAL LOCATION DETECTION FUNCTIONS =================

def get_user_location_browser():
    """Get user's real location using browser geolocation API"""
    
    # JavaScript code to get real user location
    location_js = """
    <script>
    function getUserLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(
                function(position) {
                    const location = {
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude,
                        accuracy: position.coords.accuracy,
                        timestamp: new Date().toISOString()
                    };
                    
                    // Store in sessionStorage for Streamlit to access
                    sessionStorage.setItem('userLocation', JSON.stringify(location));
                    
                    // Trigger a Streamlit rerun
                    window.parent.postMessage({
                        type: 'streamlit:componentReady'
                    }, '*');
                    
                    console.log('Location detected:', location);
                },
                function(error) {
                    console.error('Geolocation error:', error.message);
                    const errorInfo = {
                        error: error.message,
                        code: error.code,
                        timestamp: new Date().toISOString()
                    };
                    sessionStorage.setItem('locationError', JSON.stringify(errorInfo));
                },
                {
                    enableHighAccuracy: true,
                    timeout: 10000,
                    maximumAge: 300000  // 5 minutes cache
                }
            );
        } else {
            console.error('Geolocation not supported');
            sessionStorage.setItem('locationError', JSON.stringify({
                error: 'Geolocation not supported by browser',
                code: -1,
                timestamp: new Date().toISOString()
            }));
        }
    }
    
    // Auto-start location detection
    getUserLocation();
    
    // Also provide manual trigger
    window.getUserLocation = getUserLocation;
    </script>
    
    <div id="location-detector">
        <p>๐Ÿ” Detecting your real location...</p>
        <button onclick="getUserLocation()" style="
            background: #28a745; 
            color: white; 
            border: none; 
            padding: 8px 16px; 
            border-radius: 4px; 
            cursor: pointer;
        ">๐Ÿ“ Allow Location Access</button>
    </div>
    """
    
    return location_js

def reverse_geocode(lat, lon):
    """Convert coordinates to address using free APIs"""
    try:
        # Using Nominatim (OpenStreetMap) - Free and reliable
        url = f"https://nominatim.openstreetmap.org/reverse?lat={lat}&lon={lon}&format=json&addressdetails=1"
        headers = {'User-Agent': 'ChatFlow-App/1.0'}
        
        response = requests.get(url, headers=headers, timeout=10)
        
        if response.status_code == 200:
            data = response.json()
            address = data.get('address', {})
            
            location_data = {
                'latitude': lat,
                'longitude': lon,
                'city': address.get('city') or address.get('town') or address.get('village'),
                'region': address.get('state') or address.get('province'),
                'country': address.get('country'),
                'country_code': address.get('country_code', '').upper(),
                'postal': address.get('postcode'),
                'address': data.get('display_name'),
                'accuracy': 'GPS-based (Very High)',
                'timestamp': datetime.now().isoformat(),
                'api_used': 'Nominatim/OpenStreetMap',
                'source': 'Browser Geolocation'
            }
            return location_data
            
    except Exception as e:
        st.error(f"Reverse geocoding error: {e}")
    
    return None

def calculate_distance(lat1, lon1, lat2, lon2):
    """Calculate distance between two points in kilometers"""
    if not all([lat1, lon1, lat2, lon2]):
        return None
    
    try:
        from math import radians, cos, sin, asin, sqrt
        
        # Convert to radians
        lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])
        
        # Haversine formula
        dlat = lat2 - lat1
        dlon = lon2 - lon1
        a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
        c = 2 * asin(sqrt(a))
        r = 6371  # Earth's radius in kilometers
        
        distance = c * r
        return round(distance, 1)
        
    except Exception as e:
        print(f"Distance calculation error: {e}")
        return None

def get_country_flag(country_code):
    """Get emoji flag for country code"""
    flags = {
        'US': '๐Ÿ‡บ๐Ÿ‡ธ', 'UK': '๐Ÿ‡ฌ๐Ÿ‡ง', 'CA': '๐Ÿ‡จ๐Ÿ‡ฆ', 'AU': '๐Ÿ‡ฆ๐Ÿ‡บ', 'DE': '๐Ÿ‡ฉ๐Ÿ‡ช',
        'FR': '๐Ÿ‡ซ๐Ÿ‡ท', 'IT': '๐Ÿ‡ฎ๐Ÿ‡น', 'ES': '๐Ÿ‡ช๐Ÿ‡ธ', 'JP': '๐Ÿ‡ฏ๐Ÿ‡ต', 'CN': '๐Ÿ‡จ๐Ÿ‡ณ',
        'IN': '๐Ÿ‡ฎ๐Ÿ‡ณ', 'BR': '๐Ÿ‡ง๐Ÿ‡ท', 'RU': '๐Ÿ‡ท๐Ÿ‡บ', 'MX': '๐Ÿ‡ฒ๐Ÿ‡ฝ', 'NL': '๐Ÿ‡ณ๐Ÿ‡ฑ',
        'PK': '๐Ÿ‡ต๐Ÿ‡ฐ', 'BD': '๐Ÿ‡ง๐Ÿ‡ฉ', 'ID': '๐Ÿ‡ฎ๐Ÿ‡ฉ', 'NG': '๐Ÿ‡ณ๐Ÿ‡ฌ', 'TR': '๐Ÿ‡น๐Ÿ‡ท',
        'EG': '๐Ÿ‡ช๐Ÿ‡ฌ', 'ZA': '๐Ÿ‡ฟ๐Ÿ‡ฆ', 'KR': '๐Ÿ‡ฐ๐Ÿ‡ท', 'TH': '๐Ÿ‡น๐Ÿ‡ญ', 'VN': '๐Ÿ‡ป๐Ÿ‡ณ',
        'PH': '๐Ÿ‡ต๐Ÿ‡ญ', 'MY': '๐Ÿ‡ฒ๐Ÿ‡พ', 'SG': '๐Ÿ‡ธ๐Ÿ‡ฌ', 'AE': '๐Ÿ‡ฆ๐Ÿ‡ช', 'SA': '๐Ÿ‡ธ๐Ÿ‡ฆ'
    }
    return flags.get(country_code, '๐ŸŒ')

# ================= USER FUNCTIONS WITH REAL LOCATION =================

def get_user_id():
    """Get unique ID for this user session"""
    if 'user_id' not in st.session_state:
        st.session_state.user_id = str(uuid.uuid4())[:8]
    return st.session_state.user_id

def update_online_users_with_real_location():
    """Update user status with REAL browser-based location"""
    try:
        # Load current users
        users = {}
        if os.path.exists(USERS_FILE):
            with open(USERS_FILE, 'r') as f:
                users = json.load(f)

        user_id = get_user_id()
        
        # Check if we have real location data from browser
        location_data = st.session_state.get(f'real_location_{user_id}')
        
        # Update user info with real location
        users[user_id] = {
            'last_seen': datetime.now().isoformat(),
            'name': f'User-{user_id}',
            'location': location_data,
            'session_start': users.get(user_id, {}).get('session_start', datetime.now().isoformat())
        }

        # Clean up old users (not seen in 5 minutes)
        current_time = datetime.now()
        active_users = {}
        for uid, data in users.items():
            try:
                last_seen = datetime.fromisoformat(data['last_seen'])
                if current_time - last_seen < timedelta(minutes=5):
                    active_users[uid] = data
            except:
                continue

        # Save updated users
        with open(USERS_FILE, 'w') as f:
            json.dump(active_users, f, indent=2)

        return len(active_users)
        
    except Exception as e:
        st.error(f"Location tracking error: {e}")
        return 1

def show_real_location_detector():
    """Show real location detector interface"""
    st.header("๐ŸŒ Real Location Detection")
    
    user_id = get_user_id()
    
    # Check if we already have location
    if f'real_location_{user_id}' in st.session_state:
        location = st.session_state[f'real_location_{user_id}']
        if location:
            flag = get_country_flag(location.get('country_code', ''))
            st.success(f"โœ… Location detected: {flag} {location.get('city', 'Unknown')}, {location.get('country', 'Unknown')}")
            return True
    
    # Show location detector
    st.info("๐Ÿ” Click below to detect your real location using browser GPS")
    
    # Embed JavaScript for real geolocation
    location_component = get_user_location_browser()
    components.html(location_component, height=100)
    
    # Manual coordinate input as backup
    with st.expander("๐Ÿ› ๏ธ Manual Location Entry (Backup)"):
        st.write("If automatic detection doesn't work, enter your coordinates:")
        
        col1, col2 = st.columns(2)
        with col1:
            manual_lat = st.number_input("Latitude", value=0.0, format="%.6f")
        with col2:
            manual_lon = st.number_input("Longitude", value=0.0, format="%.6f")
        
        if st.button("๐ŸŽฏ Use These Coordinates"):
            if manual_lat != 0.0 and manual_lon != 0.0:
                location_data = reverse_geocode(manual_lat, manual_lon)
                if location_data:
                    st.session_state[f'real_location_{user_id}'] = location_data
                    st.success("โœ… Manual location set!")
                    st.rerun()
                else:
                    st.error("โŒ Could not resolve coordinates to address")
    
    return False

def process_browser_location():
    """Process location data from browser JavaScript"""
    user_id = get_user_id()
    
    # This would be called when JavaScript sends location data
    # In a real implementation, you'd use Streamlit's session state
    # or a custom component to receive the JavaScript data
    
    # For now, we'll simulate this with manual input
    # In production, you'd replace this with actual JavaScript communication
    pass

def show_user_locations():
    """Display all user locations with real coordinates"""
    st.header("๐ŸŒ Who's Online & Where")
    
    try:
        if not os.path.exists(USERS_FILE):
            st.info("No user data yet")
            return 0
            
        with open(USERS_FILE, 'r') as f:
            users = json.load(f)
        
        if not users:
            st.info("No active users")
            return 0
        
        # Get current user's location for distance calculation
        current_user_id = get_user_id()
        current_location = st.session_state.get(f'real_location_{current_user_id}')
        
        online_count = len(users)
        
        # Show count
        if online_count == 1:
            st.success("๐ŸŸข Just you online")
        else:
            st.success(f"๐ŸŸข {online_count} people online")
        
        st.divider()
        
        # Show each user with real location
        for user_id, data in users.items():
            location = data.get('location')
            is_current_user = (user_id == current_user_id)
            
            # User header
            if is_current_user:
                st.markdown("**๐Ÿ‘ค You**")
            else:
                st.markdown(f"**๐Ÿ‘ค {data.get('name', user_id)}**")
            
            if location and location.get('city'):
                # Get flag
                flag = get_country_flag(location.get('country_code', ''))
                
                # Location string with accuracy indicator
                accuracy = location.get('accuracy', 'Unknown')
                location_str = f"{flag} {location['city']}, {location['country']}"
                
                if 'GPS' in accuracy:
                    st.markdown(f"๐Ÿ“ **{location_str}** ๐ŸŽฏ")
                    st.caption("โœ… Real GPS location")
                else:
                    st.markdown(f"๐Ÿ“ **{location_str}**")
                    st.caption("โš ๏ธ Approximate location")
                
                # Calculate REAL distance to other users
                if not is_current_user and current_location and current_location.get('latitude'):
                    distance = calculate_distance(
                        current_location.get('latitude'),
                        current_location.get('longitude'),
                        location.get('latitude'),
                        location.get('longitude')
                    )
                    
                    if distance:
                        if distance < 1:
                            st.caption("๐Ÿ“ Very close to you! (<1km)")
                        elif distance < 10:
                            st.caption(f"๐Ÿ“ ~{distance} km from you (same city)")
                        elif distance < 100:
                            st.caption(f"๐Ÿ“ ~{distance} km from you (nearby)")
                        else:
                            st.caption(f"๐Ÿ“ ~{distance} km from you")
                
                # Show detailed location info
                with st.expander(f"Real Location Details - {user_id}"):
                    col1, col2 = st.columns(2)
                    
                    with col1:
                        st.write(f"๐ŸŽฏ **Coordinates:** {location.get('latitude', 'N/A'):.6f}, {location.get('longitude', 'N/A'):.6f}")
                        if location.get('region'):
                            st.write(f"๐Ÿ›๏ธ **Region:** {location['region']}")
                        if location.get('postal'):
                            st.write(f"๐Ÿ“ฎ **Postal:** {location['postal']}")
                    
                    with col2:
                        st.write(f"๐Ÿ” **Source:** {location.get('source', 'Unknown')}")
                        st.write(f"๐ŸŽฏ **Accuracy:** {location.get('accuracy', 'Unknown')}")
                        st.write(f"โฐ **Detected:** {location.get('timestamp', 'Unknown')[:16]}")
                
            else:
                st.caption("๐Ÿ“ Location not detected yet")
                if is_current_user:
                    st.caption("๐Ÿ‘† Use the location detector above")
            
            # Show session info
            try:
                session_start = datetime.fromisoformat(data['session_start'])
                duration = datetime.now() - session_start
                minutes = int(duration.total_seconds() / 60)
                st.caption(f"๐Ÿ• Online for {minutes} minutes")
            except:
                st.caption("๐Ÿ• Session time unknown")
            
            st.divider()
        
        return online_count
        
    except Exception as e:
        st.error(f"Error showing locations: {e}")
        return 0

# ================= CHAT FUNCTIONS (unchanged) =================
# [All the existing chat functions remain the same]

def load_chat_history():
    """Load chat history from file"""
    try:
        if os.path.exists(HISTORY_FILE):
            with open(HISTORY_FILE, 'r', encoding='utf-8') as f:
                return json.load(f)
    except Exception as e:
        st.error(f"Error loading chat history: {e}")
    return []

def save_chat_history(messages):
    """Save chat history to file"""
    try:
        with open(HISTORY_FILE, 'w', encoding='utf-8') as f:
            json.dump(messages, f, ensure_ascii=False, indent=2)
    except Exception as e:
        st.error(f"Error saving chat history: {e}")

def clear_chat_history():
    """Clear chat history file"""
    try:
        if os.path.exists(HISTORY_FILE):
            os.remove(HISTORY_FILE)
        st.session_state.messages = []
    except Exception as e:
        st.error(f"Error clearing chat history: {e}")

def load_chat_sessions():
    """Load all chat sessions"""
    try:
        if os.path.exists(SESSIONS_FILE):
            with open(SESSIONS_FILE, 'r', encoding='utf-8') as f:
                return json.load(f)
    except Exception as e:
        st.error(f"Error loading chat sessions: {e}")
    return {}

def save_chat_sessions(sessions):
    """Save chat sessions to file"""
    try:
        with open(SESSIONS_FILE, 'w', encoding='utf-8') as f:
            json.dump(sessions, f, ensure_ascii=False, indent=2)
    except Exception as e:
        st.error(f"Error saving chat sessions: {e}")

def get_session_id():
    """Get or create session ID"""
    if 'session_id' not in st.session_state:
        st.session_state.session_id = str(uuid.uuid4())
    return st.session_state.session_id

def start_new_chat():
    """Start a new chat session"""
    if st.session_state.messages:
        save_current_session()
    st.session_state.messages = []
    st.session_state.session_id = str(uuid.uuid4())

# Initialize session state
if "messages" not in st.session_state:
    st.session_state.messages = load_chat_history()

if "session_id" not in st.session_state:
    st.session_state.session_id = str(uuid.uuid4())

# Get API key
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")

# ================= MAIN APP WITH REAL LOCATION =================

# Header
st.title("Chat Flow ๐Ÿ•ท")
st.caption("10 powerful Models, one simple chat.")

# Sidebar with REAL LOCATION TRACKING
with st.sidebar:
    # New Chat Button (BLACK)
    if st.button("โž• New Chat", use_container_width=True, type="primary"):
        start_new_chat()
        st.rerun()
    
    st.divider()
    
    # REAL LOCATION DETECTION
    show_real_location_detector()
    
    st.divider()
    
    # Show online users with real locations
    online_count = show_user_locations()
    
    # Update location tracking
    update_online_users_with_real_location()
    
    # Quick refresh
    if st.button("๐Ÿ”„ Refresh Locations", use_container_width=True):
        st.rerun()
    
    st.divider()
    
    # Rest of the sidebar (chat sessions, settings, etc.) remains the same
    # [All existing sidebar code continues here...]

# Main chat area remains the same
# [All existing chat code continues here...]

# Location Status Footer - Updated for real location
user_id = get_user_id()
location = st.session_state.get(f'real_location_{user_id}')
if location and location.get('city'):
    flag = get_country_flag(location.get('country_code', ''))
    accuracy = location.get('accuracy', 'Unknown')
    if 'GPS' in accuracy:
        st.caption(f"๐Ÿ“ Your real location: {flag} {location['city']}, {location['country']} | โœ… GPS-accurate")
    else:
        st.caption(f"๐Ÿ“ Your location: {flag} {location['city']}, {location['country']} | Accuracy: {accuracy}")
else:
    st.caption("๐Ÿ“ Real location not detected yet - click 'Allow Location Access' above")