
import os
import io
import logging
from lawrisk.services.licensing_repo import _parse_import_workbook
from lawrisk.utils.env_loader import load_env

# Configure logging to see the import details
logging.basicConfig(level=logging.INFO)

def diagnose_import():
    load_env()
    file_path = r"c:\Users\WIN10\Desktop\work\11th-week\法律风险提示-new\市监局-lawRisk-backend\市级初版-20251219\许可风险提示\101（生产科） 风险提示表(食品小作坊登记，市场监管部门) (1)_转自XLS.xlsx"
    
    with open(file_path, "rb") as f:
        content = f.read()
    
    # We want to catch the logger output
    logger = logging.getLogger('lawrisk.services.licensing_repo')
    
    print("Starting diagnostic parse...")
    grouped = _parse_import_workbook(content, "101_test.xlsx")
    
    # After parsing, we want to see the results for the relevant sheets
    for sheet_name, permits in grouped.items():
        if "高明区" in sheet_name or "三水区" in sheet_name:
            print(f"\nResults for Sheet: {sheet_name}")
            for permit_name, rows in permits.items():
                if "食品小作坊" in permit_name:
                    print(f"  Permit: {permit_name} ({len(rows)} risks)")
                    for i, r in enumerate(rows):
                        print(f"    Risk {i+1}: Serial=[{r.get('serial_number')}] | Content=[{r.get('risk_content')[:50]}...]")

if __name__ == "__main__":
    diagnose_import()
