"""Package Suite27 useful reports and provenance, optionally including predictions."""
from __future__ import annotations

import argparse
import hashlib
import json
from pathlib import Path
import zipfile


REPORT_SUFFIXES = {'.csv', '.json', '.log', '.txt', '.md', '.tsv'}


def collect(results_dir, out_file, include_predictions=False):
    root, target = Path(results_dir).resolve(), Path(out_file).resolve()
    if not root.is_dir():
        raise FileNotFoundError(f'Results directory missing: {root}')
    if target.exists():
        raise FileExistsError(f'Collector output already exists: {target}')
    paths = []
    for path in sorted(root.rglob('*')):
        if not path.is_file() or path.suffix.lower() not in REPORT_SUFFIXES or path == target or path.name == 'COLLECTION_MANIFEST.json':
            continue
        lower = path.name.lower()
        predictions = 'prediction' in lower or lower.endswith('_scores.csv') or lower in ('scores.csv', 'target_scores.csv')
        if predictions and not include_predictions:
            continue
        paths.append(path)
    if not paths:
        raise ValueError(f'No useful reports found in {root}')
    target.parent.mkdir(parents=True, exist_ok=True)
    temporary = target.with_name(target.name + '.tmp')
    manifest = dict(format_version=1, method='archive_existing_reports_without_recomputation',
                    results_dir=str(root), include_predictions=bool(include_predictions),
                    weightsExcluded=True, inventory=[])
    try:
        with zipfile.ZipFile(temporary, 'w', compression=zipfile.ZIP_DEFLATED) as archive:
            for path in paths:
                relative = path.relative_to(root).as_posix()
                payload = path.read_bytes()
                archive.writestr(relative, payload)
                manifest['inventory'].append(dict(path=relative, bytes=len(payload),
                                                  sha256=hashlib.sha256(payload).hexdigest()))
            archive.writestr('COLLECTION_MANIFEST.json', json.dumps(manifest, indent=2, sort_keys=True) + '\n')
        temporary.replace(target)
    finally:
        if temporary.exists():
            temporary.unlink()
    print(f'Collected {len(paths)} report/provenance files to {target}', flush=True)
    return target


def main(argv=None):
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument('--results-dir', required=True)
    p.add_argument('--out-file', required=True)
    p.add_argument('--include-predictions', action='store_true')
    args = p.parse_args(argv)
    return collect(args.results_dir, args.out_file, args.include_predictions)


if __name__ == '__main__':
    main()
