File size: 27,860 Bytes
f0497f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# PrepGenie/app.py
"""Main Gradio application file."""

import gradio as gr
import os
import json
import google.generativeai as genai
from dotenv import load_dotenv
import datetime

# --- Import Logic Modules ---
import interview_logic
import interview_history
# import auth_logic # If you move auth logic
# import chat_logic # If chat logic is moved

# --- Environment and Configuration ---
load_dotenv()

# --- Firebase Admin SDK Setup (in app.py or auth_logic) ---
import firebase_admin
from firebase_admin import credentials, auth, firestore # Added firestore

def initialize_firebase():
    """Attempts to initialize Firebase Admin SDK."""
    if firebase_admin._apps:
        print("Firebase app already initialized in app.py.")
        return firebase_admin.get_app()
    cred = None
    try:
        firebase_credentials_path = os.getenv("FIREBASE_CREDENTIALS_PATH", "prepgenie-64134-firebase-adminsdk-fbsvc-3370ac4ab9.json")
        if firebase_credentials_path and os.path.exists(firebase_credentials_path):
            print(f"Initializing Firebase with credentials file in app.py: {firebase_credentials_path}")
            cred = credentials.Certificate(firebase_credentials_path)
            firebase_app = firebase_admin.initialize_app(cred)
            print("Firebase Admin initialized successfully using credentials file in app.py.")
            return firebase_app
        elif not firebase_credentials_path:
             print("FIREBASE_CREDENTIALS_PATH is not set or is None in app.py.")
        else:
             print(f"Firebase credentials file not found at {firebase_credentials_path} in app.py.")
    except Exception as e:
        print(f"Failed to initialize Firebase using credentials file in app.py: {e}")
    try:
        firebase_credentials_json = os.getenv("FIREBASE_CREDENTIALS_JSON")
        if firebase_credentials_json:
            print("Initializing Firebase with credentials from FIREBASE_CREDENTIALS_JSON environment variable in app.py.")
            cred_dict = json.loads(firebase_credentials_json)
            cred = credentials.Certificate(cred_dict)
            firebase_app = firebase_admin.initialize_app(cred)
            print("Firebase Admin initialized successfully using FIREBASE_CREDENTIALS_JSON in app.py.")
            return firebase_app
        else:
             print("FIREBASE_CREDENTIALS_JSON environment variable not set in app.py.")
    except (json.JSONDecodeError, ValueError) as e:
        print(f"Error parsing FIREBASE_CREDENTIALS_JSON in app.py: {e}")
    except Exception as e:
        print(f"Failed to initialize Firebase using FIREBASE_CREDENTIALS_JSON in app.py: {e}")
    print("Warning: Firebase Admin SDK could not be initialized in app.py. Authentication features will not work.")
    return None

FIREBASE_APP = initialize_firebase()
FIREBASE_AVAILABLE = FIREBASE_APP is not None

# --- Generative AI Setup ---
genai.configure(api_key=os.getenv("GOOGLE_API_KEY") or "YOUR_DEFAULT_API_KEY_HERE")
TEXT_MODEL = genai.GenerativeModel("gemini-1.5-flash") # Global model instance
print("Using Generative AI model: gemini-1.5-flash in app.py")

# --- Import Chat Module Functions ---
try:
    from login_module import chat as chat_module # Assuming this is external
    CHAT_MODULE_AVAILABLE = True
    print("Chat module imported successfully in app.py.")
except ImportError as e:
    print(f"Warning: Could not import chat module in app.py: {e}")
    CHAT_MODULE_AVAILABLE = False
    chat_module = None

