import os
import fitz  # PyMuPDF
import spacy
import requests
from recruitment import models
from django.conf import settings

nlp = spacy.load("en_core_web_sm")

def extract_text_from_pdf(pdf_path):
    text = ""
    with fitz.open(pdf_path) as doc:
        for page in doc:
            text += page.get_text()
    return text

def extract_summary_from_pdf(pdf_path):
    if not os.path.exists(pdf_path):
        return {'error': 'File not found'}

    text = extract_text_from_pdf(pdf_path)
    doc = nlp(text)
    summary = {
        'name': doc.ents[0].text if doc.ents else '',
        'skills': [chunk.text for chunk in doc.noun_chunks if len(chunk.text.split()) > 1],
        'summary': text[:500]
    }
    return summary

def match_resume_with_job_description(resume, job_description,prompt=""):
    resume_doc = nlp(resume)
    job_doc = nlp(job_description)
    similarity = resume_doc.similarity(job_doc)
    return similarity