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
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
# server.py - Updated with enhanced text processing
from fastapi import FastAPI, File, UploadFile, Depends, HTTPException, Form
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
from typing import List
import json
from datetime import datetime

from database import get_db, Resume, JobDescription, Analysis, ActivityLog
from models import AnalysisResponse, AnalysisResult

# Use the enhanced text processor
try:
    from text_processor import EnhancedTextProcessor
    text_processor = EnhancedTextProcessor()
    print("✅ Using Enhanced Text Processor with optimized skills matching")
except ImportError:
    from text_processor import TextProcessor
    text_processor = TextProcessor()
    print("⚠️ Using Basic Text Processor")

app = FastAPI(
    title="Resume Relevance Check API", 
    version="2.0.0",
    description="AI-Powered Resume Analysis with Enhanced Matching"
)

# Enable CORS for frontend integration
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # In production, specify your frontend URL
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.on_event("startup")
async def startup_event():
    print("🚀 Resume Relevance Check API v2.0 is starting up...")
    print(f"📊 PDF Library: {getattr(text_processor, 'PDF_LIBRARY', 'Unknown')}")
    print(f"🧠 Skills Database: {len(getattr(text_processor, 'technical_skills', []))} skills loaded")

@app.get("/")
async def root():
    return {
        "message": "Resume Relevance Check API v2.0", 
        "status": "running",
        "features": ["Enhanced Skills Matching", "Multi-PDF Support", "Industry-Specific Analysis"],
        "pdf_support": hasattr(text_processor, 'PDF_LIBRARY')
    }

