File size: 35,458 Bytes
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a96637
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afe3487
e8ab00f
 
 
 
 
d0dde6e
798d668
e8ab00f
 
 
 
 
 
01db229
d0dde6e
477a67c
 
 
 
 
 
 
 
 
 
6862507
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01db229
 
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4386762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8ab00f
 
4386762
e8ab00f
 
 
4386762
e8ab00f
 
4386762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4386762
 
 
 
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4386762
 
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
4386762
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4386762
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4386762
 
 
 
 
 
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dd3718
 
 
 
e8ab00f
 
 
 
 
 
 
 
1dd3718
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dd3718
 
e8ab00f
 
 
 
 
 
 
 
 
 
 
 
1dd3718
e8ab00f
 
 
 
 
 
 
 
 
 
9f420d8
 
 
 
e8ab00f
 
 
 
9f420d8
e8ab00f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from datetime import datetime, timedelta, date
import requests
import google.generativeai as genai

# Constants
ENGINEER_SALARY = 10000  # Monthly cost per engineer ($120K/year)

# Initialize session state variables
if 'startups' not in st.session_state:
    st.session_state.startups = {}  # Dictionary to store startup data
if 'current_startup' not in st.session_state:
    st.session_state.current_startup = None  # Currently selected startup
if 'current_page' not in st.session_state:
    st.session_state.current_page = 'upload'  # Default page
if 'insights_cache' not in st.session_state:
    st.session_state.insights_cache = {}
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = [
        {"role": "assistant", "content": "Hi! I'm your AI financial advisor. How can I help with your startup's finances?"}
    ]

# Setup page config and styling
st.set_page_config(page_title="MONEYMINDSPilot", page_icon="๐Ÿ’ฐ", layout="wide")

# Apply custom styling
st.markdown("""
<style>
    .main-header {font-size: 2.5rem; color: #0066cc; margin-bottom: 0.5rem;}
    .sub-header {font-size: 1.5rem; color: #5c5c5c; margin-bottom: 1.5rem;}
    .metric-card {background-color: #f8f9fa; border-radius: 10px; padding: 20px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);}
    .metric-label {font-size: 1rem; color: #5c5c5c;}
    .metric-value {font-size: 1.8rem; color: #0066cc; font-weight: bold;}
    .good-metric {color: #28a745;}
    .warning-metric {color: #ffc107;}
    .danger-metric {color: #dc3545;}
    .title-box {background: linear-gradient(45deg, #0066cc, #66b3ff); padding: 20px; border-radius: 10px; 
                margin-bottom: 20px; text-align: center; color: white;}
    .ai-badge {display: inline-block; background-color: #0066cc; color: white; border-radius: 4px; 
              padding: 2px 6px; font-size: 0.7rem; font-weight: bold; margin-bottom: 8px;}
    .insight-card, .advisor-card {background-color: #f8f9fa; border-radius: 10px; padding: 15px; 
                                margin-bottom: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);}
    div.stButton > button {width: 100%; padding: 10px; border: none; background-color: #E6F3FF; 
                          color: #0066cc; border-radius: 10px; text-align: left; font-weight: bold;}
    div.stButton > button:hover {background-color: #CCE5FF; color: #004080;}
</style>
""", unsafe_allow_html=True)

# AI Integration Functions
def initialize_gemini():
    """Initialize Google's Gemini AI with API key"""
    try:
        api_key = st.secrets.get("GEMINI_API_KEY", None)
        if api_key:
            genai.configure(api_key=api_key)
            return True
        else:
            st.warning("Gemini API key not found. Using simulated responses.")
            return False
    except Exception as e:
        st.error(f"Failed to initialize Gemini AI: {e}")
        return False

def generate_ai_response(prompt, simulate=False):
    """Generate text using Google's Gemini AI"""
    if simulate:
        return "AI response simulation: Based on your financial data, I recommend focusing on extending runway, accelerating revenue growth, and preparing for your next funding round."
    else:
        try:
            # Use the correct model name
            model = genai.GenerativeModel('gemini-2.0-flash')  # Updated model name
            response = model.generate_content(prompt)
            return response.text
        except Exception as e:
            st.error(f"Error generating AI response: {e}")
            return "Sorry, I couldn't generate a response at this time."



