from pathlib import Path
from docx import Document
from docx.shared import Inches, Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.section import WD_SECTION
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from reportlab.lib import colors
from reportlab.lib.enums import TA_LEFT
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak, KeepTogether
from xml.sax.saxutils import escape

ROOT = Path(r'C:\xampp\htdocs\Sewing-Project')
OUT = ROOT / 'output'
DOCX = OUT / 'Mohamed_Adham_Ismail_ATS_Resume.docx'
OUT.mkdir(exist_ok=True)
doc = Document()
sec = doc.sections[0]
sec.top_margin = Inches(.55)
sec.bottom_margin = Inches(.55)
sec.left_margin = Inches(.72)
sec.right_margin = Inches(.72)

styles = doc.styles
normal = styles['Normal']
normal.font.name = 'Arial'
normal.font.size = Pt(9.5)
normal.font.color.rgb = RGBColor(42, 51, 64)
normal.paragraph_format.space_after = Pt(2)
normal.paragraph_format.line_spacing = 1.04
for name in ('Title', 'Heading 1', 'Heading 2'):
    styles[name].font.name = 'Arial'
styles['Title'].font.size = Pt(22)
styles['Title'].font.bold = True
styles['Title'].font.color.rgb = RGBColor(18, 39, 67)
styles['Title'].paragraph_format.space_after = Pt(1)
styles['Heading 1'].font.size = Pt(11.5)
styles['Heading 1'].font.bold = True
styles['Heading 1'].font.color.rgb = RGBColor(18, 74, 119)
styles['Heading 1'].paragraph_format.space_before = Pt(8)
styles['Heading 1'].paragraph_format.space_after = Pt(3)
styles['Heading 2'].font.size = Pt(10)
styles['Heading 2'].font.bold = True
styles['Heading 2'].font.color.rgb = RGBColor(25, 40, 58)
styles['Heading 2'].paragraph_format.space_before = Pt(4)
styles['Heading 2'].paragraph_format.space_after = Pt(0)

def para(text='', bold=False, color=None, size=None, after=2, before=0, align=None):
    p = doc.add_paragraph()
    p.paragraph_format.space_after = Pt(after)
    p.paragraph_format.space_before = Pt(before)
    if align is not None: p.alignment = align
    r = p.add_run(text)
    r.bold = bold
    if color: r.font.color.rgb = RGBColor(*color)
    if size: r.font.size = Pt(size)
    r.font.name = 'Arial'
    return p

def section(title):
    doc.add_heading(title, level=1)

def bullet(text):
    p = doc.add_paragraph(style='List Bullet')
    p.paragraph_format.left_indent = Inches(.18)
    p.paragraph_format.first_line_indent = Inches(-.12)
    p.paragraph_format.space_after = Pt(1.4)
    p.paragraph_format.line_spacing = 1.02
    r = p.add_run(text)
    r.font.name = 'Arial'; r.font.size = Pt(9.2); r.font.color.rgb = RGBColor(42,51,64)
    return p

para('MOHAMED ADHAM ISMAIL', bold=True, color=(18,39,67), size=22, after=0)
para('SENIOR FULL STACK DEVELOPER | LARAVEL & FLUTTER', bold=True, color=(18,105,173), size=11, after=4)
para('Riyadh, Saudi Arabia  |  0543577633  |  mohamedadhamismail@gmail.com', size=9.2, after=1)
para('LinkedIn: linkedin.com/in/mohamed-adham-313882240', size=9.2, after=1)
para('GitHub: github.com/mohamedadhamahmed', size=9.2, after=1)

section('PROFESSIONAL SUMMARY')
para('Senior Full Stack Developer with 5+ years of experience designing and delivering large-scale enterprise web and mobile applications. Specializes in PHP, Laravel, Flutter, and Dart, with experience building ERP platforms, logistics systems, RESTful APIs, and cross-platform mobile apps. Develops offline-first workflows that let users continue working without an internet connection and synchronize data when connectivity returns. Skilled in database design and optimization, Clean Architecture, application state management, cloud integrations, and end-to-end app delivery. Improved database query performance by 40% and mentored developers on SOLID principles and clean code.', after=2)

section('TECHNICAL SKILLS')
para('Backend: PHP, Laravel, RESTful API Design, MySQL, PostgreSQL, SQLite', after=1)
para('Mobile: Flutter, Dart, Bloc, GetX, Provider, Firebase, Firebase Cloud Messaging', after=1)
para('Frontend: JavaScript, Vue.js, HTML5, CSS3, Bootstrap, Tailwind CSS', after=1)
para('Architecture: Clean Architecture, MVC, MVVM, SOLID, Object-Oriented Programming (OOP)', after=1)
para('Tools and Platforms: Git, GitHub, Docker, Postman, Dio/HTTP, CI/CD, Google Maps API', after=1)
para('Application Capabilities: Offline-First Applications, Local Workflows, Data Synchronization on Reconnection', after=1)
para('Additional: Python, C++, Java, Payment Gateway Integration', after=2)

