#!/usr/bin/env python3
from pathlib import Path
import os,json,urllib.request,urllib.parse,datetime as dt
ROOT=Path('/data/video-pipeline/HaTramAudio/project/026-Ngay-Chung-Ta-Xoa-Ten-Khoi-Giay-Dang-Ky-Ket-Hon');ENV=Path.home()/'.config/ha-tram-audio/postiz.env';e={}
for line in ENV.read_text().splitlines():
 if line and not line.lstrip().startswith('#') and '=' in line:
  k,v=line.split('=',1);e[k.strip()]=v.strip().strip('"').strip("'")
base=e['POSTIZ_BASE_URL'].rstrip('/');key=e['POSTIZ_API_KEY'];slot='2026-07-31T12:00:00Z';fb=e['POSTIZ_FACEBOOK_INTEGRATION_ID'];yt=e['POSTIZ_YOUTUBE_INTEGRATION_ID'];expected={fb:'cmrvozc42000mj70q63kaofgx',yt:'cmrvozc4j000nj70q1wswpl59'}
def get(path):
 q=urllib.request.Request(base+path,headers={'Authorization':key});return json.loads(urllib.request.urlopen(q,timeout=60).read())
start='2026-07-31T11:55:00Z';end='2026-07-31T12:05:00Z';d=get('/posts?'+urllib.parse.urlencode({'startDate':start,'endDate':end}));posts=d.get('posts',d.get('data',d));out={}
def norm(s):
 return dt.datetime.fromisoformat(s.replace('Z','+00:00')).astimezone(dt.timezone.utc)
for iid,pid in expected.items():
 xs=[p for p in posts if (p.get('integration') or {}).get('id')==iid and norm(p.get('publishDate') or p.get('date'))==norm(slot) and p.get('state') in ('QUEUE','PUBLISHED')]
 assert len(xs)==1 and xs[0].get('id')==pid
 out[iid]={'post_id':pid,'state':xs[0]['state'],'same_slot_count':len(xs)}
rec={'schema_version':1,'project_id':'026','run_id':'run-20260722T033406Z-3df8cc80','status':'passed','verified':True,'slot_utc':slot,'slot_local':'2026-07-31T19:00:00+07:00','posts':out,'duplicate':False,'checked_at':dt.datetime.now(dt.timezone.utc).isoformat()};(ROOT/'script/calendar-verification.json').write_text(json.dumps(rec,ensure_ascii=False,indent=2)+'\n');print(json.dumps(rec,ensure_ascii=False))