# --- Helper Functions for UI Updates ---
def apply_ui_updates(updates_dict):
    """Converts logic function UI update instructions to Gradio updates."""
    gr_updates = {}
    for component_name, instruction in updates_dict.items():
        if instruction == "gr_hide":
            gr_updates[component_name] = gr.update(visible=False)
        elif instruction == "gr_show":
            gr_updates[component_name] = gr.update(visible=True)
        elif instruction == "gr_show_and_update":
             # This needs specific handling in the calling function
             # Placeholder, logic should set value in calling function
             gr_updates[component_name] = gr.update(visible=True)
        elif instruction == "gr_show_and_update_error":
             gr_updates[component_name] = gr.update(visible=True)
        elif instruction == "gr_clear":
             gr_updates[component_name] = "" # For textboxes
        elif instruction == "gr_clear_dict":
             gr_updates[component_name] = {} # For JSON
        # Add more instructions as needed
        else:
            # Default or pass through
            gr_updates[component_name] = gr.update()
    return gr_updates

# --- Navigation Functions ---
def navigate_to_interview():
    return (gr.update(visible=True), gr.update(visible=False), gr.update(visible=False))

def navigate_to_chat():
    return (gr.update(visible=False), gr.update(visible=True), gr.update(visible=False))

def navigate_to_history():
    return (gr.update(visible=False), gr.update(visible=False), gr.update(visible=True))

# --- Event Handler Functions (Orchestrators) ---
def process_resume_handler(file_obj):
    """Handles resume processing event."""
    result = interview_logic.process_resume_logic(file_obj)
    ui_updates = apply_ui_updates(result["ui_updates"])
    # Unpack ui_updates in the correct order for outputs
    return (
        result["status"],
        ui_updates.get("role_selection", gr.update()),
        ui_updates.get("start_interview_btn", gr.update()),
        ui_updates.get("question_display", gr.update()),
        ui_updates.get("answer_instructions", gr.update()),
        ui_updates.get("audio_input", gr.update()),
        ui_updates.get("submit_answer_btn", gr.update()),
        ui_updates.get("next_question_btn", gr.update()),
        ui_updates.get("submit_interview_btn", gr.update()),
        ui_updates.get("answer_display", gr.update()),
        ui_updates.get("feedback_display", gr.update()),
        ui_updates.get("metrics_display", gr.update()),
        result["processed_data"] # Pass processed data
    )

def start_interview_handler(roles, processed_resume_data):
    """Handles interview start event."""
    # First, format the resume data using the AI model
    formatted_resume_data = interview_logic.getallinfo(processed_resume_data, TEXT_MODEL)
    result = interview_logic.start_interview_logic(roles, formatted_resume_data, TEXT_MODEL)
    ui_updates = apply_ui_updates(result["ui_updates"])
    return (
        result["status"],
        result["initial_question"],
        ui_updates.get("audio_input", gr.update()),
        ui_updates.get("submit_answer_btn", gr.update()),
        ui_updates.get("next_question_btn", gr.update()),
        ui_updates.get("submit_interview_btn", gr.update()),
        ui_updates.get("feedback_display", gr.update()),
        ui_updates.get("metrics_display", gr.update()),
        ui_updates.get("question_display", gr.update()),
        ui_updates.get("answer_instructions", gr.update()),
        result["interview_state"]
    )

def submit_answer_handler(audio, interview_state):
    """Handles answer submission event."""
    result = interview_logic.submit_answer_logic(audio, interview_state, TEXT_MODEL)
    ui_updates = apply_ui_updates(result["ui_updates"])
    # Handle special updates for feedback and metrics that need value setting
    feedback_update = ui_updates.get("feedback_display", gr.update())
    if "gr_show_and_update" in result["ui_updates"].values():
        feedback_update = gr.update(visible=True, value=result["feedback_text"])
    metrics_update = ui_updates.get("metrics_display", gr.update())
    if "gr_show_and_update" in result["ui_updates"].values():
        metrics_update = gr.update(visible=True, value=result["metrics"])

    return (
        result["status"],
        result["answer_text"],
        result["interview_state"],
        feedback_update,
        metrics_update,
        ui_updates.get("audio_input", gr.update()),
        ui_updates.get("submit_answer_btn", gr.update()),
        ui_updates.get("next_question_btn", gr.update()),
        ui_updates.get("submit_interview_btn", gr.update()),
        ui_updates.get("question_display", gr.update()),
        ui_updates.get("answer_instructions", gr.update())
    )