section('PROFESSIONAL EXPERIENCE')
doc.add_heading('Senior Full Stack Developer', level=2)
para('Ebdaa Soft for Software  |  2022 - Present', bold=True, color=(18,105,173), after=1)
bullet('Lead development of enterprise software solutions using Laravel and Vue.js.')
bullet('Architect and maintain MySQL and PostgreSQL databases; optimize queries to improve performance by 40%.')
bullet('Develop Flutter applications for internal management systems and integrate them with web backends.')
bullet('Mentor junior developers and conduct code reviews, reinforcing SOLID principles and clean code standards.')

doc.add_heading('Full Stack Developer', level=2)
para('NSAF (Logistics Tech)  |  2021 - 2022', bold=True, color=(18,105,173), after=1)
bullet('Developed a truck logistics platform with real-time bidding and order management.')
bullet('Integrated Google Maps API for live tracking and route calculation for drivers across Saudi Arabia.')
bullet('Implemented payment gateway integration and real-time notifications using Firebase Cloud Messaging.')
bullet('Managed the application lifecycle from concept through deployment to Google Play and the App Store.')

doc.add_heading('Flutter Developer', level=2)
para('MTI  |  2020 - 2021', bold=True, color=(18,105,173), after=1)
bullet('Developed and maintained mobile applications with a focus on responsive design and user experience.')
bullet('Integrated RESTful APIs and managed application state using Bloc and GetX.')

doc.add_heading('Freelance Flutter Developer', level=2)
para('Self-Employed  |  2018 - 2020', bold=True, color=(18,105,173), after=1)
bullet('Delivered custom mobile applications for clients, including e-commerce solutions.')

doc.add_page_break()
section('ERP PROJECTS')
para('Delivered ERP solutions for seven business sectors:', after=2)
doc.add_heading('Accounting ERP', level=2)
para('ERP solution for accounting and financial operations.', after=1)
doc.add_heading('Manufacturing ERP', level=2)
para('ERP solution for factory and manufacturing operations.', after=1)
doc.add_heading('Clinic Management ERP', level=2)
para('ERP solution for clinic operations.', after=1)
doc.add_heading('Pharmacy ERP', level=2)
para('ERP solution for pharmacy operations.', after=1)
doc.add_heading('Supermarket ERP and POS', level=2)
para('Retail ERP and point-of-sale solution for supermarket operations.', after=1)
doc.add_heading('Automotive Spare Parts ERP', level=2)
para('ERP solution for automotive spare parts business operations.', after=1)
doc.add_heading('Restaurant Management ERP', level=2)
para('ERP solution for restaurant operations, including a Flutter-based interface.', after=2)

section('EDUCATION')
doc.add_heading('Bachelor of Computer Science', level=2)
para('Benha University, Faculty of Computers and Artificial Intelligence  |  2016 - 2020', bold=True, color=(18,105,173), after=1)
para('Major: Computer Science  |  GPA: 2.6 / 4.0', after=3)

section('LANGUAGES')
para('Arabic: Native  |  English: Very Good (Professional Working Proficiency)', after=1)

# Keep paragraph formatting simple and machine-readable; set document metadata.
doc.core_properties.title = 'Mohamed Adham Ismail - ATS Resume'
doc.core_properties.subject = 'Senior Full Stack Developer Resume'
doc.core_properties.author = 'Mohamed Adham Ismail'
doc.save(DOCX)

# Create a selectable, text-based PDF edition with the same single-column content.
PDF_DIR = OUT / 'pdf'
PDF_DIR.mkdir(exist_ok=True)
PDF = PDF_DIR / 'Mohamed_Adham_Ismail_ATS_Resume.pdf'
pdfdoc = SimpleDocTemplate(str(PDF), pagesize=letter, rightMargin=.72*inch, leftMargin=.72*inch,
                           topMargin=.55*inch, bottomMargin=.55*inch,
                           title='Mohamed Adham Ismail - ATS Resume', author='Mohamed Adham Ismail')
navy = colors.HexColor('#122743')
blue = colors.HexColor('#1269AD')
body = colors.HexColor('#2A3340')
ps = {
 'name': ParagraphStyle('Name', fontName='Helvetica-Bold', fontSize=21, leading=23, textColor=navy, spaceAfter=1),
 'role': ParagraphStyle('Role', fontName='Helvetica-Bold', fontSize=10.7, leading=13, textColor=blue, spaceAfter=4),
 'contact': ParagraphStyle('Contact', fontName='Helvetica', fontSize=8.8, leading=11, textColor=body, spaceAfter=1),
 'section': ParagraphStyle('Section', fontName='Helvetica-Bold', fontSize=11, leading=13, textColor=blue, spaceBefore=7, spaceAfter=3),
 'job': ParagraphStyle('Job', fontName='Helvetica-Bold', fontSize=9.6, leading=11, textColor=navy, spaceBefore=3, spaceAfter=1, keepWithNext=True),
 'org': ParagraphStyle('Org', fontName='Helvetica-Bold', fontSize=8.9, leading=10.5, textColor=blue, spaceAfter=1, keepWithNext=True),
 'body': ParagraphStyle('Body', fontName='Helvetica', fontSize=8.6, leading=10.1, textColor=body, spaceAfter=2),
 'bullet': ParagraphStyle('Bullet', fontName='Helvetica', fontSize=8.45, leading=9.9, textColor=body, leftIndent=11, firstLineIndent=-7, spaceAfter=1),
}
story=[]
def pp(text, style): story.append(Paragraph(text, ps[style]))
def secp(text): pp(escape(text), 'section')
def jobp(title, employer, dates, bullets):
    story.append(KeepTogether([Paragraph(escape(title), ps['job']), Paragraph(escape(f'{employer}  |  {dates}'), ps['org'])] + [Paragraph('&bull;  '+escape(b), ps['bullet']) for b in bullets]))

