"""
Tiny CLI: call Inpainter server's inpaint tool over MCP stdio.

Usage:
  python -X utf8 tools/inpaint_image.py data/example.jpeg artifacts/tmp/example_mask.png --prompt "smooth matte patch ."
"""
from __future__ import annotations

import argparse
import asyncio
import json
import sys
from pathlib import Path

from mcp.client.session import ClientSession  # type: ignore
from mcp.client.stdio import stdio_client, StdioServerParameters  # type: ignore


async def main() -> int:
    p = argparse.ArgumentParser(description="Inpaint via InpainterServer")
    p.add_argument("image", help="Path to input image")
    p.add_argument("mask", help="Path to mask image")
    p.add_argument("--prompt", required=True, help="Inpainting prompt")
    args = p.parse_args()

    ip = Path(args.image)
    mp = Path(args.mask)
    if not ip.exists():
        print(f"error: image not found: {ip}", file=sys.stderr)
        return 2
    if not mp.exists():
        print(f"error: mask not found: {mp}", file=sys.stderr)
        return 2

    params = StdioServerParameters(
        command=sys.executable,
        args=["-m", "mcp.servers.inpainter_server"],
    )
    async with stdio_client(params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()
            res = await session.call_tool(
                "inpaint",
                {"image_path": str(ip), "mask_path": str(mp), "prompt": args.prompt},
            )
            print(json.dumps(res, ensure_ascii=False, indent=2))
    return 0


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