"""
run_full_sync_pipeline_jsonl_sm.py

独立执行：基于 sync_state_machine 的 JSONL -> JSONL 全量同步脚本。
"""

from __future__ import annotations

import asyncio
import sys
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent.parent
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

import sync_state_machine.domain  # noqa: F401, E402
from sync_state_machine.pipeline import (  # noqa: E402
    build_config,
    run_pipeline_from_config,
)
from sync_state_machine.config import load_named_preset_overrides  # noqa: E402


PRESET_NAME = "jsonl_sm"


async def main() -> int:
    try:
        overrides, preset_file, used_default = load_named_preset_overrides(PROJECT_ROOT, PRESET_NAME)
        source_label = "default" if used_default else "custom"
        print(f"🧩 Loaded preset ({source_label}): {preset_file}")

        config = build_config(project_root=PROJECT_ROOT, overrides=overrides)
        await run_pipeline_from_config(
            config,
            title="sync_state_machine JSONL -> JSONL pipeline",
            print_summary=True,
        )
        return 0

    except KeyboardInterrupt:
        print("\n⚠️ Interrupted by user")
        return 130
    except Exception as exc:
        print(f"\n❌ Pipeline failed: {type(exc).__name__}: {exc}")
        import traceback
        traceback.print_exc()
        return 1


if __name__ == "__main__":
    raise SystemExit(asyncio.run(main()))
