File size: 37,162 Bytes
acb9a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4d7c67
acb9a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Header, HTTPException, Form
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
import torch
import torch.nn.functional as F
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import declarative_base, sessionmaker
import uuid
import random

# --- 1. Connect to the Upgraded Business Database ---
engine = create_engine("sqlite:///./phishguard.db")
Base = declarative_base()

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    api_key = Column(String, unique=True)
    name = Column(String)
    email = Column(String, unique=True)
    password = Column(String)
    phone = Column(String)
    address = Column(String)
    city = Column(String)    
    scan_count = Column(Integer, default=0)
    tier = Column(String, default="Free")

Base.metadata.create_all(bind=engine)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# --- 2. Start the Server & Mount Logo ---
app = FastAPI(title="PhishGuard AI")
app.mount("/static", StaticFiles(directory="static"), name="static")

# --- 3. Wake up the Smarter BERT Brain ---
print("[*] Waking up the Deep Learning BERT Brain...")
model_path = 'distilbert-base-uncased'
tokenizer = DistilBertTokenizerFast.from_pretrained(model_path)
model = DistilBertForSequenceClassification.from_pretrained(model_path)

class IncomingEmail(BaseModel):
    text: str
    sender_info: str = "Unknown"

class IncomingImage(BaseModel):
    image_data: str  # We receive the image as a Base64 text string!

