# -*- coding: utf-8 -*-
"""Chẩn đoán: vì sao ảnh mới không được nhặt? (mốc thời gian vs mtime ảnh, sổ theo dõi)"""
import hashlib, importlib.util, json, os, time

D = r'C:\Claw 2\Nghien cuu\Du-an-A-Quy-trinh-son'
CACHE = os.path.join(os.environ['LOCALAPPDATA'], 'hermes', 'cache', 'images')
MARK = os.path.join(D, '_lan_chay_cuoi.txt')
STATE = os.path.join(D, '_anh_da_xu_ly.json')

spec = importlib.util.spec_from_file_location('r', os.path.join(D, '_run_agy_theo_lo.py'))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)

mark = m._doc_mark()
print('MỐC thời gian (lần chạy trước):', time.strftime('%d/%m %H:%M:%S', time.localtime(mark)), '| raw', open(MARK).read().strip())
st = m._doc_state()
print('sổ theo dõi:', len(st), 'ảnh')
print()
print('--- các ảnh trong cache (20 mới nhất) ---')
lo = [f for f in os.listdir(CACHE) if f.startswith('img_')]
lo.sort(key=lambda f: os.path.getmtime(os.path.join(CACHE, f)))
for f in lo[-20:]:
    p = os.path.join(CACHE, f)
    mt = os.path.getmtime(p)
    h = hashlib.md5(open(p, 'rb').read()).hexdigest()
    print('  %-26s %s | mới hơn mốc: %-5s | đã theo dõi: %-5s' %
          (f, time.strftime('%d/%m %H:%M:%S', time.localtime(mt)), mt > mark, h in st))
print()
print('--- ảnh mới nhất (vừa gửi) ---')
p = os.path.join(CACHE, 'img_6a39c678b873.jpg')
mt = os.path.getmtime(p)
h = hashlib.md5(open(p, 'rb').read()).hexdigest()
print('  mtime:', time.strftime('%d/%m %H:%M:%S', time.localtime(mt)), '| mới hơn mốc:', mt > mark, '| md5:', h[:12], '| đã theo dõi:', h in st)
