import { z } from "zod";
import { tool } from "@anthropic-ai/claude-agent-sdk";
import { getAgentState } from "../agent/state.js";
const MAX_SELF_AUTHORIZED_CENTS = 200_00;
export const manageCardTool = tool("manage_card", "Manage your credit card: check card info (PAN, CVV, expiry), set a temporary per-transaction spending limit before making a purchase, or view recent transactions.", {
    action: z
        .enum(["get_info", "raise_limit", "get_transactions"])
        .describe("Action to perform. get_info: full card details including PAN/CVV. raise_limit: set a temporary per-transaction limit for the next 30 minutes. get_transactions: list recent transactions."),
    spend_limit: z
        .number()
        .optional()
        .describe("Temporary per-transaction spend limit in USD cents (required for raise_limit, expires after 30 minutes)"),
    reason: z
        .string()
        .optional()
        .describe("Reason for the limit change (optional, logged for audit)"),
}, async (args) => {
    const state = getAgentState();
    const { config } = state;
    if (!config.onboardingServiceUrl || !config.visionclawServiceApiKey) {
        return {
            content: [{ type: "text", text: "Error: Onboarding service URL or API key not configured." }],
            isError: true,
        };
    }
    const baseUrl = config.onboardingServiceUrl.replace(/\/+$/, "");
    const agentName = encodeURIComponent(config.agentName);
    const headers = {
        Authorization: `Bearer ${config.visionclawServiceApiKey}`,
    };
    try {
        switch (args.action) {
            case "get_info": {
                const res = await fetch(`${baseUrl}/api/agents/${agentName}/card/info`, { headers, signal: AbortSignal.timeout(15_000) });
                if (!res.ok) {
                    const err = await res.json().catch(() => ({ error: res.statusText }));
                    return { content: [{ type: "text", text: `Error (${res.status}): ${err.error ?? res.statusText}` }], isError: true };
                }
                const info = await res.json();
                return { content: [{ type: "text", text: JSON.stringify(info, null, 2) }] };
            }
            case "raise_limit": {
                if (typeof args.spend_limit !== "number" || args.spend_limit <= 0) {
                    return { content: [{ type: "text", text: "Error: spend_limit is required (positive integer, USD cents)" }], isError: true };
                }
                if (args.spend_limit > MAX_SELF_AUTHORIZED_CENTS) {
                    return {
                        content: [{ type: "text", text: `Error: Cannot self-authorize more than $${MAX_SELF_AUTHORIZED_CENTS / 100}. Maximum is ${MAX_SELF_AUTHORIZED_CENTS} cents.` }],
                        isError: true,
                    };
                }
                const body = { spend_limit: args.spend_limit };
                if (args.reason)
                    body.reason = args.reason;
                const res = await fetch(`${baseUrl}/api/agents/${agentName}/card/raise-limit`, {
                    method: "POST",
                    headers: { ...headers, "Content-Type": "application/json" },
                    body: JSON.stringify(body),
                    signal: AbortSignal.timeout(15_000),
                });
                if (!res.ok) {
                    const err = await res.json().catch(() => ({ error: res.statusText }));
                    return { content: [{ type: "text", text: `Error (${res.status}): ${err.error ?? res.statusText}` }], isError: true };
                }
                const result = await res.json();
                const typed = result;
                return { content: [{ type: "text", text: `Temporary per-transaction limit set to ${typed.spend_limit} cents. It expires at ${typed.expires_at ?? "the server-configured expiry"}.` }] };
            }
            case "get_transactions": {
                const res = await fetch(`${baseUrl}/api/agents/${agentName}/card/transactions`, { headers, signal: AbortSignal.timeout(15_000) });
                if (!res.ok) {
                    const err = await res.json().catch(() => ({ error: res.statusText }));
                    return { content: [{ type: "text", text: `Error (${res.status}): ${err.error ?? res.statusText}` }], isError: true };
                }
                const data = await res.json();
                return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
            }
        }
    }
    catch (err) {
        return {
            content: [{ type: "text", text: `Failed to call card API: ${err instanceof Error ? err.message : String(err)}` }],
            isError: true,
        };
    }
});
//# sourceMappingURL=card.js.map