import { describe, it, expect, beforeEach, vi } from "vitest";
import { GET } from "@/app/api/stripe/connect/status/route";
import { db } from "@/lib/db";
import { auth } from "@/lib/auth";

vi.mock("@/lib/db", () => ({
  db: {
    user: { findUnique: vi.fn() },
  },
}));

vi.mock("@/lib/auth", () => ({
  auth: vi.fn(),
}));

describe("GET /api/stripe/connect/status", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns 401 when not authenticated", async () => {
    vi.mocked(auth).mockResolvedValue(null as never);

    const res = await GET();
    const data = await res.json();

    expect(res.status).toBe(401);
    expect(data.error).toBe("Unauthorized");
    expect(db.user.findUnique).not.toHaveBeenCalled();
  });

  it("returns connected: false when user has no Connect account", async () => {
    vi.mocked(auth).mockResolvedValue({ user: { id: "user_1" } } as never);
    vi.mocked(db.user.findUnique).mockResolvedValue({
      stripeConnectAccountId: null,
    } as never);

    const res = await GET();
    const data = await res.json();

    expect(res.status).toBe(200);
    expect(data.connected).toBe(false);
    expect(data.accountId).toBeUndefined();
  });

  it("returns connected: true and accountId when user has Connect account", async () => {
    vi.mocked(auth).mockResolvedValue({ user: { id: "user_1" } } as never);
    vi.mocked(db.user.findUnique).mockResolvedValue({
      stripeConnectAccountId: "acct_express123",
    } as never);

    const res = await GET();
    const data = await res.json();

    expect(res.status).toBe(200);
    expect(data.connected).toBe(true);
    expect(data.accountId).toBe("acct_express123");
  });
});
