import * as React from "react";
import renderer from "react-test-renderer";
import {
  BillingPaywall,
  type BillingPackageOption,
  type BillingPaywallProps,
} from "../BillingPaywall";

const basePackages: BillingPackageOption[] = [
  {
    description: "Parfait pour une personne",
    id: "solo_ios_monthly",
    package: {} as BillingPackageOption["package"],
    plan: "solo",
    price: "4,99 €",
    title: "Solo mensuel",
  },
  {
    description: "Accès annuel complet",
    id: "annual_ios",
    package: {} as BillingPackageOption["package"],
    plan: "annual",
    price: "49,99 €",
    title: "Annuel",
  },
];

const baseProps: BillingPaywallProps = {
  canManageBillingInApp: true,
  errorMessage: null,
  isLoadingPackages: false,
  isRestoring: false,
  isSyncing: false,
  onPurchase: jest.fn(),
  onRestore: jest.fn(),
  packages: basePackages,
  requiredPlan: "solo",
};

const renderToText = async (props?: Partial<BillingPaywallProps>) => {
  let component: renderer.ReactTestRenderer;

  await renderer.act(async () => {
    component = renderer.create(<BillingPaywall {...baseProps} {...props} />);
  });

  return JSON.stringify(component!.toJSON());
};

describe("BillingPaywall", () => {
  it("renders Apple purchase actions and restore CTA for eligible owners", async () => {
    const output = await renderToText();

    expect(output).toContain("Débloquer XTablo sur iPhone");
    expect(output).toContain("Solo mensuel");
    expect(output).toContain("Restaurer mes achats");
  });

  it("shows a contact-owner message when the user cannot purchase in app", async () => {
    const output = await renderToText({
      canManageBillingInApp: false,
    });

    expect(output).toContain("Votre organisation n'a pas encore d'accès actif.");
    expect(output).toContain("demandez au responsable");
    expect(output).not.toContain("Restaurer mes achats");
  });

  it("surfaces the pending sync state after a successful store action", async () => {
    const output = await renderToText({
      isSyncing: true,
    });

    expect(output).toContain("Achat reçu");
    expect(output).toContain("synchronisation");
  });

  it("keeps team purchases on the web in v1", async () => {
    const output = await renderToText({
      packages: basePackages.filter((pkg) => pkg.plan === "annual"),
      requiredPlan: "team",
    });

    expect(output).toContain("Les forfaits équipe se gèrent sur le web");
    expect(output).toContain("Annuel");
    expect(output).not.toContain("Solo mensuel");
  });
});
