
import openpyxl
import os

def generate_test_file():
    template_path = r"c:\Users\WIN10\Desktop\work\11th-week\法律风险提示-new\市监局-lawRisk-backend\data\template\风险提示表 模板.xlsx"
    output_path = r"c:\Users\WIN10\Desktop\work\11th-week\法律风险提示-new\市监局-lawRisk-backend\test_import_data.xlsx"
    
    wb = openpyxl.load_workbook(template_path)
    # The blank template usually has one sheet.
    ws = wb.active
    
    # Based on previous analysis (comparison_report.md)
    # Metadata rows (approx row 1-7)
    # Header row is Row 8.
    # Data starts Row 9.
    
    # Let's fill metadata for safety if parser looks for it
    # But _normalize_import_row gets it from columns if they exist.
    # Actually, the parser extracts metadata candidates from rows 1-header.
    # The new template has: 
    # Row 3: "许可（备案）事项名称" (B3) -> value at C3?
    # Row 4: "许可情况..."
    # Row 5: "填表人" (B5) -> C5
    # Row 6: "联系方式..." (B6) -> C6
    # Row 7: "事项实施层级" (B7) -> C7, "单位名称" (E7) -> F7, "表格更新日期" (H7) -> I7
    
    # Unmerge everything.
    for range_string in list(ws.merged_cells.ranges):
        ws.unmerge_cells(range_string.coord)

    # Based on inspection:
    # Row 2 (B2): Permit Name -> Write C2
    # Row 3 (B3): Status -> Write C3
    # Row 4 (B4): Filler -> Write C4
    # Row 5 (B5): Contact -> Write C5
    # Row 6 (B6): Level -> Write C6
    # Row 7 (B7): Unit -> Write C7
    # Row 8 (B8): Date -> Write C8
    
    ws.cell(row=2, column=3).value = "Integration Test Permit"
    ws.cell(row=3, column=3).value = "Active"
    ws.cell(row=4, column=3).value = "AutoTester"
    ws.cell(row=5, column=3).value = "12345678"
    ws.cell(row=6, column=3).value = "Integration Level"
    ws.cell(row=7, column=3).value = "Test Unit"
    ws.cell(row=8, column=3).value = "2023-12-20"
    
    # Table headers are at Row 8. 
    # Columns: A:序号, B:风险提示内容, C:法律依据, D:文号, E:摘要, F:备注
    
    # Add Data Row 1 (Row 10 to avoid overwriting headers if they are in Row 9)
    ws["B10"] = "1"
    ws["C10"] = "Risk: Unregistered Business"
    ws["D10"] = "Company Law Art 1"
    ws["E10"] = "Doc No. 101"
    ws["F10"] = "Must register"
    ws["G10"] = "No remarks"
    
    # Add Data Row 2 (Row 11)
    ws["B11"] = "2"
    ws["C11"] = "Risk: Expired License"
    ws["D11"] = "License Law Art 5"
    
    print(f"Saving populated test file to {output_path}")
    wb.save(output_path)

if __name__ == "__main__":
    generate_test_file()
