#!/usr/bin/env python3
from __future__ import annotations

import os
import sys
import dotenv
from pathlib import Path
import openai

sys.path.append(str(Path(__file__).parent.parent))
from subagent_generator import SubagentGenerator
from tools import ToolArchive


def main() -> None:
	# Env and paths
	dotenv.load_dotenv()
	root = Path("SWE-agent")
	test_dir = root / "tool_gen/evaluation/tests/test30_zeroshot_sub"
	prompt_dir = root / "tool_gen/prompts"
	prompt_filename = "generate_subagent_parts_prompt.txt"
	template_dir = root / "tool_gen/seed_oss_36b"

	# Output subagent bundles into this test's tools folder
	output_dir = test_dir / "tools"
	output_dir.mkdir(parents=True, exist_ok=True)

	# Configure Claude client via OpenAI-compatible SDK routing to Anthropic
	# Requires ANTHROPIC_API_KEY in environment
	api_base ="https://api.anthropic.com/v1"
	api_key = os.getenv("ANTHROPIC_API_KEY")
	if not api_key:
		raise RuntimeError("ANTHROPIC_API_KEY not set")

	client = openai.Client(base_url=api_base, api_key=api_key)

	model_name = "claude-sonnet-4-20250514"

	# Initialize generator and archive
	archive = ToolArchive(output_dir=output_dir)
	generator = SubagentGenerator(
		client=client,
		prompt_dir=prompt_dir,
		prompt_filename=prompt_filename,
		template_dir=template_dir,
		output_dir=output_dir,
		model_name=model_name,
		temperature=0.0,
	)
	print("Generating subagents...")
	# Generate exactly 2 subagents, no warmup
	created = []
	for i in range(2):
		tool = generator.generate_new_tool(tool_archive=archive, exp_num=1)
		archive.add_tool(tool)
		created.append(tool.name)
		print(f"Generated subagent: {tool.name}")

	print(f"Created subagents: {created}")


if __name__ == "__main__":
	main()
