#!/usr/bin/env python3
"""
OSINT Person Collector - Word Report Generator
Generates standardized deep profile reports in .docx format per Chinese government document formatting standards.

Usage:
    python3 generate_report.py --output <path.docx> --title <报告标题>

The script reads profile data from a JSON structure and produces a formatted Word document.
"""

import argparse
import json
import sys
from docx import Document
from docx.shared import Pt, Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn
from docx.oxml import OxmlElement


# ====== Page Setup ======
def setup_page(doc):
    section = doc.sections[0]
    section.page_width = Cm(21.0)
    section.page_height = Cm(29.7)
    section.top_margin = Cm(3.7)
    section.bottom_margin = Cm(2.8)
    section.left_margin = Cm(2.8)
    section.right_margin = Cm(2.6)


# ====== Font Helpers ======
def set_font(run, font_name, size_pt, bold=False):
    run.font.size = Pt(size_pt)
    run.font.bold = bold
    run.font.name = font_name
    r = run._element
    rPr = r.find(qn('w:rPr'))
    if rPr is None:
        rPr = OxmlElement('w:rPr')
        r.insert(0, rPr)
    rFonts = rPr.find(qn('w:rFonts'))
    if rFonts is None:
        rFonts = OxmlElement('w:rFonts')
        rPr.insert(0, rFonts)
    rFonts.set(qn('w:eastAsia'), font_name)
    rFonts.set(qn('w:ascii'), font_name)
    rFonts.set(qn('w:hAnsi'), font_name)


def add_title(doc, text):
    p = doc.add_paragraph()
    p.alignment = WD_ALIGN_PARAGRAPH.CENTER
    p.paragraph_format.space_before = Pt(0)
    p.paragraph_format.space_after = Pt(10)
    p.paragraph_format.line_spacing = Pt(28)
    run = p.add_run(text)
    set_font(run, '方正小标宋简体', 22, bold=True)


def add_h1(doc, text):
    p = doc.add_paragraph()
    p.alignment = WD_ALIGN_PARAGRAPH.LEFT
    p.paragraph_format.space_before = Pt(12)
    p.paragraph_format.space_after = Pt(6)
    p.paragraph_format.line_spacing = Pt(28)
    run = p.add_run(text)
    set_font(run, '黑体', 16, bold=True)


def add_h2(doc, text):
    p = doc.add_paragraph()
    p.alignment = WD_ALIGN_PARAGRAPH.LEFT
    p.paragraph_format.space_before = Pt(8)
    p.paragraph_format.space_after = Pt(4)
    p.paragraph_format.line_spacing = Pt(28)
    run = p.add_run(text)
    set_font(run, '楷体_GB2312', 16, bold=False)


def add_body(doc, text, indent=False):
    p = doc.add_paragraph()
    p.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
    p.paragraph_format.space_before = Pt(0)
    p.paragraph_format.space_after = Pt(0)
    p.paragraph_format.line_spacing = Pt(28)
    if indent:
        p.paragraph_format.first_line_indent = Pt(32)
    run = p.add_run(text)
    set_font(run, '仿宋_GB2312', 16, bold=False)


def create_table(doc, headers, rows):
    table = doc.add_table(rows=1, cols=len(headers))
    table.style = 'Table Grid'
    for i, h in enumerate(headers):
        cell = table.rows[0].cells[i]
        cell.text = ''
        p = cell.paragraphs[0]
        p.alignment = WD_ALIGN_PARAGRAPH.CENTER
        run = p.add_run(h)
        set_font(run, '黑体', 14, bold=True)
    for row_data in rows:
        row = table.add_row()
        for i, text in enumerate(row_data):
            cell = row.cells[i]
            cell.text = ''
            p = cell.paragraphs[0]
            p.alignment = WD_ALIGN_PARAGRAPH.CENTER
            run = p.add_run(str(text))
            set_font(run, '仿宋_GB2312', 14, bold=False)
    return table


def add_blank_line(doc):
    p = doc.add_paragraph()
    p.paragraph_format.space_before = Pt(0)
    p.paragraph_format.space_after = Pt(0)
    p.paragraph_format.line_spacing = Pt(28)


def main():
    parser = argparse.ArgumentParser(description='Generate OSINT person profile Word report')
    parser.add_argument('--output', required=True, help='Output .docx file path')
    parser.add_argument('--title', required=True, help='Report title')
    parser.add_argument('--data', help='JSON data file path (optional)')
    args = parser.parse_args()

    doc = Document()
    setup_page(doc)
    add_title(doc, args.title)
    add_blank_line(doc)

    # If JSON data provided, populate from it
    if args.data:
        with open(args.data, 'r', encoding='utf-8') as f:
            data = json.load(f)
        # TODO: Implement structured data population
        # For now, just add the title
        add_body(doc, f'数据文件: {args.data}', indent=True)

    doc.save(args.output)
    print(f'✅ Report saved: {args.output}')


if __name__ == '__main__':
    main()
