import { describe, it, expect, beforeEach, vi } from "vitest";
import { Test, TestingModule } from "@nestjs/testing";
import type { AuthUser } from "@mosaic/shared";
import { AuthController } from "./auth.controller";
import { AuthService } from "./auth.service";

describe("AuthController", () => {
  let controller: AuthController;
  let authService: AuthService;

  const mockAuthService = {
    getAuth: vi.fn(),
  };

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      controllers: [AuthController],
      providers: [
        {
          provide: AuthService,
          useValue: mockAuthService,
        },
      ],
    }).compile();

    controller = module.get<AuthController>(AuthController);
    authService = module.get<AuthService>(AuthService);

    vi.clearAllMocks();
  });

  describe("handleAuth", () => {
    it("should call BetterAuth handler", async () => {
      const mockHandler = vi.fn().mockResolvedValue({ status: 200 });
      mockAuthService.getAuth.mockReturnValue({ handler: mockHandler });

      const mockRequest = {
        method: "GET",
        url: "/auth/session",
      };

      await controller.handleAuth(mockRequest);

      expect(mockAuthService.getAuth).toHaveBeenCalled();
      expect(mockHandler).toHaveBeenCalledWith(mockRequest);
    });
  });

  describe("getProfile", () => {
    it("should return user profile", () => {
      const mockUser: AuthUser = {
        id: "user-123",
        email: "test@example.com",
        name: "Test User",
      };

      const result = controller.getProfile(mockUser);

      expect(result).toEqual({
        id: mockUser.id,
        email: mockUser.email,
        name: mockUser.name,
      });
    });
  });
});
