import React from "react";
import "@testing-library/jest-dom";
import { render, screen } from "@testing-library/react";
import AiUsageCard from "./components/AiUsageCard";
import { api } from "./api";

jest.mock("./api", () => ({ api: { get: jest.fn(), post: jest.fn() } }));
jest.mock("./i18n/I18nProvider", () => ({ useI18n: () => ({ t: (key: string, params?: Record<string, unknown>) => {
  const messages: Record<string, string> = {
    settingsUsageTitle: "Account usage",
    settingsUsageUnavailable: "Account usage is temporarily unavailable.",
    settingsUsagePlan: "{plan} plan",
    settingsUsageGenerations: "{used} of {limit} generations this month",
    settingsUsageTokens: "{used} of {limit} estimated tokens",
    settingsUsageStorage: "{used} of {limit} attachment storage",
    settingsUsageReset: "AI limits reset at the start of each calendar month.",
    settingsUsageNoAi: "The Free plan includes core job tracking without AI. Upgrade to Pro to use AI features.",
    settingsBillingUpgrade: "Upgrade to Pro",
  };
  return Object.entries(params ?? {}).reduce((text, [name, value]) => text.replace(`{${name}}`, String(value)), messages[key] ?? key);
} }) }));

test("shows monthly AI call and token limits", async () => {
  (api.get as jest.Mock).mockImplementation((url: string) => Promise.resolve({
    data: url === "/billing/status" ? { enabled: true, canCheckout: true, canManage: false } : {
      currentMonth: { calls: 4, estimatedTokens: 12000 },
      plan: "free",
      monthlyCallLimit: 0,
      monthlyTokenLimit: 0,
      storageUsedBytes: 50000000,
      storageLimitBytes: 250000000,
    },
  }));

  render(<AiUsageCard />);

  expect(await screen.findByText(/Free plan includes core job tracking/)).toBeInTheDocument();
  expect(screen.getByText("50.0 MB of 250.0 MB attachment storage")).toBeInTheDocument();
  expect(screen.queryByLabelText("Monthly AI generations used")).not.toBeInTheDocument();
  expect(screen.getByRole("button", { name: "Upgrade to Pro" })).toBeInTheDocument();
});