pp('MOHAMED ADHAM ISMAIL','name')
pp('SENIOR FULL STACK DEVELOPER | LARAVEL &amp; FLUTTER','role')
pp('Riyadh, Saudi Arabia  |  0543577633  |  mohamedadhamismail@gmail.com','contact')
pp('LinkedIn: linkedin.com/in/mohamed-adham-313882240','contact')
pp('GitHub: github.com/mohamedadhamahmed','contact')
secp('PROFESSIONAL SUMMARY')
pp('Senior Full Stack Developer with 5+ years of experience designing and delivering large-scale enterprise web and mobile applications. Specializes in PHP, Laravel, Flutter, and Dart, with experience building ERP platforms, logistics systems, RESTful APIs, and cross-platform mobile apps. Develops offline-first workflows that let users continue working without an internet connection and synchronize data when connectivity returns. Skilled in database design and optimization, Clean Architecture, application state management, cloud integrations, and end-to-end app delivery. Improved database query performance by 40% and mentored developers on SOLID principles and clean code.', 'body')
secp('TECHNICAL SKILLS')
for line in ['Backend: PHP, Laravel, RESTful API Design, MySQL, PostgreSQL, SQLite','Mobile: Flutter, Dart, Bloc, GetX, Provider, Firebase, Firebase Cloud Messaging','Frontend: JavaScript, Vue.js, HTML5, CSS3, Bootstrap, Tailwind CSS','Architecture: Clean Architecture, MVC, MVVM, SOLID, Object-Oriented Programming (OOP)','Tools and Platforms: Git, GitHub, Docker, Postman, Dio/HTTP, CI/CD, Google Maps API','Application Capabilities: Offline-First Applications, Local Workflows, Data Synchronization on Reconnection','Additional: Python, C++, Java, Payment Gateway Integration']:
    pp(escape(line),'body')
secp('PROFESSIONAL EXPERIENCE')
jobp('Senior Full Stack Developer','Ebdaa Soft for Software','2022 - Present',[
 'Lead development of enterprise software solutions using Laravel and Vue.js.',
 'Architect and maintain MySQL and PostgreSQL databases; optimize queries to improve performance by 40%.',
 'Develop Flutter applications for internal management systems and integrate them with web backends.',
 'Mentor junior developers and conduct code reviews, reinforcing SOLID principles and clean code standards.'])
jobp('Full Stack Developer','NSAF (Logistics Tech)','2021 - 2022',[
 'Developed a truck logistics platform with real-time bidding and order management.',
 'Integrated Google Maps API for live tracking and route calculation for drivers across Saudi Arabia.',
 'Implemented payment gateway integration and real-time notifications using Firebase Cloud Messaging.',
 'Managed the application lifecycle from concept through deployment to Google Play and the App Store.'])
jobp('Flutter Developer','MTI','2020 - 2021',[
 'Developed and maintained mobile applications with a focus on responsive design and user experience.',
 'Integrated RESTful APIs and managed application state using Bloc and GetX.'])
jobp('Freelance Flutter Developer','Self-Employed','2018 - 2020',[
 'Delivered custom mobile applications for clients, including e-commerce solutions.'])
story.append(PageBreak())
secp('ERP PROJECTS')
pp('Delivered ERP solutions for seven business sectors:','body')
for title, desc in [
 ('Accounting ERP','ERP solution for accounting and financial operations.'),
 ('Manufacturing ERP','ERP solution for factory and manufacturing operations.'),
 ('Clinic Management ERP','ERP solution for clinic operations.'),
 ('Pharmacy ERP','ERP solution for pharmacy operations.'),
 ('Supermarket ERP and POS','Retail ERP and point-of-sale solution for supermarket operations.'),
 ('Automotive Spare Parts ERP','ERP solution for automotive spare parts business operations.'),
 ('Restaurant Management ERP','ERP solution for restaurant operations, including a Flutter-based interface.')]:
    story.append(KeepTogether([Paragraph(escape(title), ps['job']), Paragraph(escape(desc), ps['body'])]))
secp('EDUCATION')
pp('<b>Bachelor of Computer Science</b>','body')
pp('<font color="#1269AD"><b>Benha University, Faculty of Computers and Artificial Intelligence  |  2016 - 2020</b></font>','body')
pp('Major: Computer Science  |  GPA: 2.6 / 4.0','body')
secp('LANGUAGES')
pp('Arabic: Native  |  English: Very Good (Professional Working Proficiency)','body')
pdfdoc.build(story)
print(DOCX)
print(PDF)