def autoplay_audio(audio_content):
    """Generate HTML with audio player that auto-plays"""
    b64 = base64.b64encode(audio_content).decode()
    md = f"""
        <audio controls autoplay>
        <source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
        </audio>
        """
    st.markdown(md, unsafe_allow_html=True)
    
def generate_voice_response(text, simulate=False):
    """Generate voice response using ElevenLabs API"""
    if simulate:
        return None
    else:
        try:
            api_key = st.secrets.get("ELEVENLABS_API_KEY", None)
            if not api_key:
                return None
                
            url = "https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM"  # Rachel voice
            
            headers = {
                "Accept": "audio/mpeg",
                "Content-Type": "application/json",
                "xi-api-key": api_key
            }
            
            data = {
                "text": text,
                "model_id": "eleven_monolingual_v1",
                "voice_settings": {
                    "stability": 0.5,
                    "similarity_boost": 0.5
                }
            }
            
            response = requests.post(url, json=data, headers=headers)
            
            if response.status_code == 200:
                return response.content
            else:
                st.error(f"Error with ElevenLabs API: {response.status_code}")
                return None
                
        except Exception as e:
            st.error(f"Error generating voice response: {e}")
            return None
                
    
# Utility Functions
def switch_page(page_name):
    """Function to switch between pages"""
    st.session_state.current_page = page_name
    st.rerun()

def calculate_runway(cash, burn_rate, revenue, growth_rate, months=24):
    """Calculate runway based on cash, burn, revenue and growth"""
    current_date = datetime.now()
    date_range = [current_date + timedelta(days=30*i) for i in range(months)]
    
    cash_flow = []
    monthly_revenue = revenue
    
    for i in range(months):
        net_burn = burn_rate - monthly_revenue
        cash_flow.append(net_burn)
        monthly_revenue *= (1 + growth_rate)
    
    df = pd.DataFrame({
        'Net_Burn': cash_flow,
        'Cumulative_Cash': [cash - sum(cash_flow[:i+1]) for i in range(len(cash_flow))]
    }, index=date_range)
    
    negative_cash = df[df['Cumulative_Cash'] < 0]
    runway_months = (negative_cash.index[0] - current_date).days // 30 if len(negative_cash) > 0 else months
    
    return runway_months, df

def simulate_decision(cash, burn_rate, revenue, growth_rate, 
                     additional_expenses, new_hires, marketing_increase, growth_impact):
    """
    Simulate the financial impact of a business decision with AI-powered insights
    
    Args:
    - cash: Current cash balance
    - burn_rate: Current monthly burn rate
    - revenue: Current monthly revenue
    - growth_rate: Current monthly growth rate
    - additional_expenses: Proposed additional monthly expenses
    - new_hires: Number of new hires
    - marketing_increase: Proposed marketing budget increase
    - growth_impact: Expected growth rate impact
    
    Returns:
    - current_runway: Current financial runway in months
    - new_runway: Projected runway after proposed changes
    - current_df: DataFrame with current financial projection
    - new_df: DataFrame with projected financial scenario
    - ai_analysis: AI-generated insights about the decision
    """
    # Calculate current runway
    current_runway, current_df = calculate_runway(cash, burn_rate, revenue, growth_rate)
    
    # Calculate new financial parameters
    new_burn_rate = burn_rate + additional_expenses + (new_hires * ENGINEER_SALARY) + marketing_increase
    new_growth_rate = growth_rate + growth_impact
    
    # Calculate new runway
    new_runway, new_df = calculate_runway(cash, new_burn_rate, revenue, new_growth_rate)
    
    # Generate AI analysis of the decision
    try:
        ai_analysis = generate_ai_response(f"""
        You are a strategic financial advisor for startups. Analyze this potential business decision:

        Current Financial Situation:
        - Cash Balance: ${cash:,}
        - Monthly Burn Rate: ${burn_rate:,}
        - Monthly Revenue: ${revenue:,}
        - Current Growth Rate: {growth_rate * 100:.1f}%
        - Current Runway: {current_runway} months

        Proposed Changes:
        - Additional Expenses: ${additional_expenses:,}/month
        - New Hires: {new_hires} engineers (${new_hires * ENGINEER_SALARY:,}/month)
        - Marketing Budget Increase: ${marketing_increase:,}/month
        - Expected Growth Impact: +{growth_impact * 100:.1f}%

        Projected Outcome:
        - New Burn Rate: ${new_burn_rate:,}/month
        - New Growth Rate: {new_growth_rate * 100:.1f}%
        - Projected Runway: {new_runway} months

        Provide a comprehensive analysis addressing:
        1. Financial feasibility of the proposed changes
        2. Risk assessment
        3. Potential strategic benefits
        4. Recommendations for optimization
        5. Key metrics to monitor

        Be direct, specific, and provide actionable insights.
        """, simulate=False)
    except Exception as e:
        ai_analysis = f"AI analysis unavailable. Error: {str(e)}"
    
    return current_runway, new_runway, current_df, new_df, ai_analysis

