File size: 27,601 Bytes
33e8c89
035c4af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33e8c89
035c4af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33e8c89
035c4af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33e8c89
035c4af
 
 
 
 
 
33e8c89
035c4af
 
 
 
 
 
 
 
 
 
 
33e8c89
035c4af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33e8c89
035c4af
 
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
import streamlit as st
import json
import pandas as pd
from agents.orchestrator import ProfileOrchestrator
from agents.scraper_agent import ScraperAgent
from agents.content_agent import ContentAgent
import plotly.express as px
import plotly.graph_objects as go
from datetime import datetime

# Configure Streamlit page
st.set_page_config(
    page_title="πŸš€ LinkedIn Profile Enhancer",
    page_icon="πŸš€",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS for better styling
st.markdown("""
<style>
    .main-header {
        background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
        padding: 2rem;
        border-radius: 10px;
        color: white;
        text-align: center;
        margin-bottom: 2rem;
    }
    
    .metric-card {
        background: #f8f9fa;
        padding: 1rem;
        border-radius: 8px;
        border-left: 4px solid #667eea;
        margin: 0.5rem 0;
    }
    
    .success-card {
        background: #d4edda;
        padding: 1rem;
        border-radius: 8px;
        border-left: 4px solid #28a745;
        margin: 0.5rem 0;
    }
    
    .warning-card {
        background: #fff3cd;
        padding: 1rem;
        border-radius: 8px;
        border-left: 4px solid #ffc107;
        margin: 0.5rem 0;
    }
    
    .info-card {
        background: #e7f3ff;
        padding: 1rem;
        border-radius: 8px;
        border-left: 4px solid #17a2b8;
        margin: 0.5rem 0;
    }
    
    .stTabs > div > div > div > div {
        padding: 1rem;
    }
    
    .profile-section {
        background: white;
        padding: 1.5rem;
        border-radius: 10px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        margin: 1rem 0;
    }
</style>
""", unsafe_allow_html=True)

def initialize_session_state():
    """Initialize session state variables"""
    if 'orchestrator' not in st.session_state:
        st.session_state.orchestrator = ProfileOrchestrator()
    if 'analysis_results' not in st.session_state:
        st.session_state.analysis_results = None
    if 'profile_data' not in st.session_state:
        st.session_state.profile_data = None
    if 'suggestions' not in st.session_state:
        st.session_state.suggestions = None
    if 'current_url' not in st.session_state:
        st.session_state.current_url = None

def clear_results_if_url_changed(linkedin_url):
    """Clear cached results if URL has changed"""
    if st.session_state.current_url != linkedin_url:
        st.session_state.analysis_results = None
        st.session_state.profile_data = None
        st.session_state.suggestions = None
        st.session_state.current_url = linkedin_url
        st.cache_data.clear()  # Clear any Streamlit cache
        print(f"πŸ”„ URL changed to: {linkedin_url} - Clearing cached data")

def create_header():
    """Create the main header"""
    st.markdown("""
    <div class="main-header">
        <h1>πŸš€ LinkedIn Profile Enhancer</h1>
        <p style="font-size: 1.2em; margin: 1rem 0;">AI-powered LinkedIn profile analysis and enhancement suggestions</p>
        <div style="display: flex; justify-content: center; gap: 2rem; margin-top: 1rem;">
            <div style="text-align: center;">
                <div style="font-size: 2em;">πŸ”</div>
                <div>Real Scraping</div>
            </div>
            <div style="text-align: center;">
                <div style="font-size: 2em;">πŸ€–</div>
                <div>AI Analysis</div>
            </div>
            <div style="text-align: center;">
                <div style="font-size: 2em;">🎯</div>
                <div>Smart Suggestions</div>
            </div>
            <div style="text-align: center;">
                <div style="font-size: 2em;">πŸ“Š</div>
                <div>Data Insights</div>
            </div>
        </div>
    </div>
    """, unsafe_allow_html=True)

def create_sidebar():
    """Create the sidebar with input controls"""
    with st.sidebar:
        st.header("πŸ“ Configuration")
        
        # LinkedIn URL input
        linkedin_url = st.text_input(
            "πŸ”— LinkedIn Profile URL",
            placeholder="https://linkedin.com/in/your-profile",
            help="Enter the full LinkedIn profile URL to analyze"
        )
        
        # Job description input
        job_description = st.text_area(
            "🎯 Target Job Description (Optional)",
            placeholder="Paste the job description here for tailored suggestions...",
            height=150,
            help="Include job description for personalized optimization"
        )
        
        # API Status
        st.subheader("πŸ”Œ API Status")
        
        # Test API connections
        if st.button("πŸ”„ Test Connections"):
            with st.spinner("Testing API connections..."):
                # Test Apify
                try:
                    scraper = ScraperAgent()
                    apify_status = scraper.test_apify_connection()
                    if apify_status:
                        st.success("βœ… Apify: Connected")
                    else:
                        st.error("❌ Apify: Failed")
                except Exception as e:
                    st.error(f"❌ Apify: Error - {str(e)}")
                
                # Test OpenAI
                try:
                    content_agent = ContentAgent()
                    openai_status = content_agent.test_openai_connection()
                    if openai_status:
                        st.success("βœ… OpenAI: Connected")
                    else:
                        st.error("❌ OpenAI: Failed")
                except Exception as e:
                    st.error(f"❌ OpenAI: Error - {str(e)}")
        
        # Examples
        st.subheader("πŸ’‘ Example URLs")
        example_urls = [
            "https://linkedin.com/in/example-profile",
            "https://www.linkedin.com/in/sample-user"
        ]
        
        for url in example_urls:
            if st.button(f"πŸ“‹ {url.split('/')[-1]}", key=url):
                st.session_state.example_url = url
        
        return linkedin_url, job_description

def create_metrics_display(analysis):
    """Create metrics display"""
    col1, col2, col3, col4 = st.columns(4)
    
    with col1:
        st.metric(
            "πŸ“ˆ Completeness Score",
            f"{analysis.get('completeness_score', 0):.1f}%",
            delta=None
        )
    
    with col2:
        rating = analysis.get('overall_rating', 'Unknown')
        st.metric(
            "⭐ Overall Rating",
            rating,
            delta=None
        )
    
    with col3:
        st.metric(
            "🎯 Job Match Score",
            f"{analysis.get('job_match_score', 0):.1f}%",
            delta=None
        )
    
    with col4:
        keywords = analysis.get('keyword_analysis', {})
        found_count = len(keywords.get('found_keywords', []))
        st.metric(
            "πŸ” Keywords Found",
            found_count,
            delta=None
        )

def create_analysis_charts(analysis):
    """Create analysis charts"""
    col1, col2 = st.columns(2)
    
    with col1:
        # Completeness breakdown
        scores = {
            'Profile Info': 20,
            'About Section': 25,
            'Experience': 25,
            'Skills': 15,
            'Education': 15
        }
        
        fig_pie = px.pie(
            values=list(scores.values()),
            names=list(scores.keys()),
            title="Profile Section Weights",
            color_discrete_sequence=px.colors.qualitative.Set3
        )
        fig_pie.update_layout(height=400)
        st.plotly_chart(fig_pie, use_container_width=True)
    
    with col2:
        # Score comparison
        current_score = analysis.get('completeness_score', 0)
        target_score = 90
        
        fig_gauge = go.Figure(go.Indicator(
            mode = "gauge+number+delta",
            value = current_score,
            domain = {'x': [0, 1], 'y': [0, 1]},
            title = {'text': "Profile Completeness"},
            delta = {'reference': target_score, 'increasing': {'color': "green"}},
            gauge = {
                'axis': {'range': [None, 100]},
                'bar': {'color': "darkblue"},
                'steps': [
                    {'range': [0, 50], 'color': "lightgray"},
                    {'range': [50, 80], 'color': "gray"},
                    {'range': [80, 100], 'color': "lightgreen"}
                ],
                'threshold': {
                    'line': {'color': "red", 'width': 4},
                    'thickness': 0.75,
                    'value': 90
                }
            }
        ))
        fig_gauge.update_layout(height=400)
        st.plotly_chart(fig_gauge, use_container_width=True)

def display_profile_data(profile_data):
    """Display scraped profile data in a structured format"""
    if not profile_data:
        st.warning("No profile data available")
        return
    
    # Profile Header with Image
    st.subheader("πŸ‘€ Profile Overview")
    
    # Create columns for profile image and basic info
    col1, col2, col3 = st.columns([1, 2, 2])
    
    with col1:
        # Display profile image
        profile_image = profile_data.get('profile_image_hq') or profile_data.get('profile_image')
        if profile_image:
            st.image(profile_image, width=150, caption="Profile Picture")
        else:
            st.markdown("""
            <div style="width: 150px; height: 150px; background-color: #f0f0f0; border-radius: 50%; 
                        display: flex; align-items: center; justify-content: center; font-size: 48px;">
                πŸ‘€
            </div>
            """, unsafe_allow_html=True)
    
    with col2:
        st.markdown(f"""
        <div class="info-card">
            <strong>Name:</strong> {profile_data.get('name', 'N/A')}<br>
            <strong>Headline:</strong> {profile_data.get('headline', 'N/A')}<br>
            <strong>Location:</strong> {profile_data.get('location', 'N/A')}<br>
            <strong>Connections:</strong> {profile_data.get('connections', 'N/A')}<br>
            <strong>Followers:</strong> {profile_data.get('followers', 'N/A')}
        </div>
        """, unsafe_allow_html=True)
    
    with col3:
        st.markdown(f"""
        <div class="info-card">
            <strong>Current Job:</strong> {profile_data.get('job_title', 'N/A')}<br>
            <strong>Company:</strong> {profile_data.get('company_name', 'N/A')}<br>
            <strong>Industry:</strong> {profile_data.get('company_industry', 'N/A')}<br>
            <strong>Email:</strong> {profile_data.get('email', 'N/A')}<br>
            <strong>Profile URL:</strong> <a href="{profile_data.get('url', '#')}" target="_blank">View Profile</a>
        </div>
        """, unsafe_allow_html=True)
    
    # About Section
    if profile_data.get('about'):
        st.subheader("πŸ“ About Section")
        st.markdown(f"""
        <div class="profile-section">
            {profile_data.get('about', 'No about section available')}
        </div>
        """, unsafe_allow_html=True)
    
    # Experience
    if profile_data.get('experience'):
        st.subheader("πŸ’Ό Experience")
        for i, exp in enumerate(profile_data.get('experience', [])):
            with st.expander(f"{exp.get('title', 'Position')} at {exp.get('company', 'Company')}", expanded=i==0):
                col1, col2 = st.columns([2, 1])
                with col1:
                    st.write(f"**Duration:** {exp.get('duration', 'N/A')}")
                    st.write(f"**Location:** {exp.get('location', 'N/A')}")
                    if exp.get('description'):
                        st.write("**Description:**")
                        st.write(exp.get('description'))
                with col2:
                    st.write(f"**Current Role:** {'Yes' if exp.get('is_current') else 'No'}")
    
    # Skills
    if profile_data.get('skills'):
        st.subheader("πŸ› οΈ Skills")
        skills = profile_data.get('skills', [])
        if skills:
            # Create a DataFrame for better display
            skills_df = pd.DataFrame({'Skills': skills})
            st.dataframe(skills_df, use_container_width=True)
    
    # Education
    if profile_data.get('education'):
        st.subheader("πŸŽ“ Education")
        for edu in profile_data.get('education', []):
            st.markdown(f"""
            <div class="info-card">
                <strong>{edu.get('degree', 'Degree')}</strong><br>
                {edu.get('school', 'School')} | {edu.get('field', 'Field')}<br>
                <em>{edu.get('year', 'Year')}</em>
            </div>
            """, unsafe_allow_html=True)
    
    # Raw Data (collapsible)
    with st.expander("πŸ” Raw JSON Data"):
        st.json(profile_data)

def display_analysis_results(analysis):
    """Display analysis results"""
    if not analysis:
        st.warning("No analysis results available")
        return
    
    # Metrics
    create_metrics_display(analysis)
    
    # Charts
    st.subheader("πŸ“Š Analysis Visualization")
    create_analysis_charts(analysis)
    
    # Strengths and Weaknesses
    col1, col2 = st.columns(2)
    
    with col1:
        st.subheader("🌟 Profile Strengths")
        strengths = analysis.get('strengths', [])
        if strengths:
            for strength in strengths:
                st.markdown(f"""
                <div class="success-card">
                    βœ… {strength}
                </div>
                """, unsafe_allow_html=True)
        else:
            st.info("No specific strengths identified")
    
    with col2:
        st.subheader("πŸ”§ Areas for Improvement")
        weaknesses = analysis.get('weaknesses', [])
        if weaknesses:
            for weakness in weaknesses:
                st.markdown(f"""
                <div class="warning-card">
                    πŸ”Έ {weakness}
                </div>
                """, unsafe_allow_html=True)
        else:
            st.success("No major areas for improvement identified")
    
    # Keyword Analysis
    keyword_analysis = analysis.get('keyword_analysis', {})
    if keyword_analysis:
        st.subheader("πŸ” Keyword Analysis")
        
        col1, col2 = st.columns(2)
        with col1:
            found_keywords = keyword_analysis.get('found_keywords', [])
            if found_keywords:
                st.write("**Keywords Found:**")
                st.write(", ".join(found_keywords[:10]))
        
        with col2:
            missing_keywords = keyword_analysis.get('missing_keywords', [])
            if missing_keywords:
                st.write("**Missing Keywords:**")
                st.write(", ".join(missing_keywords[:5]))

def generate_suggestions_markdown(suggestions, profile_data=None):
    """Generate markdown content from suggestions"""
    if not suggestions:
        return "# LinkedIn Profile Enhancement Suggestions\n\nNo suggestions available."
    
    # Get profile name for personalization
    profile_name = profile_data.get('name', 'Your Profile') if profile_data else 'Your Profile'
    current_date = datetime.now().strftime("%B %d, %Y")
    
    markdown_content = f"""# LinkedIn Profile Enhancement Suggestions

**Profile:** {profile_name}  
**Generated on:** {current_date}  
**Powered by:** LinkedIn Profile Enhancer AI

---

## πŸ“‹ Table of Contents
"""
    
    # Add table of contents
    toc_items = []
    for category in suggestions.keys():
        if category == 'ai_generated_content':
            toc_items.append("- [πŸ€– AI-Generated Content Suggestions](#ai-generated-content-suggestions)")
        else:
            category_name = category.replace('_', ' ').title()
            toc_items.append(f"- [πŸ“‹ {category_name}](#{category.replace('_', '-').lower()})")
    
    markdown_content += "\n".join(toc_items) + "\n\n---\n\n"
    
    # Add suggestions content
    for category, items in suggestions.items():
        if category == 'ai_generated_content':
            markdown_content += "## πŸ€– AI-Generated Content Suggestions\n\n"
            ai_content = items if isinstance(items, dict) else {}
            
            # Headlines
            if 'ai_headlines' in ai_content and ai_content['ai_headlines']:
                markdown_content += "### ✨ Professional Headlines\n\n"
                for i, headline in enumerate(ai_content['ai_headlines'], 1):
                    cleaned_headline = headline.strip('"').replace('\\"', '"')
                    if cleaned_headline.startswith(('1.', '2.', '3.', '4.', '5.')):
                        cleaned_headline = cleaned_headline[2:].strip()
                    markdown_content += f"{i}. {cleaned_headline}\n"
                markdown_content += "\n"
            
            # About Section
            if 'ai_about_section' in ai_content and ai_content['ai_about_section']:
                markdown_content += "### πŸ“ Enhanced About Section\n\n"
                markdown_content += f"```\n{ai_content['ai_about_section']}\n```\n\n"
            
            # Experience Descriptions
            if 'ai_experience_descriptions' in ai_content and ai_content['ai_experience_descriptions']:
                markdown_content += "### πŸ’Ό Experience Description Ideas\n\n"
                for desc in ai_content['ai_experience_descriptions']:
                    markdown_content += f"- {desc}\n"
                markdown_content += "\n"
        else:
            # Standard categories
            category_name = category.replace('_', ' ').title()
            markdown_content += f"## πŸ“‹ {category_name}\n\n"
            if isinstance(items, list):
                for item in items:
                    markdown_content += f"- {item}\n"
            else:
                markdown_content += f"- {items}\n"
            markdown_content += "\n"
    
    # Add footer
    markdown_content += """---

## πŸ“š Implementation Tips

### Getting Started
1. **Prioritize High-Impact Changes**: Start with headline and about section improvements
2. **Use Keywords Strategically**: Incorporate industry-relevant keywords naturally
3. **Maintain Authenticity**: Ensure all changes reflect your genuine experience and personality
4. **Regular Updates**: Keep your profile fresh with recent achievements and experiences

### Best Practices
- **Professional Photo**: Use a high-quality, professional headshot
- **Active Engagement**: Regularly share industry insights and engage with your network
- **Skills Endorsements**: Ask colleagues to endorse your key skills
- **Recommendations**: Request recommendations from supervisors and colleagues
- **Content Strategy**: Share articles, insights, and achievements regularly

### Measuring Success
- Monitor profile views and connection requests
- Track engagement on your posts and content
- Observe changes in recruiter outreach
- Measure network growth and quality

---

*This report was generated by LinkedIn Profile Enhancer AI. For best results, implement changes gradually and monitor their impact on your profile performance.*

**Need Help?** Contact support or revisit the LinkedIn Profile Enhancer tool for updated suggestions.
"""
    
    return markdown_content

def display_suggestions(suggestions):
    """Display enhancement suggestions with download option"""
    if not suggestions:
        st.warning("No suggestions available")
        return
    
    # Add download button at the top
    col1, col2 = st.columns([1, 4])
    
    with col1:
        # Generate markdown content
        markdown_content = generate_suggestions_markdown(
            suggestions, 
            st.session_state.get('profile_data')
        )
        
        # Create filename with timestamp
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        profile_name = ""
        if st.session_state.get('profile_data'):
            name = st.session_state.profile_data.get('name', '')
            if name:
                # Clean name for filename
                profile_name = "".join(c for c in name if c.isalnum() or c in (' ', '_')).rstrip()
                profile_name = profile_name.replace(' ', '_') + "_"
        
        filename = f"linkedin_suggestions_{profile_name}{timestamp}.md"
        
        st.download_button(
            label="πŸ“₯ Download Suggestions",
            data=markdown_content,
            file_name=filename,
            mime="text/markdown",
            help="Download all suggestions as a markdown file",
            use_container_width=True
        )
    
    with col2:
        st.markdown("*πŸ’‘ Click the download button to save all suggestions as a markdown file for easy reference and implementation.*")
    
    st.markdown("---")
    
    # Display suggestions as before
    for category, items in suggestions.items():
        if category == 'ai_generated_content':
            st.subheader("πŸ€– AI-Generated Content Suggestions")
            ai_content = items if isinstance(items, dict) else {}
            
            # Headlines
            if 'ai_headlines' in ai_content and ai_content['ai_headlines']:
                st.write("**✨ Professional Headlines:**")
                for i, headline in enumerate(ai_content['ai_headlines'], 1):
                    cleaned_headline = headline.strip('"').replace('\\"', '"')
                    if cleaned_headline.startswith(('1.', '2.', '3.', '4.', '5.')):
                        cleaned_headline = cleaned_headline[2:].strip()
                    st.write(f"{i}. {cleaned_headline}")
                st.write("")
            
            # About Section
            if 'ai_about_section' in ai_content and ai_content['ai_about_section']:
                st.write("**πŸ“ Enhanced About Section:**")
                st.code(ai_content['ai_about_section'], language='text')
                st.write("")
            
            # Experience Descriptions
            if 'ai_experience_descriptions' in ai_content and ai_content['ai_experience_descriptions']:
                st.write("**πŸ’Ό Experience Description Ideas:**")
                for desc in ai_content['ai_experience_descriptions']:
                    st.write(f"β€’ {desc}")
                st.write("")
        else:
            # Standard categories
            category_name = category.replace('_', ' ').title()
            st.subheader(f"πŸ“‹ {category_name}")
            if isinstance(items, list):
                for item in items:
                    st.write(f"β€’ {item}")
            else:
                st.write(f"β€’ {items}")
            st.write("")

def main():
    """Main Streamlit application"""
    initialize_session_state()
    create_header()
    
    # Sidebar
    linkedin_url, job_description = create_sidebar()
    
    # Main content
    if st.button("πŸš€ Enhance Profile", type="primary", use_container_width=True):
        if not linkedin_url.strip():
            st.error("Please enter a LinkedIn profile URL")
        elif not any(pattern in linkedin_url.lower() for pattern in ['linkedin.com/in/', 'www.linkedin.com/in/']):
            st.error("Please enter a valid LinkedIn profile URL")
        else:
            # Clear cached data if URL has changed
            clear_results_if_url_changed(linkedin_url)
            
            with st.spinner("πŸ” Analyzing LinkedIn profile..."):
                try:
                    st.info(f"πŸ” Extracting data from: {linkedin_url}")
                    
                    # Get profile data and analysis (force fresh extraction)
                    profile_data = st.session_state.orchestrator.scraper.extract_profile_data(linkedin_url)
                    
                    st.info(f"βœ… Profile data extracted for: {profile_data.get('name', 'Unknown')}")
                    
                    analysis = st.session_state.orchestrator.analyzer.analyze_profile(profile_data, job_description)
                    suggestions = st.session_state.orchestrator.content_generator.generate_suggestions(analysis, job_description)
                    
                    # Store in session state
                    st.session_state.profile_data = profile_data
                    st.session_state.analysis_results = analysis
                    st.session_state.suggestions = suggestions
                    
                    st.success("βœ… Profile analysis completed!")
                    
                except Exception as e:
                    st.error(f"❌ Error analyzing profile: {str(e)}")
    
    # Display results if available
    if st.session_state.profile_data or st.session_state.analysis_results:
        st.markdown("---")
        
        # Create tabs for different views
        tab1, tab2, tab3, tab4 = st.tabs(["πŸ“Š Analysis", "πŸ” Scraped Data", "🎯 Suggestions", "πŸ“ˆ Implementation"])
        
        with tab1:
            st.header("πŸ“Š Profile Analysis")
            if st.session_state.analysis_results:
                display_analysis_results(st.session_state.analysis_results)
            else:
                st.info("No analysis results available yet")
        
        with tab2:
            st.header("πŸ” Scraped Profile Data")
            if st.session_state.profile_data:
                display_profile_data(st.session_state.profile_data)
            else:
                st.info("No profile data available yet")
        
        with tab3:
            st.header("🎯 Enhancement Suggestions")
            if st.session_state.suggestions:
                display_suggestions(st.session_state.suggestions)
            else:
                st.info("No suggestions available yet")
        
        with tab4:
            st.header("πŸ“ˆ Implementation Roadmap")
            if st.session_state.analysis_results:
                recommendations = st.session_state.analysis_results.get('recommendations', [])
                if recommendations:
                    st.subheader("🎯 Priority Actions")
                    for i, rec in enumerate(recommendations[:5], 1):
                        st.markdown(f"""
                        <div class="metric-card">
                            <strong>{i}.</strong> {rec}
                        </div>
                        """, unsafe_allow_html=True)
                
                st.subheader("πŸ“Š General Best Practices")
                best_practices = [
                    "Update your profile regularly with new achievements",
                    "Use professional keywords relevant to your industry",
                    "Engage with your network by sharing valuable content",
                    "Ask for recommendations from colleagues and clients",
                    "Monitor profile views and connection requests"
                ]
                
                for practice in best_practices:
                    st.markdown(f"""
                    <div class="info-card">
                        πŸ”Έ {practice}
                    </div>
                    """, unsafe_allow_html=True)
            else:
                st.info("Complete the analysis first to see implementation suggestions")
    
    # Footer
    st.markdown("---")
    st.markdown("""
    <div style="text-align: center; color: #666; margin-top: 2rem;">
        <p>πŸš€ <strong>LinkedIn Profile Enhancer</strong> | Powered by AI | Data scraped with respect to LinkedIn's ToS</p>
        <p>Built with ❀️ using Streamlit, OpenAI GPT-4o-mini, and Apify</p>
    </div>
    """, unsafe_allow_html=True)

if __name__ == "__main__":
    main()