from pathlib import Path
from anthropic import Anthropic

# For local testing only.
API_KEY = "REDACTED"
PROMPT_FILE = "input.txt"
OUTPUT_FILE = "output.txt"


def math_problem():
    client = Anthropic(api_key=API_KEY)

    prompt = Path(PROMPT_FILE).read_text(encoding="utf-8")

    response = client.messages.create(
        model="claude-sonnet-5",
        max_tokens=500,
        system=(
            "You are a helpful math tutor. "
            "Respond in plain text only. "
            "Do not use Markdown, headings, bullet points, or LaTeX."
        ),
        messages=[
            {
                "role": "user",
                "content": prompt
            }
        ]
    )

    output_text = next(
    (block.text for block in response.content if block.type == "text"),"")

    with open("input.txt", "w") as f:
        f.write(prompt)

    with open("output.txt", "w") as f:
        for block in response.content:
            if block.type == "text":
                f.write(block.text)

    print("Prompt:")
    print(prompt)
    print("\nClaude's output:")
    print(output_text)
    print(f"\nOutput saved to {OUTPUT_FILE}")


if __name__ == "__main__":
    math_problem()