def detect_suspicious_transactions(transactions_df):
    """AI-enhanced suspicious transaction detection"""
    df = transactions_df.copy()
    
    # Define thresholds for each category
    category_thresholds = {
        "Travel": 3000, "Marketing": 10000, "Office": 7000,
        "Software": 6000, "Consulting": 5000, "Legal": 6000
    }
    
    suspicious_terms = ['luxury', 'cruise', 'premium', 'personal', 'gift']
    
    # Add analysis columns
    df['Suspicious'] = False
    df['Reason'] = ""
    df['Risk_Score'] = 0
    
    for idx, row in df.iterrows():
        reasons = []
        risk_score = 0
        
        # Check category thresholds
        if row['Category'] in category_thresholds and row['Amount'] > category_thresholds[row['Category']]:
            reasons.append(f"Amount exceeds typical spending for {row['Category']}")
            risk_score += 30
        
        # Check for suspicious terms
        for field in ['Vendor', 'Description']:
            if any(term in str(row[field]).lower() for term in suspicious_terms):
                reasons.append(f"{field} contains suspicious term")
                risk_score += 20
        
        # Check for round amounts
        if row['Amount'] % 1000 == 0 and row['Amount'] > 3000:
            reasons.append(f"Suspiciously round amount")
            risk_score += 15
        
        # Mark as suspicious if risk score is high enough
        if risk_score >= 30:
            df.at[idx, 'Suspicious'] = True
            df.at[idx, 'Reason'] = "; ".join(reasons)
            df.at[idx, 'Risk_Score'] = risk_score
    
    return df.sort_values(by='Risk_Score', ascending=False)

def parse_csv_to_df(file):
    """Parse uploaded CSV file to DataFrame"""
    try:
        df = pd.read_csv(file)
        return df, None
    except Exception as e:
        return None, f"Error parsing CSV: {e}"

# Navigation
def create_sidebar():
    with st.sidebar:
        st.markdown("""
            <div class="title-box">
                <h1>๐Ÿ’ฐ StartupFinancePilot</h1>
                <p>AI-powered financial assistant for startups</p>
            </div>
        """, unsafe_allow_html=True)

        # Startup selector
        if st.session_state.startups:
            st.subheader("Selected Startup")
            startup_names = list(st.session_state.startups.keys())
            selected_startup = st.selectbox(
                "Choose Startup", 
                startup_names,
                index=startup_names.index(st.session_state.current_startup) if st.session_state.current_startup in startup_names else 0
            )
            st.session_state.current_startup = selected_startup
            
            # Show basic startup info
            if selected_startup in st.session_state.startups:
                startup_data = st.session_state.startups[selected_startup]['profile']
                st.markdown(f"""
                **Stage:** {startup_data['stage']}  
                **Cash:** ${startup_data['cash']:,}  
                **Monthly Burn:** ${startup_data['burn_rate']:,}  
                **Monthly Revenue:** ${startup_data['revenue']:,}  
                """)

        st.markdown("<hr>", unsafe_allow_html=True)
        
        # Navigation buttons
        if st.button("๐Ÿ“ค Upload Startup Data", use_container_width=True):
            switch_page('upload')
        if st.button("๐Ÿ“Š Financial Dashboard", use_container_width=True):
            switch_page('dashboard')
        if st.button("๐Ÿ”ฎ Decision Simulator", use_container_width=True):
            switch_page('simulator')
        if st.button("๐Ÿ•ต๏ธ Fund Monitoring", use_container_width=True):
            switch_page('monitoring')
        if st.button("๐Ÿค– AI Financial Advisor", use_container_width=True):
            switch_page('advisor')

