#!/usr/bin/env python3
"""CSV 업데이트 도우미 - 빈 필드만 안전하게 채움"""
import csv
import sys
import json
import os

CSV_PATH = os.path.join(os.path.dirname(__file__), 'clinic_registry_working.csv')

# Column indices
COLS = {
    'hospital_name': 0, 'brand_group': 1, 'district': 2, 'branches': 3,
    'website_kr': 4, 'website_en': 5,
    'youtube_url': 6, 'youtube_note': 7,
    'instagram_kr_url': 8, 'instagram_kr_note': 9,
    'instagram_en_url': 10, 'instagram_en_note': 11,
    'facebook_url': 12, 'facebook_note': 13,
    'tiktok_url': 14, 'tiktok_note': 15,
    'gangnam_unni_url': 16, 'gangnam_unni_note': 17,
    'naver_blog_url': 18, 'naver_blog_note': 19,
    'naver_place_url': 20, 'naver_place_reviews_note': 21,
    'google_maps_url': 22, 'google_reviews_note': 23,
}

def load_csv():
    with open(CSV_PATH, 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        header = next(reader)
        rows = list(reader)
    return header, rows

def save_csv(header, rows):
    with open(CSV_PATH, 'w', encoding='utf-8', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(header)
        writer.writerows(rows)

def ensure_row_length(row, min_len=24):
    while len(row) < min_len:
        row.append('')
    return row

def update_hospital(rows, hospital_name, updates: dict):
    """
    updates: {'youtube_url': 'https://...', 'instagram_kr_url': 'https://...', ...}
    Only fills EMPTY fields. Never overwrites existing data.
    Returns True if hospital found.
    """
    for row in rows:
        row = ensure_row_length(row)
        if row[0].strip() == hospital_name.strip():
            changed = []
            for col_name, value in updates.items():
                if col_name not in COLS:
                    print(f"  ⚠️  Unknown column: {col_name}")
                    continue
                idx = COLS[col_name]
                if row[idx].strip() == '' and value.strip() != '':
                    row[idx] = value.strip()
                    changed.append(f"{col_name}={value.strip()[:50]}")
                elif row[idx].strip() != '':
                    pass  # Skip - already has data
            if changed:
                print(f"  ✅ {hospital_name}: {', '.join(changed)}")
            else:
                print(f"  ⏭️  {hospital_name}: no empty fields to fill")
            return True
    print(f"  ❌ {hospital_name}: NOT FOUND in CSV")
    return False

def batch_update(updates_list):
    """
    updates_list: [{'hospital_name': '...', 'youtube_url': '...', ...}, ...]
    """
    header, rows = load_csv()
    count = 0
    for item in updates_list:
        name = item.pop('hospital_name', None)
        if name:
            if update_hospital(rows, name, item):
                count += 1
    save_csv(header, rows)
    print(f"\n📊 Updated {count} hospitals. CSV saved.")

def print_coverage():
    header, rows = load_csv()
    cols_to_check = {
        'website_kr': 4, 'youtube': 6, 'instagram_kr': 8,
        'facebook': 12, 'tiktok': 14, 'gangnam_unni': 16,
        'naver_blog': 18, 'naver_place': 20, 'google_maps': 22
    }
    total = len(rows)
    print(f"\n{'Channel':15s}  {'Filled':>6s}/{total}  {'%':>5s}")
    print("-" * 35)
    for name, idx in cols_to_check.items():
        filled = sum(1 for r in rows if len(r) > idx and r[idx].strip())
        pct = filled * 100 // total if total > 0 else 0
        bar = '█' * (pct // 5) + '░' * (20 - pct // 5)
        print(f'{name:15s}  {filled:3d}/{total}  {pct:3d}%  {bar}')

if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[1] == 'coverage':
        print_coverage()
    elif len(sys.argv) > 1 and sys.argv[1] == 'update':
        # Read JSON from stdin
        data = json.load(sys.stdin)
        if isinstance(data, list):
            batch_update(data)
        elif isinstance(data, dict):
            batch_update([data])
    else:
        print("Usage:")
        print("  python update_csv.py coverage")
        print("  echo '[{...}]' | python update_csv.py update")
