"""Local copy of the job-seeker skill's active JSON-to-DOCX renderer."""
import argparse
import json
import os
from docx import Document
from docx.shared import Pt, RGBColor, Inches, Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH

NAVY = RGBColor(0x1F, 0x38, 0x64)
BLACK = RGBColor(0x00, 0x00, 0x00)
DARK = RGBColor(0x33, 0x33, 0x36)
NAME = "ROB BLAKE"
CONTACT = "Phone: 303-800-7628  •  rkblake@gmail.com  •  LinkedIn.com/in/robkblake"

def set_run(para, text, bold=False, pt=11, color=BLACK, font="Calibri"):
    run = para.add_run(text); run.bold = bold; run.font.size = Pt(pt); run.font.color.rgb = color; run.font.name = font; return run

def para(doc, alignment=WD_ALIGN_PARAGRAPH.LEFT, before=0, after=0):
    p = doc.add_paragraph(); p.alignment = alignment; p.paragraph_format.space_before = Pt(before); p.paragraph_format.space_after = Pt(after); return p

def section_heading(doc, text):
    p = para(doc, before=8, after=1); p.paragraph_format.keep_with_next = True; set_run(p, text, True, 11, NAVY)

def body(doc, text, after=2):
    p = para(doc, after=after); p.paragraph_format.line_spacing = Pt(12); set_run(p, text, False, 10.5, DARK)

def parse_header(header):
    parts = [s.strip() for s in header.split("|")]
    if len(parts) == 4: return parts[0], parts[1], parts[2], parts[3]
    raise ValueError(f"Expected Title | Company | Location | Dates, got: {header}")

def role_line(doc, title, company, location, dates):
    p = para(doc, before=6); p.paragraph_format.keep_with_next = True; set_run(p, title, True, 10.5, DARK); set_run(p, "\t" + dates, False, 10.5, DARK)
    p = para(doc, after=1); p.paragraph_format.keep_with_next = True; set_run(p, company + " — " + location, False, 10.5, NAVY)

def bullet(doc, text):
    p = para(doc, after=2); p.paragraph_format.line_spacing = Pt(12); p.paragraph_format.left_indent = Inches(0.25); set_run(p, "•  " + text, False, 10.5, DARK)

def build(json_path, out_path):
    with open(json_path, encoding="utf-8") as f: data = json.load(f)
    doc = Document(); sec = doc.sections[0]
    sec.top_margin = Cm(1.5); sec.bottom_margin = Cm(1.5); sec.left_margin = Cm(1.8); sec.right_margin = Cm(1.8)
    p = para(doc, WD_ALIGN_PARAGRAPH.CENTER); set_run(p, NAME, True, 20, NAVY)
    p = para(doc, WD_ALIGN_PARAGRAPH.CENTER, after=2); set_run(p, data["title"], True, 12, BLACK)
    p = para(doc, WD_ALIGN_PARAGRAPH.CENTER, after=12); set_run(p, CONTACT, False, 10, BLACK)
    section_heading(doc, "PROFESSIONAL SUMMARY"); body(doc, data["summary"], 6)
    section_heading(doc, "CORE COMPETENCIES")
    for c in data["competencies"]:
        p = para(doc, after=2); set_run(p, c["label"] + "  ", True, 10.5, DARK); set_run(p, c["items"], False, 10.5, DARK)
    section_heading(doc, "EXPERIENCE")
    for j in data["experience"]:
        role_line(doc, *parse_header(j["header"]))
        for b in j["bullets"]: bullet(doc, b)
    section_heading(doc, "EDUCATION")
    for line in data["education"]: body(doc, line)
    section_heading(doc, "MILITARY"); body(doc, data["military_note"], 6)
    os.makedirs(os.path.dirname(out_path), exist_ok=True); doc.save(out_path)
    print(f"Saved: {out_path}\nSize: {os.path.getsize(out_path)} bytes\nParagraphs: {len(doc.paragraphs)}")

if __name__ == "__main__":
    ap = argparse.ArgumentParser(); ap.add_argument("--json", required=True); ap.add_argument("--out", required=True); a = ap.parse_args(); build(a.json, a.out)