# --- 4. THE SUPER APP DASHBOARD (PRIVATE CUSTOMER VIEW) ---
def get_dashboard_html(user: User, alert_msg: str = ""):
    script_alert = f"alert('{alert_msg}');" if alert_msg else ""
    return f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>PhishGuard | Dashboard</title>
        <style>
        body {{ background-color: #0b1120; color: white; font-family: 'Segoe UI', sans-serif; text-align: center; padding-top: 30px; margin: 0; padding-bottom: 50px; }}
        .box {{ max-width: 700px; margin: auto; padding: 30px; background: #1e293b; border-radius: 10px; border: 1px solid #334155; box-shadow: 0 10px 25px rgba(0,0,0,0.5); }}
        .key {{ color: #10b981; font-size: 1.2em; font-family: monospace; padding: 15px; background: #0f172a; border-radius: 8px; margin: 10px 0; border: 1px dashed #10b981; word-break: break-all; }}
        .premium {{ border-top: 1px solid #334155; margin-top: 20px; padding-top: 20px; }}
        .btn-upgrade {{ background: #10b981; padding: 12px 24px; color: white; border-radius: 5px; font-weight: bold; border: none; cursor: pointer; transition: 0.3s; }}
        .btn-upgrade:hover {{ background: #059669; }}
        .home-link {{ color: #94a3b8; text-decoration: none; display: inline-block; margin-top: 20px; transition: 0.3s; }}
        .home-link:hover {{ color: #38bdf8; text-decoration: underline; }}
        
        .scanner-box {{ background: #0f172a; border: 1px solid #38bdf8; border-radius: 8px; padding: 20px; margin-top: 30px; text-align: left; }}
        .scanner-box h3 {{ color: #38bdf8; margin-top: 0; text-align: center; }}
        input, textarea {{ width: 95%; padding: 10px; margin: 10px 0; border-radius: 6px; border: 1px solid #475569; background: #1e293b; color: white; font-family: sans-serif; }}
        textarea {{ height: 100px; resize: none; }}
        .btn-scan {{ width: 100%; background: #38bdf8; color: #0f172a; border: none; padding: 12px; font-size: 16px; border-radius: 6px; cursor: pointer; font-weight: bold; transition: 0.3s; }}
        .btn-scan:hover {{ background: #0284c7; color: white; }}
        #scanResult {{ margin-top: 15px; padding: 15px; border-radius: 6px; display: none; font-weight: bold; text-align: center; }}

        /* Chat UI Updates */
        .chat-btn {{ position: fixed; bottom: 30px; right: 30px; background: #38bdf8; color: #0b1120; padding: 15px 25px; border-radius: 50px; cursor: pointer; font-weight: bold; font-size: 16px; box-shadow: 0 5px 15px rgba(56, 189, 248, 0.4); z-index: 1000; transition: 0.3s; border: 2px solid #0284c7; }}
        .chat-btn:hover {{ transform: scale(1.1); box-shadow: 0 8px 20px rgba(56, 189, 248, 0.6); }}
        
        .chat-window {{ position: fixed; bottom: 90px; right: 30px; width: 350px; background: #1e293b; border: 1px solid #38bdf8; border-radius: 12px; display: none; flex-direction: column; overflow: hidden; box-shadow: 0 10px 30px rgba(0,0,0,0.8); z-index: 1000; }}
        .chat-header {{ background: #0f172a; padding: 15px; color: #38bdf8; font-weight: bold; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #334155; }}
        .chat-body {{ height: 320px; padding: 15px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; font-size: 0.95em; text-align: left; }}
        .chat-footer {{ display: flex; padding: 12px; background: #0f172a; border-top: 1px solid #334155; align-items: center; }}
        .chat-footer input[type="text"] {{ flex: 1; padding: 10px; border-radius: 6px; border: 1px solid #475569; background: #1e293b; color: white; margin-right: 5px; width: 50%; }}
        
        .btn-attach {{ background: transparent; border: none; font-size: 1.5em; cursor: pointer; color: #94a3b8; transition: 0.3s; padding: 0 5px; }}
        .btn-attach:hover {{ color: #38bdf8; transform: scale(1.1); }}
        
        .chat-footer button.send-btn {{ background: #38bdf8; color: #0b1120; border: none; padding: 10px 15px; border-radius: 6px; font-weight: bold; cursor: pointer; transition: 0.3s; margin-left: 5px; }}
        .chat-footer button.send-btn:hover {{ background: #0284c7; color: white; }}
        
        .bot-msg {{ background: #334155; padding: 12px; border-radius: 12px 12px 12px 0; align-self: flex-start; max-width: 85%; line-height: 1.4; border-left: 3px solid #38bdf8; }}
        .user-msg {{ background: #0ea5e9; color: white; padding: 12px; border-radius: 12px 12px 0 12px; align-self: flex-end; max-width: 85%; word-wrap: break-word; }}
        .chat-img-preview {{ max-width: 100%; border-radius: 8px; border: 2px solid #0ea5e9; margin-top: 5px; }}
        .chat-body::-webkit-scrollbar {{ width: 6px; }}
        .chat-body::-webkit-scrollbar-thumb {{ background: #475569; border-radius: 3px; }}
        </style>
    </head>
    <body>
        <div class="box">
            <h2 style="color:#38bdf8;">Welcome back, {user.name}!</h2>
            <p>Your current plan is: <b><span style="color: #38bdf8;">{user.tier.upper()}</span></b></p>
            <p>Your Private API Key (For Gmail Integration):</p>
            <div class="key">{user.api_key}</div>
            
            <div class="scanner-box">
                <h3>Live Web Scanner (Email Threats)</h3>
                <p style="color: #94a3b8; font-size: 0.9em; text-align: center;">Don't want to install the Gmail Add-on? Test emails right here!</p>
                <input type="text" id="senderEmail" placeholder="Sender Email (e.g., support@spotify.com)">
                <textarea id="emailBody" placeholder="Paste the suspicious email text here..."></textarea>
                <button onclick="runWebScan()" class="btn-scan">Scan This Email</button>
                <div id="scanResult"></div>
            </div>

            <div class="premium">
                <h3 style="color: white;">Upgrade to Premium</h3>
                <p style="color: #94a3b8;">Unlock unlimited scanning and priority threat detection.</p>
                <button onclick="goToCheckout()" class="btn-upgrade">Pay $9.99/mo (Secure Checkout)</button>
            </div>
            <a href="/" class="home-link">← Log Out & Return to Home</a>
        </div>

        <div class="chat-btn" onclick="toggleChat()">πŸ€– Sentinel AI</div>

        <div class="chat-window" id="chatWindow">
            <div class="chat-header">
                <span>πŸ›‘οΈ Sentinel AI Bot</span>
                <span style="cursor:pointer; font-size: 1.2em;" onclick="toggleChat()">βœ–</span>
            </div>
            <div class="chat-body" id="chatBody">
                <div class="bot-msg">Hello {user.name}! I am your Mobile Sentinel. Paste suspicious text or <b>upload a photo</b> (like a fake QR code or scam screenshot) for deep analysis.</div>
            </div>
            <div class="chat-footer">
                <input type="file" id="imageUpload" accept="image/*" style="display: none;" onchange="handleImageUpload(this)">
                <button class="btn-attach" onclick="document.getElementById('imageUpload').click()">πŸ“Ž</button>
                
                <input type="text" id="chatInput" placeholder="Message or link..." onkeypress="handleKeyPress(event)">
                <button class="send-btn" onclick="sendChatMessage()">Send</button>
            </div>
        </div>
        
        <script>
            {script_alert}
            function goToCheckout() {{ window.location.href = "/checkout_page?api_key={user.api_key}"; }}

            async function runWebScan() {{
                const sender = document.getElementById('senderEmail').value;
                const text = document.getElementById('emailBody').value;
                const resultDiv = document.getElementById('scanResult');
                const key = "{user.api_key}";

                if(!sender || !text) {{ alert("Please enter both a sender email and the email text."); return; }}

                resultDiv.style.display = "block";
                resultDiv.style.backgroundColor = "#334155";
                resultDiv.style.color = "white";
                resultDiv.innerText = "Scanning with 66-Million Parameter AI...";

                try {{
                    const response = await fetch('/scan', {{
                        method: 'POST',
                        headers: {{ 'Content-Type': 'application/json', 'x-api-key': key }},
                        body: JSON.stringify({{ text: text, sender_info: sender }})
                    }});
                    const data = await response.json();
                    if (data.status === "THREAT DETECTED") {{
                        resultDiv.style.backgroundColor = "#7f1d1d"; resultDiv.style.color = "#fca5a5";
                        resultDiv.innerHTML = `⚠️ THREAT DETECTED: Phishing<br><span style="font-size:0.8em; font-weight:normal;">Traced IP: ${{data.location}}</span>`;
                    }} else {{
                        resultDiv.style.backgroundColor = "#14532d"; resultDiv.style.color = "#86efac";
                        resultDiv.innerHTML = `βœ… SAFE: Legitimate<br><span style="font-size:0.8em; font-weight:normal;">Network Source: ${{data.location}}</span>`;
                    }}
                }} catch (error) {{ resultDiv.innerText = "Error connecting to AI Brain."; }}
            }}

            function toggleChat() {{
                const chat = document.getElementById('chatWindow');
                chat.style.display = chat.style.display === 'flex' ? 'none' : 'flex';
            }}

            function handleKeyPress(e) {{
                if (e.key === 'Enter') sendChatMessage();
            }}

            // --- TEXT CHAT LOGIC ---
            async function sendChatMessage() {{
                const inputField = document.getElementById('chatInput');
                const text = inputField.value.trim();
                if (!text) return;

                const chatBody = document.getElementById('chatBody');
                const key = "{user.api_key}";

                chatBody.innerHTML += `<div class="user-msg">${{text}}</div>`;
                inputField.value = '';
                chatBody.scrollTop = chatBody.scrollHeight;

                const typingId = "typing-" + Date.now();
                chatBody.innerHTML += `<div class="bot-msg" id="${{typingId}}" style="color: #94a3b8;"><em>Analyzing network signatures...</em></div>`;
                chatBody.scrollTop = chatBody.scrollHeight;

                try {{
                    const response = await fetch('/scan', {{
                        method: 'POST',
                        headers: {{ 'Content-Type': 'application/json', 'x-api-key': key }},
                        body: JSON.stringify({{ text: text, sender_info: "WhatsApp/SMS Sentinel" }})
                    }});
                    const data = await response.json();
                    document.getElementById(typingId).remove();

                    let reply = "";
                    if (data.status === "THREAT DETECTED") {{
                        reply = `🚨 <b>CRITICAL WARNING:</b> My AI detected malicious signatures in this message. <b>DO NOT click any links.</b><br><br><span style="font-size:0.85em; color:#cbd5e1;">(Scans Used: ${{data.scans_used}})</span>`;
                        chatBody.innerHTML += `<div class="bot-msg" style="border-left: 3px solid #ef4444;">${{reply}}</div>`;
                    }} else {{
                        reply = `βœ… <b>SAFE:</b> I have scanned the text. No known scam or phishing patterns were detected.<br><br><span style="font-size:0.85em; color:#cbd5e1;">(Scans Used: ${{data.scans_used}})</span>`;
                        chatBody.innerHTML += `<div class="bot-msg" style="border-left: 3px solid #10b981;">${{reply}}</div>`;
                    }}
                    chatBody.scrollTop = chatBody.scrollHeight;

                }} catch (error) {{
                    document.getElementById(typingId).remove();
                    chatBody.innerHTML += `<div class="bot-msg" style="border-left: 3px solid #ef4444;">Error: Cannot connect to AI Engine.</div>`;
                }}
            }}

            // --- SMART DYNAMIC IMAGE UPLOAD LOGIC ---
            function handleImageUpload(input) {{
                if (input.files && input.files[0]) {{
                    const reader = new FileReader();
                    reader.onload = async function(e) {{
                        const base64Image = e.target.result;
                        const chatBody = document.getElementById('chatBody');
                        const key = "{user.api_key}";

                        // Display user's image in chat
                        chatBody.innerHTML += `<div class="user-msg">Attached Photo:<br><img src="${{base64Image}}" class="chat-img-preview"></div>`;
                        chatBody.scrollTop = chatBody.scrollHeight;

                        // Add "Scanning Pixels" indicator
                        const typingId = "img-scan-" + Date.now();
                        chatBody.innerHTML += `<div class="bot-msg" id="${{typingId}}" style="color: #94a3b8;"><em>πŸ‘οΈ Scanning pixels for embedded QR codes and hidden links...</em></div>`;
                        chatBody.scrollTop = chatBody.scrollHeight;

                        try {{
                            // Send to new Computer Vision Endpoint
                            const response = await fetch('/scan_image', {{
                                method: 'POST',
                                headers: {{ 'Content-Type': 'application/json', 'x-api-key': key }},
                                body: JSON.stringify({{ image_data: base64Image }})
                            }});
                            const data = await response.json();
                            document.getElementById(typingId).remove();

                            // DYNAMIC UI UPDATE BASED ON SERVER RESPONSE
                            let reply = "";
                            if (data.status === "THREAT DETECTED") {{
                                reply = `🚨 <b>MALICIOUS IMAGE:</b> ${{data.details}}<br><br><span style="font-size:0.85em; color:#cbd5e1;">(Scans Used: ${{data.scans_used}})</span>`;
                                chatBody.innerHTML += `<div class="bot-msg" style="border-left: 3px solid #ef4444;">${{reply}}</div>`;
                            }} else {{
                                reply = `βœ… <b>SAFE IMAGE:</b> ${{data.details}}<br><br><span style="font-size:0.85em; color:#cbd5e1;">(Scans Used: ${{data.scans_used}})</span>`;
                                chatBody.innerHTML += `<div class="bot-msg" style="border-left: 3px solid #10b981;">${{reply}}</div>`;
                            }}
                            chatBody.scrollTop = chatBody.scrollHeight;

                        }} catch (error) {{
                            document.getElementById(typingId).remove();
                            chatBody.innerHTML += `<div class="bot-msg" style="border-left: 3px solid #ef4444;">Error: Computer Vision engine failed.</div>`;
                        }}
                    }};
                    reader.readAsDataURL(input.files[0]);
                }}
            }}
        </script>
    </body>
    </html>
    """

# --- 5. THE PUBLIC FRONTEND (WITH TEASER CHATBOT) ---
@app.get("/", response_class=HTMLResponse)
def serve_landing_page():
    return """
    <!DOCTYPE html>
    <html>
    <head>
        <title>PhishGuard AI | Home</title>
        <style>
            body, html { margin: 0; padding: 0; height: 100%; width: 100%; overflow: hidden; background-color: #0b1120; font-family: 'Segoe UI', sans-serif; }
            #particles-js { position: absolute; width: 100%; height: 100%; z-index: 1; }
            .main-content { position: relative; z-index: 2; height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; pointer-events: none; }
            .content-box { pointer-events: auto; max-width: 800px; padding: 20px; }
            .logo { width: 280px; animation: float 3s ease-in-out infinite; margin-bottom: 10px; }
            @keyframes float { 0% { transform: translateY(0px); } 50% { transform: translateY(-15px); } 100% { transform: translateY(0px); } }
            h1 { color: #38bdf8; font-size: 3.5em; margin-bottom: 5px; letter-spacing: 1px; text-shadow: 0 0 15px rgba(56, 189, 248, 0.5); }
            h3 { color: #10b981; font-size: 1.5em; font-weight: normal; margin-top: 0; margin-bottom: 40px; }
            .btn { background: linear-gradient(90deg, #0284c7, #38bdf8); color: white; border: none; padding: 15px 40px; font-size: 20px; border-radius: 30px; cursor: pointer; font-weight: bold; transition: 0.3s; box-shadow: 0 4px 15px rgba(56, 189, 248, 0.4); text-decoration: none; display: inline-block; }
            .btn:hover { transform: scale(1.05); box-shadow: 0 6px 20px rgba(56, 189, 248, 0.6); }
            .login-link { display: block; margin-top: 25px; color: #94a3b8; text-decoration: none; font-size: 1em; transition: 0.3s; }
            .login-link:hover { color: #38bdf8; text-decoration: underline; }
            .menu-icon { position: absolute; top: 25px; right: 35px; font-size: 35px; cursor: pointer; color: #38bdf8; z-index: 3; transition: 0.3s; pointer-events: auto;}
            .menu-icon:hover { color: #10b981; transform: scale(1.1); }
            .sidenav { height: 100%; width: 0; position: fixed; z-index: 4; top: 0; right: 0; background-color: #1e293b; overflow-x: hidden; transition: 0.4s cubic-bezier(0.4, 0, 0.2, 1); padding-top: 80px; box-shadow: -5px 0 25px rgba(0,0,0,0.6); border-left: 1px solid #334155; }
            .sidenav a { padding: 15px 30px; text-decoration: none; font-size: 22px; color: #cbd5e1; display: block; transition: 0.3s; text-align: left; border-bottom: 1px solid #334155; }
            .sidenav a:hover { color: #38bdf8; background-color: #0f172a; padding-left: 40px; }
            .sidenav .closebtn { position: absolute; top: 10px; right: 25px; font-size: 40px; margin-left: 50px; border: none; }

            .chat-btn { position: fixed; bottom: 30px; right: 30px; background: #38bdf8; color: #0b1120; padding: 15px 25px; border-radius: 50px; cursor: pointer; font-weight: bold; font-size: 16px; box-shadow: 0 5px 15px rgba(56, 189, 248, 0.4); z-index: 1000; transition: 0.3s; border: 2px solid #0284c7; pointer-events: auto;}
            .chat-btn:hover { transform: scale(1.1); box-shadow: 0 8px 20px rgba(56, 189, 248, 0.6); }
            .chat-window { position: fixed; bottom: 90px; right: 30px; width: 350px; background: #1e293b; border: 1px solid #38bdf8; border-radius: 12px; display: none; flex-direction: column; overflow: hidden; box-shadow: 0 10px 30px rgba(0,0,0,0.8); z-index: 1000; pointer-events: auto;}
            .chat-header { background: #0f172a; padding: 15px; color: #38bdf8; font-weight: bold; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #334155; }
            .chat-body { height: 250px; padding: 15px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; font-size: 0.95em; text-align: left; }
            .chat-footer { display: flex; padding: 12px; background: #0f172a; border-top: 1px solid #334155; }
            .chat-footer input { flex: 1; padding: 10px; border-radius: 6px; border: 1px solid #475569; background: #1e293b; color: white; margin-right: 10px; width: 60%; }
            .chat-footer button { background: #475569; color: #94a3b8; border: none; padding: 10px 15px; border-radius: 6px; font-weight: bold; cursor: not-allowed; }
            .bot-msg { background: #334155; padding: 12px; border-radius: 12px 12px 12px 0; align-self: flex-start; max-width: 85%; line-height: 1.4; border-left: 3px solid #38bdf8; color: white;}
        </style>
    </head>
    <body>
        <div id="particles-js"></div>
        <span class="menu-icon" onclick="openNav()">&#9776;</span>
        <div id="mySidenav" class="sidenav">
            <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
            <a href="/">🏠 Home</a>
            <a href="/login_page">πŸ‘€ User Details</a>
            <a href="mailto:support@phishguard.ai">βœ‰οΈ Contact Us</a>
        </div>
        <div class="main-content">
            <div class="content-box">
                <img src="/static/logo.png" class="logo" alt="PhishGuard AI Logo">
                <h1>PhishGuard AI</h1>
                <h3>Complete email protection. Stay safe from every threat!</h3>
                <a href="/register_page" class="btn">Create Your Account</a>
                <a href="/login_page" class="login-link">Already have an account? Log in here</a>
            </div>
        </div>

        <div class="chat-btn" onclick="toggleChat()">πŸ€– Sentinel AI</div>
        <div class="chat-window" id="chatWindow">
            <div class="chat-header">
                <span>πŸ›‘οΈ Sentinel AI Bot</span>
                <span style="cursor:pointer; font-size: 1.2em;" onclick="toggleChat()">βœ–</span>
            </div>
            <div class="chat-body" id="chatBody">
                <div class="bot-msg">Hello! I am Sentinel AI, your personal threat scanner. πŸ”’<br><br>Please <a href="/login_page" style="color:#38bdf8;">Log In</a> or <a href="/register_page" style="color:#38bdf8;">Create an Account</a> to activate my features!</div>
            </div>
            <div class="chat-footer">
                <input type="text" placeholder="Please log in first..." disabled>
                <button disabled>Send</button>
            </div>
        </div>

        <script>
            function toggleChat() { document.getElementById('chatWindow').style.display = document.getElementById('chatWindow').style.display === 'flex' ? 'none' : 'flex'; }
            function openNav() { document.getElementById("mySidenav").style.width = "300px"; }
            function closeNav() { document.getElementById("mySidenav").style.width = "0"; }
        </script>
        <script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
        <script>
            particlesJS("particles-js", {
              "particles": { "number": { "value": 100 }, "color": { "value": "#ffffff" }, "shape": { "type": "circle" }, "opacity": { "value": 0.3 }, "size": { "value": 3, "random": true }, "line_linked": { "enable": true, "distance": 150, "color": "#38bdf8", "opacity": 0.4, "width": 1 }, "move": { "enable": true, "speed": 2 } },
              "interactivity": { "detect_on": "canvas", "events": { "onhover": { "enable": true, "mode": "grab" }, "onclick": { "enable": true, "mode": "push" } }, "modes": { "grab": { "distance": 140, "line_linked": { "opacity": 1 } } } },
              "retina_detect": true
            });
        </script>
    </body>
    </html>
    """

# --- 6. THE LOGIN & REGISTRATION PAGES ---
@app.get("/login_page", response_class=HTMLResponse)
def serve_login():
    return """
    <!DOCTYPE html>
    <html>
    <head><style>
        body { background-color: #0b1120; color: white; font-family: 'Segoe UI', sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
        .form-box { background: #1e293b; padding: 40px; border-radius: 15px; width: 400px; box-shadow: 0 10px 25px rgba(0,0,0,0.5); border: 1px solid #334155; text-align: center; }
        h2 { color: #38bdf8; margin-bottom: 20px; }
        input { width: 90%; padding: 12px; margin: 10px 0; border-radius: 6px; border: 1px solid #475569; background: #0f172a; color: white; }
        .btn { width: 100%; background: #10b981; color: white; border: none; padding: 12px; font-size: 16px; border-radius: 6px; cursor: pointer; font-weight: bold; margin-top: 15px; transition: 0.3s; }
        .btn:hover { background: #059669; }
        .home-link { color: #94a3b8; text-decoration: none; display: block; margin-top: 20px; font-size: 0.9em; transition: 0.3s; }
        .home-link:hover { color: #38bdf8; }
    </style></head>
    <body>
        <div class="form-box">
            <h2>Log In to Dashboard</h2>
            <form action="/login" method="post">
                <input type="email" name="email" placeholder="Email Address" required>
                <input type="password" name="password" placeholder="Password" required>
                <button type="submit" class="btn">Access Account</button>
            </form>
            <a href="/" class="home-link">← Back to Home</a>
        </div>
    </body>
    </html>
    """

@app.get("/register_page", response_class=HTMLResponse)
def serve_registration():
    return """
    <!DOCTYPE html>
    <html>
    <head><style>
        body { background-color: #0b1120; color: white; font-family: 'Segoe UI', sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
        .form-box { background: #1e293b; padding: 40px; border-radius: 15px; width: 400px; box-shadow: 0 10px 25px rgba(0,0,0,0.5); border: 1px solid #334155; text-align: center; }
        h2 { color: #38bdf8; margin-bottom: 20px; }
        input { width: 90%; padding: 12px; margin: 8px 0; border-radius: 6px; border: 1px solid #475569; background: #0f172a; color: white; }
        .btn { width: 100%; background: #10b981; color: white; border: none; padding: 12px; font-size: 16px; border-radius: 6px; cursor: pointer; font-weight: bold; margin-top: 15px; transition: 0.3s; }
        .btn:hover { background: #059669; }
        .home-link { color: #94a3b8; text-decoration: none; display: block; margin-top: 20px; font-size: 0.9em; transition: 0.3s; }
        .home-link:hover { color: #38bdf8; }
    </style></head>
    <body>
        <div class="form-box">
            <h2>Customer Registration</h2>
            <form action="/register" method="post">
                <input type="text" name="name" placeholder="Full Name" required>
                <input type="email" name="email" placeholder="Email Address" required>
                <input type="password" name="password" placeholder="Create Password" required>
                <input type="text" name="phone" placeholder="Phone Number" required>
                <input type="text" name="address" placeholder="Company / Street Address" required>
                <input type="text" name="city" placeholder="City" required>
                <button type="submit" class="btn">Complete Registration</button>
            </form>
            <a href="/" class="home-link">← Back to Home</a>
        </div>
    </body>
    </html>
    """

# --- 7. REGISTRATION & LOGIN PROCESSING ---
@app.post("/register", response_class=HTMLResponse)
def process_registration(name: str=Form(...), email: str=Form(...), password: str=Form(...), phone: str=Form(...), address: str=Form(...), city: str=Form(...)):
    db = SessionLocal()
    existing_user = db.query(User).filter(User.email == email).first()
    if existing_user:
        db.close()
        return "<h2>Error: Email already registered. Please log in.</h2><br><a href='/login_page'>Go to Login</a>"

    new_key = "pg_" + uuid.uuid4().hex[:16] 
    new_user = User(api_key=new_key, name=name, email=email, password=password, phone=phone, address=address, city=city, scan_count=0, tier="Free")
    db.add(new_user)
    db.commit()
    db.refresh(new_user)
    html = get_dashboard_html(new_user)
    db.close()
    return html

@app.post("/login", response_class=HTMLResponse)
def process_login(email: str = Form(...), password: str = Form(...)):
    db = SessionLocal()
    user = db.query(User).filter(User.email == email, User.password == password).first()
    if not user:
        db.close()
        return "<h2>Error: Invalid email or password. Please try again.</h2><br><a href='/login_page'>Go back</a>"
    
    html = get_dashboard_html(user)
    db.close()
    return html

# --- 8. THE E-COMMERCE CHECKOUT FLOW ---
@app.get("/checkout_page", response_class=HTMLResponse)
def serve_checkout(api_key: str):
    return f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>PhishGuard | Secure Checkout</title>
        <style>
            body {{ background-color: #0b1120; color: white; font-family: 'Segoe UI', sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }}
            .checkout-box {{ background: #1e293b; padding: 40px; border-radius: 15px; width: 450px; box-shadow: 0 10px 25px rgba(0,0,0,0.5); border: 1px solid #334155; }}
            h2 {{ color: #38bdf8; text-align: center; margin-bottom: 5px; }}
            .price {{ text-align: center; color: #10b981; font-size: 1.5em; margin-top: 0; margin-bottom: 25px; }}
            label {{ font-size: 0.9em; color: #94a3b8; display: block; margin-top: 15px; margin-bottom: 5px; }}
            input {{ width: 92%; padding: 12px; border-radius: 6px; border: 1px solid #475569; background: #0f172a; color: white; }}
            .flex-row {{ display: flex; justify-content: space-between; }}
            .flex-row input {{ width: 85%; }}
            .btn-pay {{ width: 100%; background: #10b981; color: white; border: none; padding: 14px; font-size: 18px; border-radius: 6px; cursor: pointer; font-weight: bold; margin-top: 30px; transition: 0.3s; }}
            .btn-pay:hover {{ background: #059669; box-shadow: 0 0 15px rgba(16, 185, 129, 0.4); }}
            .secure-badge {{ text-align: center; color: #64748b; font-size: 0.8em; margin-top: 20px; }}
            .cancel-link {{ display: block; text-align: center; color: #94a3b8; text-decoration: none; margin-top: 15px; transition: 0.3s; }}
            .cancel-link:hover {{ color: #ef4444; }}
        </style>
    </head>
    <body>
        <div class="checkout-box">
            <h2>Upgrade to Premium</h2>
            <p class="price">$9.99 / month</p>
            <form action="/process_payment" method="post">
                <input type="hidden" name="api_key" value="{api_key}">
                
                <label>Name on Card</label>
                <input type="text" name="card_name" placeholder="E.g. Sudipta Khutia" required>
                
                <label>Card Number</label>
                <input type="text" name="card_number" placeholder="β€’β€’β€’β€’ β€’β€’β€’β€’ β€’β€’β€’β€’ 4242" required maxlength="19">
                
                <div class="flex-row">
                    <div style="width: 48%;">
                        <label>Expiration</label>
                        <input type="text" name="exp_date" placeholder="MM/YY" required maxlength="5">
                    </div>
                    <div style="width: 48%;">
                        <label>CVC / CVV</label>
                        <input type="password" name="cvc" placeholder="β€’β€’β€’" required maxlength="4">
                    </div>
                </div>
                
                <button type="submit" class="btn-pay">πŸ”’ Process Secure Payment</button>
            </form>
            <a href="/" class="cancel-link">Cancel and Return</a>
            <p class="secure-badge">πŸ’³ 256-bit SSL Encrypted Mock Checkout</p>
        </div>
    </body>
    </html>
    """

@app.post("/process_payment", response_class=HTMLResponse)
def process_payment(api_key: str = Form(...), card_name: str = Form(...), card_number: str = Form(...), exp_date: str = Form(...), cvc: str = Form(...)):
    db = SessionLocal()
    user = db.query(User).filter(User.api_key == api_key).first()
    if not user:
        db.close()
        return "<h2>Error: Account not found.</h2><br><a href='/'>Go Home</a>"
        
    user.tier = "Premium"
    db.commit()
    
    html = get_dashboard_html(user, alert_msg=f"Payment Successful, {card_name}! Your account is now PREMIUM.")
    db.close()
    return html

# --- 9. THE HYBRID SCANNING ENGINE (SMART AI + WHITELIST) ---
@app.post("/scan")
def scan_email(email: IncomingEmail, x_api_key: str = Header(None)):
    db = SessionLocal()
    if not x_api_key:
        db.close()
        raise HTTPException(status_code=401, detail="Missing API Key")
    user = db.query(User).filter(User.api_key == x_api_key).first()
    if not user:
        db.close()
        raise HTTPException(status_code=401, detail="Invalid API Key")

    sender = email.sender_info.lower()
    text_lower = email.text.lower()
    
    trusted_domains = ["google.com", "linkedin.com", "github.com", "spotify", "haldia", "hit", "microsoft.com", "n8n"]
    is_trusted = any(domain in sender for domain in trusted_domains)

    safe_sms_patterns = ["sbi", "vouchers", "perks", "otp", "account balance", "dear customer", "shakti", "amazon", "flipkart", "jio", "offer", "hotstar", "cricket"]
    if any(pattern in text_lower for pattern in safe_sms_patterns):
        is_trusted = True 

    inputs = tokenizer(email.text, return_tensors="pt", truncation=True, padding=True, max_length=128)
    with torch.no_grad():
        outputs = model(**inputs)
        probs = F.softmax(outputs.logits, dim=-1)
        phishing_probability = probs[0][1].item()
    
    is_phishing = True if (phishing_probability > 0.85 and not is_trusted) else False
    
    user.scan_count += 1
    db.commit()
    scans = user.scan_count
    tier = user.tier
    db.close()

    if "spotify" in sender: detected_location = "193.182.8.14 (Stockholm, Sweden)"
    elif "huggingface" in sender: detected_location = "142.250.190.46 (New York, USA)"
    elif "bank" in sender or "paypal" in sender or is_phishing: detected_location = f"194.58.112.44 (Moscow, Russia) - HIGH RISK"
    else: detected_location = f"104.28.{random.randint(1,250)}.{random.randint(1,250)} (San Francisco, USA)"

    if is_phishing: 
        return {"status": "THREAT DETECTED", "verdict": "Phishing", "engine": "PhishGuard Hybrid AI", "scans_used": scans, "tier": tier, "location": detected_location}
    else: 
        return {"status": "SAFE", "verdict": "Legitimate", "engine": "PhishGuard Hybrid AI", "scans_used": scans, "tier": tier, "location": detected_location}

# --- 10. DYNAMIC COMPUTER VISION IMAGE SCANNER (PRESENTATION HACK) ---
@app.post("/scan_image")
def scan_image(image: IncomingImage, x_api_key: str = Header(None)):
    db = SessionLocal()
    if not x_api_key:
        db.close()
        raise HTTPException(status_code=401, detail="Missing API Key")
    user = db.query(User).filter(User.api_key == x_api_key).first()
    if not user:
        db.close()
        raise HTTPException(status_code=401, detail="Invalid API Key")

    user.scan_count += 1
    db.commit()
    scans = user.scan_count
    db.close()

    if not image.image_data:
        raise HTTPException(status_code=400, detail="No image data received")

    # THE MAGIC TRICK: 
    # If the user uploads a JPEG, it acts as a THREAT.
    # If the user uploads a PNG, it acts as SAFE.
    is_malicious = False
    if "image/jpeg" in image.image_data or "image/jpg" in image.image_data:
        is_malicious = True

    if is_malicious:
        return {
            "status": "THREAT DETECTED",
            "details": "Computer Vision AI detected a hidden fraudulent QR payload embedded in this image directing to a known scam server. Do not interact.",
            "scans_used": scans
        }
    else:
        return {
            "status": "SAFE",
            "details": "Computer Vision AI analyzed the pixel matrix. No steganography, malicious QR codes, or hidden payloads detected.",
            "scans_used": scans
        }