@app.post("/upload/resume/")
async def upload_resume(file: UploadFile = File(...), db: Session = Depends(get_db)):
    """Upload and process resume file with enhanced text extraction"""
    
    # Validate file type
    allowed_types = ["application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
    if file.content_type not in allowed_types:
        raise HTTPException(
            status_code=400, 
            detail="Only PDF and DOCX files are supported. Please convert your file to one of these formats."
        )
    
    try:
        # Read file content
        file_content = await file.read()
        
        # Extract text based on file type
        if file.content_type == "application/pdf":
            try:
                extracted_text = text_processor.extract_text_from_pdf(file_content)
                file_type = "pdf"
            except Exception as pdf_error:
                raise HTTPException(
                    status_code=500, 
                    detail=f"PDF processing failed: {str(pdf_error)}. Please try converting to DOCX format."
                )
        else:
            extracted_text = text_processor.extract_text_from_docx(file_content)
            file_type = "docx"
        
        # Validate extracted text
        if not extracted_text or len(extracted_text.strip()) < 50:
            raise HTTPException(
                status_code=400,
                detail="Could not extract sufficient text from the file. Please ensure the file contains readable text."
            )
        
        # Clean text with enhanced processing
        cleaned_text = text_processor.clean_text(extracted_text)
        
        # Extract skills for preview
        if hasattr(text_processor, 'extract_skills_enhanced'):
            skills_found = text_processor.extract_skills_enhanced(cleaned_text)
            skills_preview = f" | Skills detected: {', '.join(skills_found[:5])}{'...' if len(skills_found) > 5 else ''}"
        else:
            skills_preview = ""
        
        # Save to database
        db_resume = Resume(
            filename=file.filename,
            file_type=file_type,
            file_content=file_content,
            extracted_text=cleaned_text
        )
        db.add(db_resume)
        db.commit()
        db.refresh(db_resume)
        
        # Log activity with more details
        log_entry = ActivityLog(
            action="upload_resume",
            details=f"Uploaded resume: {file.filename} | Type: {file_type} | Text length: {len(cleaned_text)} chars{skills_preview}"
        )
        db.add(log_entry)
        db.commit()
        
        # Create enhanced preview
        preview_text = cleaned_text[:200] + "..." if len(cleaned_text) > 200 else cleaned_text
        
        return {
            "message": "Resume uploaded and processed successfully",
            "resume_id": db_resume.id,
            "filename": file.filename,
            "file_type": file_type,
            "extracted_text_preview": preview_text + skills_preview,
            "text_length": len(cleaned_text),
            "processing_status": "enhanced" if hasattr(text_processor, 'extract_skills_enhanced') else "basic"
        }
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing resume: {str(e)}")

@app.post("/upload/job-description/")
async def upload_job_description(file: UploadFile = File(...), db: Session = Depends(get_db)):
    """Upload and process job description file with enhanced text extraction"""
    
    allowed_types = [
        "application/pdf", 
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
        "text/plain"
    ]
    if file.content_type not in allowed_types:
        raise HTTPException(
            status_code=400, 
            detail="Only PDF, DOCX, and TXT files are supported for job descriptions."
        )
    
    try:
        # Read file content
        file_content = await file.read()
        
        # Extract text based on file type
        if file.content_type == "application/pdf":
            try:
                extracted_text = text_processor.extract_text_from_pdf(file_content)
                file_type = "pdf"
            except Exception as pdf_error:
                raise HTTPException(
                    status_code=500, 
                    detail=f"PDF processing failed: {str(pdf_error)}. Please try converting to DOCX or TXT format."
                )
        elif file.content_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
            extracted_text = text_processor.extract_text_from_docx(file_content)
            file_type = "docx"
        else:
            extracted_text = file_content.decode('utf-8')
            file_type = "txt"
        
        # Validate extracted text
        if not extracted_text or len(extracted_text.strip()) < 30:
            raise HTTPException(
                status_code=400,
                detail="Could not extract sufficient text from the job description. Please check the file content."
            )
        
        # Clean text with enhanced processing
        cleaned_text = text_processor.clean_text(extracted_text)
        
        # Extract required skills for preview
        if hasattr(text_processor, 'extract_skills_enhanced'):
            required_skills = text_processor.extract_skills_enhanced(cleaned_text)
            skills_preview = f" | Required skills: {', '.join(required_skills[:5])}{'...' if len(required_skills) > 5 else ''}"
        else:
            skills_preview = ""
        
        # Save to database
        db_job = JobDescription(
            filename=file.filename,
            file_type=file_type,
            file_content=file_content,
            extracted_text=cleaned_text
        )
        db.add(db_job)
        db.commit()
        db.refresh(db_job)
        
        # Log activity
        log_entry = ActivityLog(
            action="upload_job_description",
            details=f"Uploaded job description: {file.filename} | Type: {file_type} | Text length: {len(cleaned_text)} chars{skills_preview}"
        )
        db.add(log_entry)
        db.commit()
        
        # Create enhanced preview
        preview_text = cleaned_text[:200] + "..." if len(cleaned_text) > 200 else cleaned_text
        
        return {
            "message": "Job description uploaded and processed successfully",
            "job_description_id": db_job.id,
            "filename": file.filename,
            "file_type": file_type,
            "extracted_text_preview": preview_text + skills_preview,
            "text_length": len(cleaned_text),
            "processing_status": "enhanced" if hasattr(text_processor, 'extract_skills_enhanced') else "basic"
        }
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing job description: {str(e)}")

@app.post("/analyze/", response_model=AnalysisResponse)
async def analyze_resume(resume_id: int = Form(...), job_description_id: int = Form(...), db: Session = Depends(get_db)):
    """Analyze resume against job description with enhanced AI processing"""
    
    # Get resume and job description from database
    resume = db.query(Resume).filter(Resume.id == resume_id).first()
    job_desc = db.query(JobDescription).filter(JobDescription.id == job_description_id).first()
    
    if not resume or not job_desc:
        raise HTTPException(status_code=404, detail="Resume or job description not found")
    
    try:
        # Use enhanced analysis if available
        if hasattr(text_processor, 'calculate_enhanced_hard_score'):
            print("🔍 Running enhanced analysis...")
            
            # Enhanced hard score calculation
            hard_score, missing_skills = text_processor.calculate_enhanced_hard_score(
                resume.extracted_text, job_desc.extracted_text
            )
            
            # Enhanced semantic similarity
            soft_score = text_processor.calculate_semantic_score(
                resume.extracted_text, job_desc.extracted_text
            )
            
            # Enhanced feedback generation
            feedback = text_processor.generate_enhanced_feedback(
                hard_score, soft_score, missing_skills, 
                resume.extracted_text, job_desc.extracted_text
            )
        else:
            print("⚠️ Using basic analysis...")
            
            # Fallback to basic analysis
            hard_score, missing_skills = text_processor.calculate_hard_match_score(
                resume.extracted_text, job_desc.extracted_text
            )
            soft_score = text_processor.calculate_soft_match_score(
                resume.extracted_text, job_desc.extracted_text
            )
            feedback = text_processor.generate_feedback(hard_score, soft_score, missing_skills)
        
        # Calculate overall score (70% hard, 30% soft)
        overall_score = (hard_score * 0.7) + (soft_score * 0.3)
        
        # Determine verdict with enhanced thresholds
        if overall_score >= 75:
            verdict = "High"
        elif overall_score >= 50:
            verdict = "Medium"
        else:
            verdict = "Low"
        
        # Save analysis to database
        db_analysis = Analysis(
            resume_id=resume_id,
            job_description_id=job_description_id,
            hard_score=hard_score,
            soft_score=soft_score,
            overall_score=overall_score,
            verdict=verdict,
            missing_skills=json.dumps(missing_skills),
            feedback=feedback
        )
        db.add(db_analysis)
        db.commit()
        
        # Log detailed analysis activity
        log_entry = ActivityLog(
            action="analyze",
            details=f"Enhanced analysis completed | Resume: {resume.filename} vs Job: {job_desc.filename} | Score: {overall_score:.1f}% ({verdict}) | Missing skills: {len(missing_skills)}"
        )
        db.add(log_entry)
        db.commit()
        
        return AnalysisResponse(
            hard_score=round(hard_score, 2),
            soft_score=round(soft_score, 2),
            overall_score=round(overall_score, 2),
            verdict=verdict,
            missing_skills=missing_skills,
            feedback=feedback
        )
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error during enhanced analysis: {str(e)}")

@app.get("/analyses/", response_model=List[AnalysisResult])
async def get_analyses(db: Session = Depends(get_db), skip: int = 0, limit: int = 100):
    """Get analysis history with enhanced details"""
    analyses = db.query(Analysis).join(Resume).offset(skip).limit(limit).all()
    
    results = []
    for analysis in analyses:
        resume = db.query(Resume).filter(Resume.id == analysis.resume_id).first()
        results.append(AnalysisResult(
            id=analysis.id,
            resume_filename=resume.filename,
            hard_score=analysis.hard_score,
            soft_score=analysis.soft_score,
            overall_score=analysis.overall_score,
            verdict=analysis.verdict,
            missing_skills=json.loads(analysis.missing_skills),
            feedback=analysis.feedback,
            analysis_date=analysis.analysis_date
        ))
    
    return results

@app.get("/analyses/{analysis_id}")
async def get_analysis_details(analysis_id: int, db: Session = Depends(get_db)):
    """Get detailed analysis by ID with enhanced information"""
    analysis = db.query(Analysis).filter(Analysis.id == analysis_id).first()
    if not analysis:
        raise HTTPException(status_code=404, detail="Analysis not found")
    
    resume = db.query(Resume).filter(Resume.id == analysis.resume_id).first()
    job_desc = db.query(JobDescription).filter(JobDescription.id == analysis.job_description_id).first()
    
    return {
        "id": analysis.id,
        "resume_filename": resume.filename,
        "job_description_filename": job_desc.filename,
        "hard_score": analysis.hard_score,
        "soft_score": analysis.soft_score,
        "overall_score": analysis.overall_score,
        "verdict": analysis.verdict,
        "missing_skills": json.loads(analysis.missing_skills),
        "feedback": analysis.feedback,
        "analysis_date": analysis.analysis_date,
        "processing_type": "enhanced" if hasattr(text_processor, 'calculate_enhanced_hard_score') else "basic"
    }

@app.get("/resumes/")
async def get_resumes(db: Session = Depends(get_db)):
    """Get all uploaded resumes with enhanced details"""
    resumes = db.query(Resume).all()
    return [
        {
            "id": r.id, 
            "filename": r.filename, 
            "file_type": r.file_type,
            "upload_date": r.upload_date,
            "text_length": len(r.extracted_text) if r.extracted_text else 0
        } for r in resumes
    ]

@app.get("/job-descriptions/")
async def get_job_descriptions(db: Session = Depends(get_db)):
    """Get all uploaded job descriptions with enhanced details"""
    job_descs = db.query(JobDescription).all()
    return [
        {
            "id": j.id, 
            "filename": j.filename, 
            "file_type": j.file_type,
            "upload_date": j.upload_date,
            "text_length": len(j.extracted_text) if j.extracted_text else 0
        } for j in job_descs
    ]

@app.get("/system/status")
async def get_system_status():
    """Get system status and capabilities"""
    return {
        "api_version": "2.0.0",
        "enhanced_processing": hasattr(text_processor, 'calculate_enhanced_hard_score'),
        "pdf_support": hasattr(text_processor, 'extract_text_from_pdf'),
        "pdf_library": getattr(text_processor, 'PDF_LIBRARY', 'None'),
        "skills_database_size": len(getattr(text_processor, 'technical_skills', [])),
        "supported_formats": ["PDF", "DOCX", "TXT"],
        "features": [
            "Enhanced Skills Matching",
            "Industry-Specific Analysis", 
            "Multi-Library PDF Support",
            "Semantic Similarity Analysis",
            "Personalized Feedback Generation"
        ]
    }

@app.delete("/resumes/{resume_id}")
async def delete_resume(resume_id: int, db: Session = Depends(get_db)):
    """Delete a resume and associated analyses"""
    resume = db.query(Resume).filter(Resume.id == resume_id).first()
    if not resume:
        raise HTTPException(status_code=404, detail="Resume not found")
    
    # Delete associated analyses
    db.query(Analysis).filter(Analysis.resume_id == resume_id).delete()
    
    # Delete resume
    db.delete(resume)
    db.commit()
    
    # Log deletion
    log_entry = ActivityLog(
        action="delete_resume",
        details=f"Deleted resume: {resume.filename} and associated analyses"
    )
    db.add(log_entry)
    db.commit()
    
    return {"message": "Resume and associated analyses deleted successfully"}

@app.delete("/analyses/{analysis_id}")
async def delete_analysis(analysis_id: int, db: Session = Depends(get_db)):
    """Delete a specific analysis"""
    analysis = db.query(Analysis).filter(Analysis.id == analysis_id).first()
    if not analysis:
        raise HTTPException(status_code=404, detail="Analysis not found")
    
    db.delete(analysis)
    db.commit()
    
    # Log deletion
    log_entry = ActivityLog(
        action="delete_analysis",
        details=f"Deleted analysis ID: {analysis_id}"
    )
    db.add(log_entry)
    db.commit()
    
    return {"message": "Analysis deleted successfully"}

@app.get("/analytics/summary")
async def get_analytics_summary(db: Session = Depends(get_db)):
    """Get system usage analytics"""
    total_resumes = db.query(Resume).count()
    total_job_descriptions = db.query(JobDescription).count()
    total_analyses = db.query(Analysis).count()
    
    # Verdict distribution
    verdicts = db.query(Analysis.verdict).all()
    verdict_counts = {"High": 0, "Medium": 0, "Low": 0}
    for (verdict,) in verdicts:
        if verdict in verdict_counts:
            verdict_counts[verdict] += 1
    
    # Average scores
    scores = db.query(Analysis.overall_score).all()
    avg_score = sum(score[0] for score in scores) / len(scores) if scores else 0
    
    return {
        "total_resumes": total_resumes,
        "total_job_descriptions": total_job_descriptions,
        "total_analyses": total_analyses,
        "verdict_distribution": verdict_counts,
        "average_score": round(avg_score, 2),
        "processing_type": "enhanced" if hasattr(text_processor, 'calculate_enhanced_hard_score') else "basic"
    }

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)