# -*- coding: utf-8 -*-
"""①キャッシュの検証: 件数・最良フレーム・明日の送信/スキップ内訳を確認する。"""
import json, os
from collections import Counter

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
cache = os.path.join(ROOT, "local_batch_api", "density_cache.jsonl")
bf_dir = os.path.join(ROOT, "local_batch_api", "prepared_best_frames")

lines = [json.loads(l) for l in open(cache, encoding="utf-8") if l.strip()]
print("cache件数:", len(lines))
print("best frame画像:", len(os.listdir(bf_dir)) if os.path.isdir(bf_dir) else 0)

src, rd = Counter(), Counter()
sent = skip = missing = 0
for r in lines:
    src[r.get("source")] += 1
    readable = bool(r.get("readable"))
    rd["readable" if readable else "unreadable"] += 1
    steam = float(r.get("steam_mean", 1.0))
    if (not readable) and steam >= 0.80:
        skip += 1
    else:
        sent += 1
        p = r.get("best_frame_path", "")
        if not (p and os.path.exists(p)):
            missing += 1

print("source:", dict(src))
print("可読性:", dict(rd))
print(f"明日LLM送信: {sent} 件 / スキップ(判定不可): {skip} 件")
print("送信対象なのに最良フレーム画像が無い:", missing, "（0なら正常）")
