import os, re

ROOT = r"D:\for python\geulbeot-light\upload"

replacements = [
    (r'OPENAI_API_KEY\s*=\s*"sk-proj-[^"]*"',
     'OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")'),
    (r'GEMINI_API_KEY\s*=\s*"AIza[^"]*"',
     'GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")'),
    (r"api_key='sk-ant-[^']*'",
     'api_key=os.environ.get("ANTHROPIC_API_KEY")'),
]

targets = [
    "converters/pipeline/step3_domain.py",
    "converters/pipeline/step4_chunk.py",
    "converters/pipeline/step5_rag.py",
    "converters/pipeline/step6_corpus.py",
    "converters/pipeline/step7_index.py",
    "converters/pipeline/step8_content.py",
    "handlers/common.py",
]

# .env 濡 肄 異媛
dotenv_snippet = "from dotenv import load_dotenv\nload_dotenv()\n"

for rel_path in targets:
    path = os.path.join(ROOT, rel_path)
    with open(path, encoding="utf-8") as f:
        content = f.read()
    if "load_dotenv" not in content:
        content = dotenv_snippet + content
        with open(path, "w", encoding="utf-8") as f:
            f.write(content)
        print(f"
 dotenv 異媛: {rel_path}")
    changed = False
    for pattern, replacement in replacements:
        new_content = re.sub(pattern, replacement, content)
        if new_content != content:
            content = new_content
            changed = True

    if changed:
        with open(path, "w", encoding="utf-8") as f:
            f.write(content)
        print(f"
 : {rel_path}")
    else:
        print(f"  蹂寃쎌: {rel_path}")

print("\n
          猷!")
