from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from lxml import etree
import unicodedata

def add_section_border(paragraph):
    # Define the Word namespace
    namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    nsmap = {"w": namespace}

    # Get the paragraph XML element
    p_xml = paragraph._element

    # Create the border XML structure
    pPr = p_xml.find("w:pPr", namespaces=nsmap)
    if pPr is None:
        pPr = etree.SubElement(p_xml, "{%s}pPr" % namespace)

    pbdr = pPr.find("w:pBdr", namespaces=nsmap) or etree.SubElement(pPr, "{%s}pBdr" % namespace)

    for border_type in ["top", "left", "bottom", "right"]:
        border = pbdr.find(f"w:{border_type}", namespaces=nsmap) or etree.SubElement(pbdr, f"{{%s}}{border_type}" % namespace)
        border.set("{%s}val" % namespace, "single")
        border.set("{%s}sz" % namespace, "12")
        border.set("{%s}space" % namespace, "1")
        border.set("{%s}color" % namespace, "auto")


def generate_resume_from_template(data, output_file):
    doc = Document()

    # Title Section: Name and Job Title
    name_title = doc.add_heading(level=1)
    name_run = name_title.add_run(f"{data['name']}")
    name_run.bold = True
    name_run.font.size = Pt(20)
    name_title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

    job_title = doc.add_paragraph(data['job_title'])
    job_title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    job_title.runs[0].italic = True
    job_title.runs[0].font.size = Pt(14)

    # Horizontal Line
    doc.add_paragraph().add_run("_" * 100).bold = True

    # Contact Information
    contact_info = doc.add_heading("Contact Information", level=2)
    add_section_border(contact_info)
    doc.add_paragraph(f"📍 Address: {unicodedata.normalize('NFC', data['address'])}")
    doc.add_paragraph(f"📞 Phone: {data['phone']}")
    doc.add_paragraph(f"📧 Email: {data['email']}")

    # Career Objective
    career_section = doc.add_heading("Career Objective", level=2)
    add_section_border(career_section)
    doc.add_paragraph(data['career_objective'])

    # Educational Qualifications
    edu_section = doc.add_heading("Educational Qualifications", level=2)
    add_section_border(edu_section)
    table = doc.add_table(rows=1, cols=3)
    hdr_cells = table.rows[0].cells
    hdr_cells[0].text = "Degree"
    hdr_cells[1].text = "Institute"
    hdr_cells[2].text = "Year"

    for edu in data['education']:
        row_cells = table.add_row().cells
        row_cells[0].text = edu['degree']
        row_cells[1].text = edu['institute']
        row_cells[2].text = edu['year']

    # Technical Skills
    skill_section = doc.add_heading("Technical Skills", level=2)
    add_section_border(skill_section)
    for skill in data['skills']:
        doc.add_paragraph(f"{skill}", style="List Bullet")

    # Projects Undertaken
    project_section = doc.add_heading("Projects Undertaken", level=2)
    add_section_border(project_section)
    for project in data['projects']:
        text = unicodedata.normalize("NFC", f"{project['name']}")
        doc.add_paragraph(text, style="List Bullet")
        doc.add_paragraph(f"  {project['description']}")

    # Footer
    section = doc.sections[-1]
    footer = section.footer
    footer_para = footer.paragraphs[0]
    footer_para.text = "Generated by AI Resume Builder"
    footer_para.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

    # Save the document
    doc.save(output_file)