# Page Renderers
def render_upload_page():
    """Render the upload page for startup data"""
    st.markdown("<h1 class='main-header'>Upload Your Startup Data</h1>", unsafe_allow_html=True)
    st.markdown("<p class='sub-header'>Upload CSV files to get started</p>", unsafe_allow_html=True)
    
    with st.expander("Upload Instructions", expanded=False):
        st.markdown("""
        ### How to Upload Your Startup Data
        
        You can upload three types of files:
        
        1. **Company Profile** - A CSV with basic information about your startup including:
           - name, stage, founded, employees, last_funding, cash, burn_rate, revenue, growth_rate
        
        2. **Cash Flow Data** - A CSV with monthly cash flow data with columns:
           - Month, Revenue, Payroll, Marketing, Office, Software, Travel, Legal, Misc
        
        3. **Transaction Data** - A CSV with transaction details:
           - Date, Category, Vendor, Amount, Description, Flag
        """)
    
    startup_name = st.text_input("Startup Name", value="My Startup")
    
    col1, col2, col3 = st.columns(3)
    
    with col1:
        profile_file = st.file_uploader("Upload Company Profile (CSV)", type=['csv'])
    with col2:
        cash_flow_file = st.file_uploader("Upload Cash Flow Data (CSV)", type=['csv'])
    with col3:
        transactions_file = st.file_uploader("Upload Transactions Data (CSV)", type=['csv'])
    
    # Process the files if uploaded
    if st.button("Process Data"):
        # Initialize with default values
        startup_data = {
            "name": startup_name,
            "stage": "Seed",
            "founded": "12 months ago",
            "employees": 5,
            "last_funding": "Not specified",
            "cash": 100000,
            "burn_rate": 20000,
            "revenue": 5000,
            "growth_rate": 0.05
        }
        
        cash_flow_df = None
        transactions_df = None
        
        # Parse company profile
        if profile_file:
            profile_df, error = parse_csv_to_df(profile_file)
            if error:
                st.error(error)
            elif len(profile_df) > 0:
                startup_data.update(profile_df.iloc[0].to_dict())
                st.success(f"Successfully loaded company profile")
        
        # Parse cash flow data
        if cash_flow_file:
            cash_flow_df, error = parse_csv_to_df(cash_flow_file)
            if error:
                st.error(error)
            else:
                if "Total_Expenses" not in cash_flow_df.columns:
                    expense_columns = [col for col in cash_flow_df.columns if col not in ["Month", "Revenue", "Total_Expenses", "Net_Burn"]]
                    cash_flow_df["Total_Expenses"] = cash_flow_df[expense_columns].sum(axis=1)
                
                if "Net_Burn" not in cash_flow_df.columns:
                    cash_flow_df["Net_Burn"] = cash_flow_df["Total_Expenses"] - cash_flow_df["Revenue"]
                
                st.success("Successfully loaded cash flow data")
        
        # Parse transactions data
        if transactions_file:
            transactions_df, error = parse_csv_to_df(transactions_file)
            if error:
                st.error(error)
            else:
                # Ensure transactions data has required columns
                required_columns = ["Date", "Category", "Vendor", "Amount", "Description"]
                if all(col in transactions_df.columns for col in required_columns):
                    if "Flag" not in transactions_df.columns:
                        transactions_df["Flag"] = "Normal"
                    st.success("Successfully loaded transactions data")
                else:
                    st.error("Transactions file is missing required columns")
        
        # Save to session state if we have at least some data
        if profile_file:
            # Store in session state
            st.session_state.startups[startup_data['name']] = {
                'profile': startup_data,
                'cash_flow': cash_flow_df,
                'transactions': transactions_df
            }
            
            # Set as current startup
            st.session_state.current_startup = startup_data['name']
            
            st.success(f"Successfully added {startup_data['name']} to your startups")
            switch_page('dashboard')
        else:
            st.error("Please upload at least a company profile file")

