#!/usr/bin/env python3
"""Remove 中国石化安庆分公司 config + archive orphan sinopec scripts"""
import json, os, shutil

CONFIG_PATH = '/root/gov_crawler/daily_crawl_config.json'
ARCHIVE_DIR = '/root/gov_crawler/Archive'

# Scripts to archive (redundant - covered by crawl_sinopec.py)
scripts_to_archive = [
    'crawl_apw.py',      # 安庆石化 → sinopec.py apw
    'crawl_bypc.py',     # 燕山石化 → sinopec.py bypc
    'crawl_zrcc.py',     # 镇海石化 → sinopec.py zrcc
    'crawl_zrpc.py',     # 中科炼化 → sinopec.py zrpc
    'crawl_zrpc_env.py', # 中科炼化-项目环保三同时 → sinopec.py zrpc_env
]

# 1. Remove config entry
with open(CONFIG_PATH, 'r') as f:
    items = json.load(f)

removed = []
items_new = []
for item in items:
    if item.get('name') == '中国石化安庆分公司':
        removed.append(item)
        print(f"Removed config: {item['name']} ({item['script']})")
    else:
        items_new.append(item)

with open(CONFIG_PATH, 'w') as f:
    json.dump(items_new, f, ensure_ascii=False, indent=2)
print(f"Config saved: {len(items_new)} entries")

# 2. Archive scripts
os.makedirs(ARCHIVE_DIR, exist_ok=True)
for script in scripts_to_archive:
    src = os.path.join('/root/gov_crawler', script)
    dst = os.path.join(ARCHIVE_DIR, script)
    if os.path.exists(src):
        shutil.move(src, dst)
        print(f"Archived: {script}")
    else:
        print(f"Not found (already archived?): {script}")

# 3. Verify no remaining references
with open(CONFIG_PATH, 'r') as f:
    items = json.load(f)

orphans = []
for item in items:
    script = item.get('script', '')
    script_path = os.path.join('/root/gov_crawler', script)
    if not os.path.exists(script_path):
        orphans.append(script)

print(f"\nOrphan scripts remaining: {len(orphans)}")
for s in orphans[:10]:
    print(f"  [orphan] {s}")

print("\nDone!")