def next_question_handler(interview_state):
    """Handles next question event."""
    result = interview_logic.next_question_logic(interview_state)
    ui_updates = apply_ui_updates(result["ui_updates"])
    return (
        result["status"],
        result["next_q"],
        result["interview_state"],
        ui_updates.get("audio_input", gr.update()),
        ui_updates.get("submit_answer_btn", gr.update()),
        ui_updates.get("next_question_btn", gr.update()),
        ui_updates.get("feedback_display", gr.update()),
        ui_updates.get("metrics_display", gr.update()),
        ui_updates.get("submit_interview_btn", gr.update()),
        ui_updates.get("question_display", gr.update()),
        ui_updates.get("answer_instructions", gr.update()),
        ui_updates.get("answer_display", ""), # Clear
        ui_updates.get("metrics_display_clear", {}) # Clear
    )

def submit_interview_handler(interview_state, user_id_state=""):
    """Handles interview submission event."""
    result = interview_logic.submit_interview_logic(interview_state, TEXT_MODEL)
    ui_updates = apply_ui_updates(result["ui_updates"])

    # --- NEW: Save Interview History ---
    if FIREBASE_AVAILABLE and user_id_state and "report_text" in result and "chart_buffer" in result:
        print(f"Attempting to save history for user: {user_id_state} in submit_interview_handler")
        # Package data for saving (using data from interview_state and result)
        interview_summary_data = {
            "timestamp": datetime.datetime.now().isoformat(),
            "resume_overview": interview_state.get("resume_data", ""),
            "selected_roles": interview_state.get("selected_roles", []),
            "questions": list(interview_state.get("interactions", {}).keys()),
            "answers": list(interview_state.get("interactions", {}).values()),
            "feedback": interview_state.get("feedback", []),
            "interactions": interview_state.get("interactions", {}),
            "metrics_list": interview_state.get("metrics_list", []),
            "final_metrics": interview_logic.parse_metrics(interview_logic.getmetrics(interview_state.get("interactions", {}), interview_state.get("resume_data", ""), TEXT_MODEL)), # Recalculate or pass from logic?
            # Use average from report text parsing or recalculate if needed, or pass from logic if stored
            "average_rating": sum(interview_logic.parse_metrics(interview_logic.getmetrics(interview_state.get("interactions", {}), interview_state.get("resume_data", ""), TEXT_MODEL)).values()) / len(interview_logic.parse_metrics(interview_logic.getmetrics(interview_state.get("interactions", {}), interview_state.get("resume_data", ""), TEXT_MODEL))) if interview_logic.parse_metrics(interview_logic.getmetrics(interview_state.get("interactions", {}), interview_state.get("resume_data", ""), TEXT_MODEL)) else 0.0,
            "evaluation_report": result["report_text"]
        }
        save_success = interview_history.save_interview_history(user_id_state, interview_summary_data)
        if save_success:
            print("Interview history saved successfully in submit_interview_handler.")
            # Optionally append to report text
            # result["report_text"] += "\n\n---\n*Interview history saved successfully.*"
        else:
            print("Failed to save interview history in submit_interview_handler.")
            # result["report_text"] += "\n\n---\n*Failed to save interview history.*"
    else:
        if not user_id_state:
            print("User not logged in. Skipping history save in submit_interview_handler.")
        else:
            print("Firestore not available. Skipping history save in submit_interview_handler.")

    # Handle special updates for report and chart that need value setting
    report_update = ui_updates.get("evaluation_report_display", gr.update())
    if "gr_show_and_update" in result["ui_updates"].values():
        report_update = gr.update(visible=True, value=result["report_text"])
    elif "gr_show_and_update_error" in result["ui_updates"].values():
         report_update = gr.update(visible=True, value=result["report_text"]) # Show error

    chart_update = ui_updates.get("evaluation_chart_display", gr.update())
    if "gr_show_and_update" in result["ui_updates"].values():
        chart_update = gr.update(visible=True, value=result["chart_buffer"])
    elif "gr_show_and_update_error" in result["ui_updates"].values():
         chart_update = gr.update(visible=False) # Hide chart on error

    return (
        result["status"],
        result["interview_state"], # Pass through
        report_update,
        chart_update
    )