def render_financial_dashboard():
    """Render the AI-powered financial dashboard page"""
    if not st.session_state.current_startup or st.session_state.current_startup not in st.session_state.startups:
        st.warning("No startup selected. Please upload data first.")
        render_upload_page()
        return
    
    # Get the selected startup data
    startup_data = st.session_state.startups[st.session_state.current_startup]['profile']
    cash_flow_df = st.session_state.startups[st.session_state.current_startup]['cash_flow']
    
    st.markdown("<h1 class='main-header'>Financial Dashboard</h1>", unsafe_allow_html=True)
    
    # AI Insights
    insights_key = f"dashboard_{date.today().isoformat()}"
    if insights_key not in st.session_state.insights_cache:
        insights = generate_ai_response(f"""
        You are a financial advisor for startups. Based on this startup's data:
        - Current cash: ${startup_data['cash']}
        - Monthly burn rate: ${startup_data['burn_rate']}
        - Monthly revenue: ${startup_data['revenue']}
        - Monthly growth rate: {startup_data['growth_rate'] * 100}%
        
        Provide the top 3 most important financial insights that the founder should know today.
        Format each insight as a brief, action-oriented bullet point.
        """)
        st.session_state.insights_cache[insights_key] = insights
    
    with st.expander("๐Ÿ“Š AI Financial Insights", expanded=True):
        st.markdown("<span class='ai-badge'>AI-Generated Insights</span>", unsafe_allow_html=True)
        st.markdown(st.session_state.insights_cache[insights_key])
    
    # Key metrics
    col1, col2, col3, col4 = st.columns(4)
    
    # Calculate runway
    runway_months, runway_df = calculate_runway(
        startup_data['cash'], 
        startup_data['burn_rate'], 
        startup_data['revenue'], 
        startup_data['growth_rate']
    )
    
    # Determine status colors
    runway_status = "danger-metric" if runway_months < 6 else ("warning-metric" if runway_months < 9 else "good-metric")
    burn_status = "danger-metric" if startup_data['burn_rate'] > 100000 else ("warning-metric" if startup_data['burn_rate'] > 80000 else "good-metric")
    revenue_status = "good-metric" if startup_data['revenue'] > 20000 else ("warning-metric" if startup_data['revenue'] > 10000 else "danger-metric")
    
    with col1:
        st.markdown(f"""
        <div class='metric-card'>
            <p class='metric-label'>Current Cash</p>
            <p class='metric-value'>${startup_data['cash']:,}</p>
        </div>
        """, unsafe_allow_html=True)
    
    with col2:
        st.markdown(f"""
        <div class='metric-card'>
            <p class='metric-label'>Monthly Burn</p>
            <p class='metric-value {burn_status}'>${startup_data['burn_rate']:,}</p>
        </div>
        """, unsafe_allow_html=True)
    
    with col3:
        st.markdown(f"""
        <div class='metric-card'>
            <p class='metric-label'>Monthly Revenue</p>
            <p class='metric-value {revenue_status}'>${startup_data['revenue']:,}</p>
        </div>
        """, unsafe_allow_html=True)
    
    with col4:
        st.markdown(f"""
        <div class='metric-card'>
            <p class='metric-label'>Runway</p>
            <p class='metric-value {runway_status}'>{runway_months} months</p>
        </div>
        """, unsafe_allow_html=True)
    
    # Financial charts
    st.subheader("Financial Overview")
    
    # Display only if we have cash flow data
    if cash_flow_df is not None:
        # Runway chart
        fig = px.line(runway_df.reset_index(), x='index', y='Cumulative_Cash', 
                    title="Cash Runway Projection",
                    labels={'index': 'Date', 'Cumulative_Cash': 'Remaining Cash ($)'},
                    color_discrete_sequence=['#0066cc'])
        fig.add_hline(y=0, line_dash="dash", line_color="red", annotation_text="Out of Cash")
        fig.update_layout(height=400)
        st.plotly_chart(fig, use_container_width=True)
        
        # Revenue vs Expenses
        fig = px.bar(cash_flow_df, x='Month', y=['Revenue', 'Total_Expenses'],
                    title="Revenue vs. Expenses",
                    barmode='group',
                    color_discrete_sequence=['#28a745', '#dc3545'])
        st.plotly_chart(fig, use_container_width=True)
    else:
        st.info("Upload cash flow data to see detailed financial charts")



        
