/**
 * Smoke Test: Billing Authentication Regression Prevention
 *
 * Quick validation that billing APIs support dual authentication
 * Run this test before every deployment to catch auth regressions
 *
 * Duration: < 5 seconds
 *
 * @see BILLING_AUTH_FIX.md
 */

import {
  createAuthTokenCookie,
  createMockNextAuthSession,
  TEST_USER,
} from "../helpers/auth-test-helpers";

// Code-analysis tests for auth patterns - files migrated to Convex
describe.skip("Smoke Test: Billing API Dual Auth", () => {
  const CRITICAL_ENDPOINTS = [
    "/api/subscription/details",
    "/api/user/package-status",
    "/api/billing/current",
  ];

  describe("Helper Functions Exist", () => {
    it("should have getAuthenticatedEmail helper", async () => {
      const { getAuthenticatedEmail } =
        await import("@/lib/utils/auth-helpers.util");
      expect(typeof getAuthenticatedEmail).toBe("function");
    });

    it("should have getAuthenticatedUser helper", async () => {
      const { getAuthenticatedUser } =
        await import("@/lib/utils/auth-helpers.util");
      expect(typeof getAuthenticatedUser).toBe("function");
    });
  });

  describe("Auth Token Format", () => {
    it("should create valid auth_token cookie", () => {
      const authToken = createAuthTokenCookie(
        TEST_USER.id,
        TEST_USER.email,
        TEST_USER.name,
      );

      expect(authToken).toBeTruthy();
      expect(typeof authToken).toBe("string");

      // Should be valid base64
      const decoded = JSON.parse(Buffer.from(authToken, "base64").toString());
      expect(decoded.userId).toBe(TEST_USER.id);
      expect(decoded.email).toBe(TEST_USER.email);
      expect(decoded.timestamp).toBeTruthy();
    });

    it("should create valid NextAuth session", () => {
      const session = createMockNextAuthSession(TEST_USER.id, TEST_USER.email);

      expect(session.user.id).toBe(TEST_USER.id);
      expect(session.user.email).toBe(TEST_USER.email);
      expect(session.expires).toBeTruthy();
    });
  });

  describe("Critical Billing APIs Exist", () => {
    it("should have /api/subscription/details route file", () => {
      const fs = require("fs");
      const path = require("path");

      const filePath = path.join(
        process.cwd(),
        "app/api/subscription/details/route.ts",
      );
      expect(fs.existsSync(filePath)).toBe(true);
    });

    it("should have /api/user/package-status route file", () => {
      const fs = require("fs");
      const path = require("path");

      const filePath = path.join(
        process.cwd(),
        "app/api/user/package-status/route.ts",
      );
      expect(fs.existsSync(filePath)).toBe(true);
    });

    it("should have /api/billing/current route file", () => {
      const fs = require("fs");
      const path = require("path");

      const filePath = path.join(
        process.cwd(),
        "app/api/billing/current/route.ts",
      );
      expect(fs.existsSync(filePath)).toBe(true);
    });
  });

  describe("Billing APIs Use Dual Auth Helpers", () => {
    it("/api/subscription/details should import getAuthenticatedEmail", async () => {
      const fs = require("fs");
      const path = require("path");

      const filePath = path.join(
        process.cwd(),
        "app/api/subscription/details/route.ts",
      );
      const content = fs.readFileSync(filePath, "utf-8");

      // Verify it imports the helper
      expect(content).toContain("getAuthenticatedEmail");
      expect(content).toContain("from '@/lib/utils/auth-helpers.util'");
    });

    it("/api/user/package-status should import getAuthenticatedEmail", async () => {
      const fs = require("fs");
      const path = require("path");

      const filePath = path.join(
        process.cwd(),
        "app/api/user/package-status/route.ts",
      );
      const content = fs.readFileSync(filePath, "utf-8");

      expect(content).toContain("getAuthenticatedEmail");
      expect(content).toContain("from '@/lib/utils/auth-helpers.util'");
    });

    it("/api/billing/current should import getAuthenticatedUser", async () => {
      const fs = require("fs");
      const path = require("path");

      const filePath = path.join(
        process.cwd(),
        "app/api/billing/current/route.ts",
      );
      const content = fs.readFileSync(filePath, "utf-8");

      expect(content).toContain("getAuthenticatedUser");
      expect(content).toContain("from '@/lib/utils/auth-helpers.util'");
    });
  });

  describe("Documentation", () => {
    it("should have BILLING_AUTH_FIX.md", () => {
      const fs = require("fs");
      const path = require("path");

      const docPath = path.join(process.cwd(), "BILLING_AUTH_FIX.md");
      expect(fs.existsSync(docPath)).toBe(true);
    });
  });
});

/**
 * Exit code for CI/CD pipelines
 */
afterAll(() => {
  console.log("\n✅ Billing auth regression smoke tests passed\n");
});
