import re, json

with open('xygbfpsg_loans.sql', encoding='utf-8', errors='replace') as f:
    content = f.read()

idx = content.find('INSERT INTO `products`')
chunk = content[idx:idx+15000]
end = chunk.find(';')
stmt = chunk[:end+1] if end != -1 else chunk

# Extract product_id, product_name, product_configuration
rows = re.findall(r'\((\d+),\s*\'(.*?)\',\s*\'.*?\',\s*\'[A-Z]\',\s*\'(\{.*?\})\'\s*(?:,\s*(?:NULL|\d+))?\)', stmt, re.DOTALL)
print(f"Found {len(rows)} products\n")
for row in rows:
    pid, pname, config_raw = row
    try:
        config_clean = config_raw.replace('\\"', '"')
        config = json.loads(config_clean)
        interest = config.get('defaultLoanInterest', '0')
        method = config.get('loanInterestMethod', 'FLAT_RATE')
        min_a = config.get('minLoanPrincipalAmount', '1000')
        max_a = config.get('maxLoanPrincipalAmount', '1000000')
        min_d = config.get('minLoanDuration', '1')
        max_d = config.get('maxLoanDuration', '365')
        period = config.get('loanDurationPeriod', '01')  # 01=months
        repay = config.get('repaymentMethods', [])
        fees = config.get('productPercentageFees', [])
        print(f"Product {pid}: {pname}")
        print(f"  Interest: {interest}% | Method: {method} | Period: {period}")
        print(f"  Amount: {min_a} - {max_a}")
        print(f"  Duration: {min_d} - {max_d}")
        print(f"  Repayment: {repay}")
        for fee in fees:
            print(f"  Fee: {fee.get('feeDescription')} {fee.get('percentage')}% on {fee.get('chargeTerm')}")
        print()
    except Exception as e:
        print(f"Product {pid}: {pname} - ERROR: {e}\n")