def render_decision_simulator(startup_data):
    """Render the decision simulator page"""
    st.markdown("<h1 class='main-header'>Decision Simulator</h1>", unsafe_allow_html=True)
    st.markdown("<p class='sub-header'>Test the financial impact of business decisions</p>", unsafe_allow_html=True)
    
    # Decision input form
    with st.form("decision_form"):
        st.subheader("Scenario Parameters")
        
        col1, col2 = st.columns(2)
        
        with col1:
            new_hires = st.number_input("New Engineering Hires", min_value=0, max_value=10, value=0)
            st.caption(f"Monthly Cost: ${new_hires * ENGINEER_SALARY:,}")
            
            new_marketing = st.number_input("Additional Monthly Marketing Budget", 
                                          min_value=0, max_value=50000, value=0, step=1000)
        
        with col2:
            other_expenses = st.number_input("Other Additional Monthly Expenses", 
                                           min_value=0, max_value=50000, value=0, step=1000)
            
            growth_impact = st.slider("Estimated Impact on Monthly Growth Rate", 
                                    min_value=0.0, max_value=0.10, value=0.0, step=0.01, 
                                    format="%.2f")
        
        question = st.text_area("Describe your decision scenario", height=100)
        
        decision_summary = f"""
        - {new_hires} new engineers: ${new_hires * ENGINEER_SALARY:,}/month
        - Marketing increase: ${new_marketing:,}/month
        - Other expenses: ${other_expenses:,}/month
        - Total additional burn: ${new_hires * ENGINEER_SALARY + new_marketing + other_expenses:,}/month
        - Growth impact: +{growth_impact * 100:.1f}% monthly growth
        """
        
        st.markdown(f"**Decision Summary:**\n{decision_summary}")
        
        submitted = st.form_submit_button("Simulate Decision")
    
    if submitted:
        # Calculate current and new runway with AI analysis
        current_runway, new_runway, current_df, new_df, ai_analysis = simulate_decision(
            startup_data['cash'],
            startup_data['burn_rate'],
            startup_data['revenue'],
            startup_data['growth_rate'],
            other_expenses,
            new_hires,
            new_marketing,
            growth_impact
        )
        
        # Display results
        st.markdown("<h3>Decision Impact Analysis</h3>", unsafe_allow_html=True)
        
        # Summary metrics (existing code remains the same)
        col1, col2, col3 = st.columns(3)
        
        with col1:
            st.metric("Current Runway", f"{current_runway} months")
        with col2:
            runway_change = new_runway - current_runway
            st.metric("New Runway", f"{new_runway} months", 
                    delta=f"{runway_change} months", 
                    delta_color="off" if runway_change == 0 else ("normal" if runway_change > 0 else "inverse"))
        with col3:
            new_burn = startup_data['burn_rate'] + other_expenses + (new_hires * ENGINEER_SALARY) + new_marketing
            burn_change = new_burn - startup_data['burn_rate']
            burn_percentage = burn_change / startup_data['burn_rate'] * 100
            st.metric("New Monthly Burn", f"${new_burn:,}", 
                     delta=f"${burn_change:,} ({burn_percentage:.1f}%)", 
                     delta_color="inverse")
        
        # Cash projection comparison (existing code remains the same)
        st.subheader("Cash Projection Comparison")
        
        # Combine dataframes for comparison
        current_df['Scenario'] = 'Current'
        new_df['Scenario'] = 'After Decision'
        
        combined_df = pd.concat([current_df, new_df])
        combined_df = combined_df.reset_index()
        combined_df = combined_df.rename(columns={'index': 'Date'})
        
        # Plot comparison
        fig = px.line(combined_df, x='Date', y='Cumulative_Cash', color='Scenario',
                     title="Cash Runway Comparison",
                     labels={'Cumulative_Cash': 'Remaining Cash'},
                     color_discrete_sequence=['#4c78a8', '#f58518'])
        
        fig.add_hline(y=0, line_dash="dash", line_color="red", annotation_text="Out of Cash")
        fig.update_layout(height=400)
        
        st.plotly_chart(fig, use_container_width=True)
        
        # Display AI Analysis (New section)
        st.markdown("<div class='advisor-card'>", unsafe_allow_html=True)
        st.markdown("<span class='ai-badge'>AI Decision Analysis</span>", unsafe_allow_html=True)
        st.markdown(f"<p class='advice-text'>{ai_analysis}</p>", unsafe_allow_html=True)
        st.markdown("</div>", unsafe_allow_html=True)
        

