#!/usr/bin/env python3
import sqlite3
from pathlib import Path

print("="*80)
print("어드민 서버 & DB 연결 검증")
print("="*80)

dbs = {
    'kis_data_collection.db': 'src/quant_engine/kis_data_collection.db',
    'snapshot_admin.db': 'src/quant_engine/snapshot_admin.db'
}

all_ok = True

for name, path in dbs.items():
    if not Path(path).exists():
        print(f'[FAIL] {name} not found')
        all_ok = False
        continue

    try:
        conn = sqlite3.connect(path)
        cursor = conn.cursor()
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        tables = [row[0] for row in cursor.fetchall()]

        # 각 테이블 행 수
        table_info = {}
        for table in tables:
            if table == 'sqlite_sequence':
                continue
            cursor.execute(f'SELECT COUNT(*) FROM {table}')
            count = cursor.fetchone()[0]
            table_info[table] = count

        conn.close()

        file_size = Path(path).stat().st_size / 1024
        print(f'\n[OK] {name} ({file_size:.2f} KB)')
        for table, count in sorted(table_info.items()):
            print(f'      └─ {table}: {count} records')
    except Exception as e:
        print(f'\n[FAIL] {name}: {e}')
        all_ok = False

print("\n" + "="*80)
if all_ok:
    print("[결과] [OK] 어드민 서버 & DB 모두 정상 접속")
else:
    print("[결과] [FAIL] DB 연결 실패")
