from __future__ import annotations

from pathlib import Path

import typer

from companion.config import load_config
from companion.rag.indexer import Indexer
from companion.rag.vector_store import VectorStore

app = typer.Typer(help="Companion vault indexer")


def _get_indexer() -> Indexer:
    config = load_config("config.json")
    store = VectorStore(
        uri=config.rag.vector_store.path, dimensions=config.rag.embedding.dimensions
    )
    return Indexer(config, store)


@app.command()
def index() -> None:
    """Run a full index of the vault."""
    indexer = _get_indexer()
    typer.echo("Running full index...")
    indexer.full_index()
    typer.echo(f"Done. Total chunks: {indexer.status()['total_chunks']}")


@app.command()
def sync() -> None:
    """Run an incremental sync."""
    indexer = _get_indexer()
    typer.echo("Running incremental sync...")
    indexer.sync()
    typer.echo(f"Done. Total chunks: {indexer.status()['total_chunks']}")


@app.command()
def reindex() -> None:
    """Force a full reindex (same as index)."""
    index()


@app.command()
def status() -> None:
    """Show indexer status."""
    indexer = _get_indexer()
    s = indexer.status()
    typer.echo(f"Total chunks: {s['total_chunks']}")
    typer.echo(f"Indexed files: {s['indexed_files']}")
    typer.echo(f"Unindexed files: {s['unindexed_files']}")


if __name__ == "__main__":
    app()