# --- Login/Logout Logic (Simplified here, could be in auth_logic) ---
def login(email, password):
    if not FIREBASE_AVAILABLE:
        return (
            "Firebase not initialized. Login unavailable.",
            gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
            "", "", "", ""
        )
    if not email or not password:
        return (
            "Please enter email and password.",
            gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
            email, password, "", ""
        )
    try:
        user = auth.get_user_by_email(email)
        welcome_msg = f"Welcome, {user.display_name or user.uid}!"
        return (
            welcome_msg,
            gr.update(visible=False), gr.update(visible=False), gr.update(visible=True),
            "", "", user.uid, user.email
        )
    except auth.UserNotFoundError:
        return (
            "User not found. Please check your email or sign up.",
            gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
            email, password, "", ""
        )
    except Exception as e:
        error_msg = f"Login failed: {str(e)}"
        print(error_msg)
        return (
            error_msg,
            gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
            email, password, "", ""
        )

def signup(email, password, username):
    if not FIREBASE_AVAILABLE:
        return (
            "Firebase not initialized. Signup unavailable.",
            gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
            "", "", "", "", ""
        )
    if not email or not password or not username:
        return (
            "Please fill all fields.",
            gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),
            email, password, username, "", ""
        )
    try:
        user = auth.create_user(email=email, password=password, uid=username, display_name=username)
        success_msg = f"Account created successfully for {username}!"
        return (
            success_msg,
            gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
            "", "", "", user.uid, user.email
        )
    except auth.UidAlreadyExistsError:
        return (
            "Username already exists. Please choose another.",
            gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),
            email, password, username, "", ""
        )
    except auth.EmailAlreadyExistsError:
        return (
            "Email already exists. Please use another email.",
            gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),
            email, password, username, "", ""
        )
    except Exception as e:
        error_msg = f"Signup failed: {str(e)}"
        print(error_msg)
        return (
            error_msg,
            gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),
            email, password, username, "", ""
        )

def logout():
    return (
        "",
        gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
        "", "", "", "", ""
    )

