from __future__ import annotations

import argparse

from app.config import Settings
from app.storage import Store


def main() -> int:
    parser = argparse.ArgumentParser(description="Reconcile a worker run after independently confirming it stopped")
    parser.add_argument("--run-id", required=True)
    parser.add_argument("--confirm-process-stopped", action="store_true")
    args = parser.parse_args()
    if not args.confirm_process_stopped:
        parser.error("--confirm-process-stopped is required after checking the worker and sync subprocess are stopped")
    settings = Settings.from_env()
    store = Store(settings.database_path)
    store.initialize()
    if not store.reconcile(args.run_id, "Marked failed after operator confirmed the process is stopped; rerun only after reviewing destination tables."):
        print("No uncertain running execution matched that run_id.")
        return 1
    print("Run marked failed. Review worker logs and ClickHouse before starting another run.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