def render_fund_monitoring():
    """Render the fund monitoring page"""
    if not st.session_state.current_startup or st.session_state.current_startup not in st.session_state.startups:
        st.warning("No startup selected. Please upload data first.")
        render_upload_page()
        return
    
    # Get the selected startup data
    transactions_df = st.session_state.startups[st.session_state.current_startup]['transactions']
    
    st.markdown("<h1 class='main-header'>Fund Monitoring</h1>", unsafe_allow_html=True)
    st.markdown("<p class='sub-header'>AI-powered fraud detection and spending analysis</p>", unsafe_allow_html=True)
    
    if transactions_df is None:
        st.warning("No transaction data available. Please upload transaction data.")
        return
        
    # Process transactions to detect suspicious ones
    processed_df = detect_suspicious_transactions(transactions_df)
    
    # Summary metrics
    total_transactions = len(processed_df)
    suspicious_transactions = processed_df[processed_df['Suspicious']].copy()
    suspicious_count = len(suspicious_transactions)
    suspicious_amount = suspicious_transactions['Amount'].sum() if not suspicious_transactions.empty else 0
    total_amount = processed_df['Amount'].sum()
    
    col1, col2 = st.columns(2)
    
    with col1:
        st.markdown(f"""
        <div class='metric-card'>
            <p class='metric-label'>Total Transactions</p>
            <p class='metric-value'>{total_transactions}</p>
        </div>
        """, unsafe_allow_html=True)
    
    with col2:
        flagged_percent = suspicious_count/total_transactions*100 if total_transactions > 0 else 0
        status = "danger-metric" if flagged_percent > 10 else ("warning-metric" if flagged_percent > 5 else "good-metric")
        st.markdown(f"""
        <div class='metric-card'>
            <p class='metric-label'>Flagged Transactions</p>
            <p class='metric-value {status}'>{suspicious_count} ({flagged_percent:.1f}%)</p>
        </div>
        """, unsafe_allow_html=True)
    
    # Tabs for different views
    tab1, tab2 = st.tabs(["Flagged Transactions", "All Transactions"])
    
    with tab1:
        if suspicious_count > 0:
            st.dataframe(
                suspicious_transactions[['Date', 'Category', 'Vendor', 'Amount', 'Description', 'Risk_Score', 'Reason']], 
                use_container_width=True
            )
            
            # Get AI analysis of suspicious transactions
            fraud_key = f"fraud_{date.today().isoformat()}"
            if fraud_key not in st.session_state.insights_cache:
                suspicious_text = "\n".join([
                    f"- {row['Vendor']} (${row['Amount']:.2f}): {row['Description']}"
                    for _, row in suspicious_transactions.head(5).iterrows()
                ])
                
                fraud_analysis = generate_ai_response(f"""
                You are a financial fraud detection expert. Review these flagged suspicious transactions:
                
                {suspicious_text}
                
                Provide a brief analysis and recommendations.
                """)
                st.session_state.insights_cache[fraud_key] = fraud_analysis
            
            st.markdown("<div class='advisor-card'>", unsafe_allow_html=True)
            st.markdown("<span class='ai-badge'>AI Fraud Analysis</span>", unsafe_allow_html=True)
            st.markdown(f"<p class='advice-text'>{st.session_state.insights_cache[fraud_key]}</p>", unsafe_allow_html=True)
            st.markdown("</div>", unsafe_allow_html=True)
        else:
            st.success("No suspicious transactions detected.")
    
    with tab2:
        st.dataframe(processed_df[['Date', 'Category', 'Vendor', 'Amount', 'Description', 'Suspicious', 'Risk_Score']], 
                    use_container_width=True)
    
    # Category spending
    if not processed_df.empty:
        st.subheader("Spending by Category")
        category_spending = processed_df.groupby('Category')['Amount'].sum().reset_index()
        
        fig = px.bar(category_spending, x='Category', y='Amount',
                    title="Spending by Category",
                    color='Amount',
                    color_continuous_scale='Blues')
        st.plotly_chart(fig, use_container_width=True)

