from __future__ import annotations

import sys
from pathlib import Path

_agent_root = Path(__file__).resolve().parents[3]
if str(_agent_root) not in sys.path:
    sys.path.insert(0, str(_agent_root))
_src = _agent_root / "src"
if _src.exists() and str(_src) not in sys.path:
    sys.path.insert(0, str(_src))

from app.modules.rag.persistence.repository import RagRepository
from tests.pipeline_setup_v3.shared.env_loader import load_pipeline_setup_env
from tests.pipeline_setup_v3.shared.rag_indexer import RagSessionIndexer


def main() -> int:
    import argparse

    parser = argparse.ArgumentParser(description="Index a repo for pipeline_setup_v3")
    parser.add_argument("--repo-path", required=True, help="Repository path to index")
    parser.add_argument("--project-id", default=None, help="Optional project id override")
    ns = parser.parse_args(sys.argv[1:])

    load_pipeline_setup_env(start_dir=Path(__file__).resolve().parents[1])
    repo_path = Path(str(ns.repo_path)).expanduser().resolve()
    RagSessionIndexer(RagRepository()).index_repo(repo_path, project_id=ns.project_id)
    return 0


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