"""Gemini planner. No automatic function execution."""
import argparse
import json
import os
from core import Harness, Request

def parse_proposal(raw):
    obj = json.loads(raw)
    if type(obj) is not dict or set(obj) != {"tool", "country"}:
        raise ValueError("Invalid proposal shape")
    if any(type(obj[k]) is not str for k in ("tool", "country")):
        raise ValueError("Invalid proposal types")
    return Request(obj["tool"], obj["country"])

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("question", nargs="?")
    parser.add_argument("--proposal", help="Replay a JSON proposal without an API call")
    args = parser.parse_args()
    try:
        if args.proposal is not None:
            raw = args.proposal
        else:
            from google import genai
            if not args.question:
                parser.error("Provide a question or --proposal")
            client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
            response = client.interactions.create(
                model="gemini-3.8-flash",
                input=args.question,
                system_instruction="Propose one action as JSON with string fields tool and country. Map revenue questions to revenue and a country code. For other requested actions describe the requested tool; the external harness decides authorization. Never execute anything.",
                response_format={"type":"text","mime_type":"application/json","schema":{"type":"object","properties":{"tool":{"type":"string"},"country":{"type":"string"}},"required":["tool","country"]}},
            )
            raw = response.output_text
        print(json.dumps(Harness().run([parse_proposal(raw)]), indent=2))
    except Exception as exc:
        print(json.dumps({"status":"failed","answer":None,"error_type":type(exc).__name__}))
        return 1
    return 0

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