def render_ai_financial_advisor():
    """Render the AI financial advisor page with voice chat"""
    if not st.session_state.current_startup or st.session_state.current_startup not in st.session_state.startups:
        st.warning("No startup selected. Please upload data first.")
        return
    
    startup_data = st.session_state.startups[st.session_state.current_startup]['profile']
    
    st.markdown("<h1 class='main-header'>AI Financial Advisor</h1>", unsafe_allow_html=True)
    
    # Chat container
    st.markdown("<div style='background-color: #f8f9fa; padding: 20px; border-radius: 10px; margin-bottom: 20px;'>", unsafe_allow_html=True)
    
    # Display chat history
    for message in st.session_state.chat_history:
        if message["role"] == "user":
            st.markdown(f"<div style='background-color: #e6f7ff; padding: 10px; border-radius: 10px; margin-bottom: 10px;'><strong>You:</strong> {message['content']}</div>", unsafe_allow_html=True)
        else:
            st.markdown(f"<div style='background-color: #f0f7ff; padding: 10px; border-radius: 10px; margin-bottom: 10px;'><strong>Financial Advisor:</strong> {message['content']}</div>", unsafe_allow_html=True)
            
            # Show play button for voice if it exists
            if 'audio' in message and message['audio']:
                try:
                    st.audio(message['audio'], format='audio/mp3')
                except Exception as e:
                    st.error(f"Error playing audio: {e}")
    
    # Input for new message
    col1, col2 = st.columns([5, 1])
    
    with col1:
        user_input = st.text_input("Ask a financial question", key="user_question")
    
    with col2:
        use_voice = st.checkbox("Voice", value=True)
    
    # Common financial questions
    st.markdown("### Common Questions")
    question_cols = st.columns(3)
    
    common_questions = [
        "How much runway do we have?",
        "When should we start fundraising?",
        "How can we optimize our burn rate?"
    ]
    
    for i, question in enumerate(common_questions):
        with question_cols[i % 3]:
            if st.button(question, key=f"q_{i}"):
                user_input = question
    
    # Process user input
    if user_input:
        # Add user message to chat history
        st.session_state.chat_history.append({"role": "user", "content": user_input})
        
        # Get AI response
        response = generate_ai_response(f"""
        You are a strategic financial advisor for startups. A founder asks:
        "{user_input}"

        Here's their current financial situation:
        - Stage: {startup_data['stage']}
        - Current cash: ${startup_data['cash']}
        - Monthly burn rate: ${startup_data['burn_rate']}
        - Monthly revenue: ${startup_data['revenue']}
        - Monthly growth rate: {startup_data['growth_rate'] * 100}%
        - Last funding: {startup_data['last_funding']}

        Provide concise, actionable advice.
        """)
        
        # Generate voice response if enabled
        audio_data = None
        if use_voice:
            with st.spinner("Generating voice response..."):
                audio_data = generate_voice_response(response)
        
        # Add AI response to chat history
        st.session_state.chat_history.append({
            "role": "assistant", 
            "content": response,
            "audio": audio_data
        })
        
        # Rerun to display updated chat
        st.rerun()
    
    st.markdown("</div>", unsafe_allow_html=True)
    
def main():
    # Create sidebar navigation
    create_sidebar()
    
    # Render the correct page based on session state
    if st.session_state.current_page == 'upload':
        render_upload_page()
    elif st.session_state.current_page == 'dashboard':
        render_financial_dashboard()
    elif st.session_state.current_page == 'simulator':
        # Pass the current startup's data
        render_decision_simulator(
            st.session_state.startups[st.session_state.current_startup]['profile']
        )
    elif st.session_state.current_page == 'monitoring':
        render_fund_monitoring()
    elif st.session_state.current_page == 'advisor':
        render_ai_financial_advisor()
        

if __name__ == "__main__":
    main()