import json,os,re,subprocess,time,urllib.request
from pathlib import Path
from app.db import SessionLocal
from app.models import AutomationEpisode
from app.services.automation import _upload_batch
MANIFEST=json.loads('[{"episode_id": 1477, "title": "Fate/Grand Order Selected Specials", "destination": {"server": "Ronak", "location": "anime/2017/Special", "folder": "Fate Grand Order - Moonlight Lostroom"}, "output_names": {"1080p": "Fate Grand Order - Moonlight Lostroom.[1080][MixFlixTop].mkv", "480p": "Fate Grand Order - Moonlight Lostroom.[480][MixFlixTop].mkv", "720p": "Fate Grand Order - Moonlight Lostroom.[720][MixFlixTop].mkv"}, "inferred_quality_names": [], "source_page": "https://animex.pro/anime/fate-grand-order-moonlight-lostroom/", "source_urls": ["https://sr.hollowofthealley.space/ndl1/anime/2017/Special/Fate%20Grand%20Order%20-%20Moonlight%20Lostroom/Fate%20Grand%20Order%20-%20Moonlight%20Lostroom.%5B1080%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/ndl1/anime/2017/Special/Fate%20Grand%20Order%20-%20Moonlight%20Lostroom/Fate%20Grand%20Order%20-%20Moonlight%20Lostroom.%5B480%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/ndl1/anime/2017/Special/Fate%20Grand%20Order%20-%20Moonlight%20Lostroom/Fate%20Grand%20Order%20-%20Moonlight%20Lostroom.%5B720%5D%5BMixFlixTop%5D.mkv"]}, {"episode_id": 1478, "title": "Fate/Grand Order Selected Specials", "destination": {"server": "Ronak", "location": "anime/2019/Special", "folder": "Fate Grand Order - Initium Iter"}, "output_names": {"1080p": "Fate Grand Order - Initium Iter.[SS][1080][MixFlixTop].mkv", "480p": "Fate Grand Order - Initium Iter.[SS][480][MixFlixTop].mkv", "720p": "Fate Grand Order - Initium Iter.[SS][720][MixFlixTop].mkv"}, "inferred_quality_names": [], "source_page": "https://animex.pro/anime/fate-grand-order-zettai-majuu-sensen-babylonia-initium-iter/", "source_urls": ["https://sr.hollowofthealley.space/ndl1/anime/2019/Special/Fate%20Grand%20Order%20-%20Initium%20Iter/Fate%20Grand%20Order%20-%20Initium%20Iter.%5BSS%5D%5B1080%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/ndl1/anime/2019/Special/Fate%20Grand%20Order%20-%20Initium%20Iter/Fate%20Grand%20Order%20-%20Initium%20Iter.%5BSS%5D%5B480%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/ndl1/anime/2019/Special/Fate%20Grand%20Order%20-%20Initium%20Iter/Fate%20Grand%20Order%20-%20Initium%20Iter.%5BSS%5D%5B720%5D%5BMixFlixTop%5D.mkv"]}, {"episode_id": 1479, "title": "Fate/Grand Order Selected Specials", "destination": {"server": "Ronak", "location": "anime/Movie/2020", "folder": "Fate Grand Order - Shinsei Camelot"}, "output_names": {"1080p": "Fate Grand Order - Shinsei Camelot.[SS][1080][MixFlixTop].mkv", "480p": "Fate Grand Order - Shinsei Camelot.[SS][480][MixFlixTop].mkv", "720p": "Fate Grand Order - Shinsei Camelot.[SS][720][MixFlixTop].mkv"}, "inferred_quality_names": [], "source_page": "https://animex.pro/anime/fate-grand-order-shinsei-entaku-ryouiki-camelot-1-wandering-agateram/", "source_urls": ["https://sr.hollowofthealley.space/ndl1/anime/Movie/2020/Fate%20Grand%20Order%20-%20Shinsei%20Camelot/Fate%20Grand%20Order%20-%20Shinsei%20Camelot.%5BSS%5D%5B1080%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/ndl1/anime/Movie/2020/Fate%20Grand%20Order%20-%20Shinsei%20Camelot/Fate%20Grand%20Order%20-%20Shinsei%20Camelot.%5BSS%5D%5B480%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/ndl1/anime/Movie/2020/Fate%20Grand%20Order%20-%20Shinsei%20Camelot/Fate%20Grand%20Order%20-%20Shinsei%20Camelot.%5BSS%5D%5B720%5D%5BMixFlixTop%5D.mkv"]}, {"episode_id": 1483, "title": "Fate/kaleid liner Prisma Illya Movie: Sekka no Chikai", "destination": {"server": "Ronak", "location": "anime/Movie/2017", "folder": "Fate kaleid liner Prisma Illya Movie - Oath Under Snow"}, "output_names": {"1080p": "Fate kaleid liner Prisma Illya Movie - Oath Under Snow.[SS][1080][MixFlixTop].mkv", "480p": "Fate kaleid liner Prisma Illya Movie - Oath Under Snow.[SS][480][MixFlixTop].mkv", "720p": "Fate kaleid liner Prisma Illya Movie - Oath Under Snow.[SS][720][MixFlixTop].mkv"}, "inferred_quality_names": [], "source_page": "https://animex.pro/anime/fate-kaleid-liner-prisma%e2%98%86illya-movie-sekka-no-chikai/", "source_urls": ["https://sr.hollowofthealley.space/ndl1/anime/Movie/2017/Fate%20kaleid%20liner%20Prisma%20Illya%20Movie%20-%20Oath%20Under%20Snow/Fate%20kaleid%20liner%20Prisma%20Illya%20Movie%20-%20Oath%20Under%20Snow.%5BSS%5D%5B1080%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/ndl1/anime/Movie/2017/Fate%20kaleid%20liner%20Prisma%20Illya%20Movie%20-%20Oath%20Under%20Snow/Fate%20kaleid%20liner%20Prisma%20Illya%20Movie%20-%20Oath%20Under%20Snow.%5BSS%5D%5B480%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/ndl1/anime/Movie/2017/Fate%20kaleid%20liner%20Prisma%20Illya%20Movie%20-%20Oath%20Under%20Snow/Fate%20kaleid%20liner%20Prisma%20Illya%20Movie%20-%20Oath%20Under%20Snow.%5BSS%5D%5B720%5D%5BMixFlixTop%5D.mkv"]}, {"episode_id": 1490, "title": "Kobayashi-san Chi no Maid Dragon OVA", "destination": {"server": "Ronak", "location": "anime/2017/Special", "folder": "Kobayashi-san - OVA"}, "output_names": {"1080p": "Kobayashi-san - OVA.[SS][1080][MixFlixTop].mkv", "480p": "Kobayashi-san - OVA.[SS][480][MixFlixTop].mkv", "720p": "Kobayashi-san - OVA.[SS][720][MixFlixTop].mkv"}, "inferred_quality_names": [], "source_page": "https://animex.pro/anime/kobayashi-san-chi-no-maid-dragon-valentine-soshite-onsen-amari-kitai-shinaide-kudasai/", "source_urls": ["https://sr.hollowofthealley.space/ndl1/anime/2017/Special/Kobayashi-san%20-%20OVA/Kobayashi-san%20-%20OVA.%5BSS%5D%5B1080%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/ndl1/anime/2017/Special/Kobayashi-san%20-%20OVA/Kobayashi-san%20-%20OVA.%5BSS%5D%5B480%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/ndl1/anime/2017/Special/Kobayashi-san%20-%20OVA/Kobayashi-san%20-%20OVA.%5BSS%5D%5B720%5D%5BMixFlixTop%5D.mkv"]}, {"episode_id": 1491, "title": "Monsters: 103 Mercies Dragon Damnation", "destination": {"server": "Ronak", "location": "anime/2024/ONA", "folder": "Monsters - Ippyaku Sanjou Hiryuu Jigoku - ONA"}, "output_names": {"1080p": "Monsters - Ippyaku Sanjou Hiryuu Jigoku - ONA.[SS][1080][MixFlixTop].mkv", "480p": "Monsters - Ippyaku Sanjou Hiryuu Jigoku - ONA.[SS][480][MixFlixTop].mkv", "720p": "Monsters - Ippyaku Sanjou Hiryuu Jigoku - ONA.[SS][720][MixFlixTop].mkv"}, "inferred_quality_names": [], "source_page": "https://animex.pro/anime/monsters-ippyaku-sanjou-hiryuu-jigoku/", "source_urls": ["https://sr.hollowofthealley.space/dl/anime/2024/ONA/Monsters%20-%20Ippyaku%20Sanjou%20Hiryuu%20Jigoku%20-%20ONA/Monsters%20-%20Ippyaku%20Sanjou%20Hiryuu%20Jigoku%20-%20ONA.%5BSS%5D%5B1080%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/dl/anime/2024/ONA/Monsters%20-%20Ippyaku%20Sanjou%20Hiryuu%20Jigoku%20-%20ONA/Monsters%20-%20Ippyaku%20Sanjou%20Hiryuu%20Jigoku%20-%20ONA.%5BSS%5D%5B480%5D%5BMixFlixTop%5D.mkv", "https://sr.hollowofthealley.space/dl/anime/2024/ONA/Monsters%20-%20Ippyaku%20Sanjou%20Hiryuu%20Jigoku%20-%20ONA/Monsters%20-%20Ippyaku%20Sanjou%20Hiryuu%20Jigoku%20-%20ONA.%5BSS%5D%5B720%5D%5BMixFlixTop%5D.mkv"]}]')
REPORT=Path('/home/sites/files/ndl1/_automation/rdl1-recovery-upload-results.json')
results=[]
if REPORT.exists():results=json.loads(REPORT.read_text())
for row in MANIFEST:
 eid=row['episode_id']
 if any(r['episode_id']==eid and r.get('verified') for r in results):continue
 try:
  with SessionLocal() as db:
   ep=db.get(AutomationEpisode,eid)
   assert ep and ep.subtitle_status=='ready' and ep.video_status=='ready'
   assert ep.mux_status=='awaiting_destination',(eid,ep.mux_status)
   source=Path('/home/sites/files/ndl1/_automation/upload_batches')/str(eid)
   staging=source.with_name(str(eid)+'-rdl1-original-names');staging.mkdir(exist_ok=True)
   files=[]
   for q,fn in row['output_names'].items():
    found=[p for p in source.glob('*.mkv') if '['+q+']' in p.name];assert len(found)==1,(eid,q)
    target=staging/fn
    if not target.exists():os.link(found[0],target)
    assert target.stat().st_ino==found[0].stat().st_ino
    probe=subprocess.run(['ffprobe','-v','error','-select_streams','s','-show_entries','stream=index:stream_tags=language,title','-of','json',str(target)],capture_output=True,text=True,check=True)
    assert any(s.get('tags',{}).get('language') in ('fas','per','fa') and s.get('tags',{}).get('title')=='Animex' for s in json.loads(probe.stdout)['streams'])
    files.append(target)
   subtitles=[p for p in source.iterdir() if p.suffix.lower() in ('.ass','.srt','.ssa')];assert len(subtitles)==1
   subtitle_name=re.sub(r'\.\[.*$', '',next(iter(row['output_names'].values())))+subtitles[0].suffix
   subtarget=staging/subtitle_name
   if not subtarget.exists():os.link(subtitles[0],subtarget)
   files.append(subtarget)
   ep.manifest={**(ep.manifest or {}),'upload_status':'processing','upload_destination':row['destination'],'recovery_original_names':row['output_names'],'upload_batch':str(staging)}
   ep.mux_status='processing';ep.error=None;db.commit()
   print('UPLOADING',eid,flush=True)
   _upload_batch(staging,expected_count=len(files),destination=row['destination'],flat=True)
   checked=[]
   from urllib.parse import quote
   for file in files:
    dest=row['destination'];url='https://rdl1.hollowofthealley.space/'+quote(dest['location']+'/'+dest['folder']+'/'+file.name,safe='/')
    with urllib.request.urlopen(urllib.request.Request(url,method='HEAD'),timeout=30) as response:
     size=int(response.headers.get('Content-Length','-1'));assert size==file.stat().st_size,(file.name,size,file.stat().st_size)
     checked.append({'file':file.name,'url':url,'bytes':size,'http_status':response.status})
   db.refresh(ep);ep.manifest={**(ep.manifest or {}),'upload_status':'completed','softsub_outputs':[str(f) for f in files if f.suffix=='.mkv'],'rdl1_verified_files':checked,'recovery_uploaded_at':time.time()};ep.mux_status='completed';ep.status='completed';ep.error=None;db.commit()
   results.append({'episode_id':eid,'verified':True,'files':checked});print('VERIFIED',eid,len(checked),flush=True)
 except Exception as exc:
  # Avoid exposing uploader output or credential-bearing commands.
  with SessionLocal() as db:
   ep=db.get(AutomationEpisode,eid);ep.mux_status='failed';ep.error='Recovery upload failed; inspect restricted task log';ep.manifest={**(ep.manifest or {}),'upload_status':'failed'};db.commit()
  results.append({'episode_id':eid,'verified':False,'error_type':type(exc).__name__});print('FAILED',eid,type(exc).__name__,flush=True)
 REPORT.write_text(json.dumps(results,ensure_ascii=False,indent=2))
print('UPLOAD_BATCH_FINISHED',flush=True)
