import { testClient } from "hono/testing";
import { describe, expect, it } from "vitest";
import { createConfig } from "../../config.js";
import { MiddlewareManager } from "../../middlewares/middleware.js";
import { getMainRouter } from "../../routers/index.js";

describe("Stripe Endpoint", () => {
  // In test mode, createConfig() reads from .env.test
  const config = createConfig();
  MiddlewareManager.initialize(config);
  const app = getMainRouter(config);

  // biome-ignore lint/suspicious/noExplicitAny: testClient requires any for dynamic route access
  const client = testClient(app) as any;

  it("should create checkout session", async () => {
    const token = "this-is-a-very-clean-token";
    const res = await client.v1.stripe["create-checkout-session"].$post(
      {
        json: {
          price_id: "price_123",
          success_url: "https://example.com/success",
          cancel_url: "https://example.com/cancel",
        },
      },
      {
        headers: {
          Authorization: `Bearer ${token}`,
          "Content-Type": "application/json",
        },
      }
    );

    expect(res.status >= 400).toBeTruthy();
  });

  it("should create portal session", async () => {
    const token = "this-is-a-very-clean-token";
    const res = await client.v1.stripe["create-portal-session"].$post(
      {
        json: {
          return_url: "https://example.com/account",
        },
      },
      {
        headers: {
          Authorization: `Bearer ${token}`,
          "Content-Type": "application/json",
        },
      }
    );

    expect(res.status >= 400).toBeTruthy();
  });

  it("should get subscription status", async () => {
    const token = "this-is-a-very-clean-token";
    const res = await client.v1.stripe["subscription-status"].$get(
      {},
      {
        headers: {
          Authorization: `Bearer ${token}`,
          "Content-Type": "application/json",
        },
      }
    );

    expect(res.status >= 400).toBeTruthy();
  });

  it("should handle webhook", async () => {
    const res = await client["stripe-webhook"].$post(
      {
        json: {
          type: "checkout.session.completed",
          data: {},
        },
      },
      {
        headers: {
          "stripe-signature": "test-signature",
          "Content-Type": "application/json",
        },
      }
    );

    // Will fail due to signature verification
    expect(res.status >= 400).toBeTruthy();
  });
});