# --- Gradio Interface ---
with gr.Blocks(title="PrepGenie - Mock Interviewer") as demo:
    interview_state = gr.State({})
    user_state = gr.State("") # Holds user.uid
    user_email_state = gr.State("") # Holds user.email
    processed_resume_data_state = gr.State("")

    # --- Header Section ---
    with gr.Row():
        gr.Markdown(
            """

            <h1 style="display: flex; justify-content: center; align-items: center;">

                PrepGenie- Interview Preparation App

            </h1>

            """,
            elem_id="title"
        )

    # --- Login Section ---
    with gr.Column(visible=True) as login_section:
        gr.Markdown("## Login")
        login_email_input = gr.Textbox(label="Email Address")
        login_password_input = gr.Textbox(label="Password", type="password")
        login_btn = gr.Button("Login")
        login_status = gr.Textbox(label="Login Status", interactive=False)
        switch_to_signup_btn = gr.Button("Don't have an account? Sign Up")

    # --- Signup Section ---
    with gr.Column(visible=False) as signup_section:
        gr.Markdown("## Sign Up")
        signup_email_input = gr.Textbox(label="Email Address")
        signup_password_input = gr.Textbox(label="Password", type="password")
        signup_username_input = gr.Textbox(label="Unique Username")
        signup_btn = gr.Button("Create my account")
        signup_status = gr.Textbox(label="Signup Status", interactive=False)
        switch_to_login_btn = gr.Button("Already have an account? Login")

    # --- Main App Sections ---
    with gr.Column(visible=False) as main_app:
        with gr.Row():
            with gr.Column(scale=1):
                 logout_btn = gr.Button("Logout")
            with gr.Column(scale=4):
                welcome_display = gr.Markdown("### Welcome, User!")

        with gr.Row():
            with gr.Column(scale=1):
                interview_btn = gr.Button("Mock Interview")
                if CHAT_MODULE_AVAILABLE:
                    chat_btn = gr.Button("Chat with Resume")
                else:
                    chat_btn = gr.Button("Chat with Resume (Unavailable)", interactive=False)
                history_btn = gr.Button("My Interview History")

            with gr.Column(scale=4):
                # --- Interview Section ---
                with gr.Column(visible=False) as interview_selection:
                    gr.Markdown("## Mock Interview")
                    with gr.Row():
                        with gr.Column():
                            file_upload_interview = gr.File(label="Upload Resume (PDF)", file_types=[".pdf"])
                            process_btn_interview = gr.Button("Process Resume")
                        with gr.Column():
                            file_status_interview = gr.Textbox(label="Status", interactive=False)
                    role_selection = gr.Dropdown(
                        choices=["Data Scientist", "Software Engineer", "Product Manager", "Data Analyst", "Business Analyst"],
                        multiselect=True, label="Select Job Role(s)", visible=False
                    )
                    start_interview_btn = gr.Button("Start Interview", visible=False)
                    question_display = gr.Textbox(label="Question", interactive=False, visible=False)
                    answer_instructions = gr.Markdown("Click 'Record Answer' and speak your response.", visible=False)
                    audio_input = gr.Audio(label="Record Answer", type="numpy", visible=False)
                    submit_answer_btn = gr.Button("Submit Answer", visible=False)
                    next_question_btn = gr.Button("Next Question", visible=False)
                    submit_interview_btn = gr.Button("Submit Interview", visible=False, variant="primary")
                    answer_display = gr.Textbox(label="Your Answer", interactive=False, visible=False)
                    feedback_display = gr.Textbox(label="Feedback", interactive=False, visible=False)
                    metrics_display = gr.JSON(label="Metrics", visible=False)
                    processed_resume_data_hidden_interview = gr.Textbox(visible=False)
                    # --- Evaluation Results Section ---
                    with gr.Column(visible=False) as evaluation_selection:
                        gr.Markdown("## Interview Evaluation Results")
                        evaluation_report_display = gr.Markdown(label="Your Evaluation Report", visible=False)
                        evaluation_chart_display = gr.Image(label="Skills Breakdown", type="pil", visible=False)

                # --- Chat Section ---
                if CHAT_MODULE_AVAILABLE:
                    with gr.Column(visible=False) as chat_selection:
                        gr.Markdown("## Chat with Resume")
                        with gr.Row():
                            with gr.Column():
                                file_upload_chat = gr.File(label="Upload Resume (PDF)", file_types=[".pdf"])
                                process_chat_btn = gr.Button("Process Resume")
                            with gr.Column():
                                file_status_chat = gr.Textbox(label="Status", interactive=False)
                        chatbot = gr.Chatbot(label="Chat History", visible=False, type="messages")
                        query_input = gr.Textbox(label="Ask about your resume", placeholder="Type your question here...", visible=False)
                        send_btn = gr.Button("Send", visible=False)
                else:
                    with gr.Column(visible=False) as chat_selection:
                        gr.Markdown("## Chat with Resume (Unavailable)")
                        gr.Textbox(value="Chat module is not available.", interactive=False)

                # --- History Section ---
                with gr.Column(visible=False) as history_selection:
                    gr.Markdown("## Your Interview History")
                    load_history_btn = gr.Button("Load My Past Interviews")
                    history_output = gr.Textbox(label="Past Interviews", max_lines=30, interactive=False, visible=True)

        # Assign view variables for navigation
        interview_view = interview_selection
        chat_view = chat_selection
        history_view = history_selection

        # Navigation button listeners
        interview_btn.click(fn=navigate_to_interview, inputs=None, outputs=[interview_view, chat_view, history_view])
        if CHAT_MODULE_AVAILABLE:
            chat_btn.click(fn=navigate_to_chat, inputs=None, outputs=[interview_view, chat_view, history_view])
        history_btn.click(fn=navigate_to_history, inputs=None, outputs=[interview_view, chat_view, history_view])

    # --- Event Listeners for Interview ---
    process_btn_interview.click(
        fn=process_resume_handler,
        inputs=[file_upload_interview],
        outputs=[
            file_status_interview, role_selection, start_interview_btn,
            question_display, answer_instructions, audio_input,
            submit_answer_btn, next_question_btn, submit_interview_btn,
            answer_display, feedback_display, metrics_display,
            processed_resume_data_hidden_interview
        ]
    )
    start_interview_btn.click(
        fn=start_interview_handler,
        inputs=[role_selection, processed_resume_data_hidden_interview],
        outputs=[
            file_status_interview, question_display,
            audio_input, submit_answer_btn, next_question_btn,
            submit_interview_btn, feedback_display, metrics_display,
            question_display, answer_instructions,
            interview_state
        ]
    )
    submit_answer_btn.click(
        fn=submit_answer_handler,
        inputs=[audio_input, interview_state],
        outputs=[
            file_status_interview, answer_display, interview_state,
            feedback_display, metrics_display,
            audio_input, submit_answer_btn, next_question_btn,
            submit_interview_btn, question_display, answer_instructions
        ]
    )
    next_question_btn.click(
        fn=next_question_handler,
        inputs=[interview_state],
        outputs=[
            file_status_interview, question_display, interview_state,
            audio_input, submit_answer_btn, next_question_btn,
            feedback_display, metrics_display, submit_interview_btn,
            question_display, answer_instructions,
            answer_display, metrics_display # Clear previous
        ]
    )
    submit_interview_btn.click(
        fn=submit_interview_handler,
        inputs=[interview_state, user_state], # Pass user_state for history saving
        outputs=[
            file_status_interview,
            interview_state,
            evaluation_report_display,
            evaluation_chart_display
        ]
    )

    # --- Event Listeners for Chat ---
    if CHAT_MODULE_AVAILABLE:
        process_chat_btn.click(
            fn=chat_module.process_resume_chat,
            inputs=[file_upload_chat],
            outputs=[file_status_chat, processed_resume_data_state, query_input, send_btn, chatbot]
        )
        send_btn.click(
            fn=chat_module.chat_with_resume,
            inputs=[query_input, processed_resume_data_state, chatbot],
            outputs=[query_input, chatbot]
        )
        query_input.submit(
            fn=chat_module.chat_with_resume,
            inputs=[query_input, processed_resume_data_state, chatbot],
            outputs=[query_input, chatbot]
        )

    # --- NEW: Event Listener for History ---
    load_history_btn.click(fn=interview_history.load_user_history, inputs=[user_state], outputs=[history_output])

    # --- Login/Logout Event Listeners ---
    login_btn.click(
        fn=login,
        inputs=[login_email_input, login_password_input],
        outputs=[login_status, login_section, signup_section, main_app,
                 login_email_input, login_password_input, user_state, user_email_state]
    )
    signup_btn.click(
        fn=signup,
        inputs=[signup_email_input, signup_password_input, signup_username_input],
        outputs=[signup_status, login_section, signup_section, main_app,
                 signup_email_input, signup_password_input, signup_username_input,
                 user_state, user_email_state]
    )
    logout_btn.click(
        fn=logout,
        inputs=None,
        outputs=[login_status, login_section, signup_section, main_app,
                 login_email_input, login_password_input, signup_username_input,
                 user_state, user_email_state]
    )
    switch_to_signup_btn.click(
        fn=lambda: (gr.update(visible=False), gr.update(visible=True)),
        inputs=None,
        outputs=[login_section, signup_section]
    )
    switch_to_login_btn.click(
        fn=lambda: (gr.update(visible=True), gr.update(visible=False)),
        inputs=None,
        outputs=[login_section, signup_section]
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0")