"""Select the sample again from the catalogs under data/meta and check it is the one the search used.

python verify_selection.py <meta dir> <data dir> <results dir>
"""
import json
import os
import subprocess
import sys
import tempfile

import numpy as np
import pandas as pd

meta, data, out = sys.argv[1], sys.argv[2], sys.argv[3]
os.makedirs(out, exist_ok=True)
with tempfile.TemporaryDirectory() as tmp:
    log = subprocess.run([sys.executable, os.path.join(os.path.dirname(__file__), 'select_sample.py'), meta, tmp],
                         check=True, capture_output=True, text=True).stdout
    res = {}
    for name in ('sample_stars', 'sample_lcs'):
        mine = pd.read_csv(os.path.join(tmp, name + '.csv'))
        p = os.path.join(data, name + '.csv')
        theirs = pd.read_csv(p if os.path.exists(p) else p + '.gz')
        same = list(mine.columns) == list(theirs.columns) and len(mine) == len(theirs)
        if same:
            for c in mine.columns:
                a, b = mine[c], theirs[c]
                if pd.api.types.is_float_dtype(a) and pd.api.types.is_float_dtype(b):
                    # Platforms' math libraries can round the last digit differently
                    same &= bool(np.allclose(a.values, b.values, rtol=1e-12, atol=0, equal_nan=True))
                else:
                    same &= bool(a.equals(b))
        res[name] = {'rows': int(len(mine)), 'identical': bool(same)}
    stars = pd.read_csv(os.path.join(tmp, 'sample_stars.csv'))
    lcs = pd.read_csv(os.path.join(tmp, 'sample_lcs.csv'))
    dev = set(stars.tic[stars.dev])
    res['stars'] = int(len(stars))
    res['light_curves'] = int(len(lcs))
    res['dev_stars'] = int(len(dev))
    res['dev_light_curves'] = int(lcs.tic.isin(dev).sum())
    res['main_stars'] = res['stars'] - res['dev_stars']
    res['main_light_curves'] = res['light_curves'] - res['dev_light_curves']
    res['steps'] = [line.strip() for line in log.splitlines() if line.strip()]
with open(os.path.join(out, 'selection.json'), 'w') as fh:
    json.dump(res, fh, indent=1, sort_keys=True)
print(json.dumps(res, indent=1))
