const GetMeQueryHandler = require('../../../src/application/user/queries/GetMeQueryHandler');
const GetMeQuery = require('../../../src/application/user/queries/GetMeQuery');

describe('GetMeQueryHandler', () => {
  let handler;
  let mockPrisma;

  beforeEach(() => {
    // Mock Prisma
    mockPrisma = {
      user: {
        findUnique: jest.fn()
      }
    };

    handler = new GetMeQueryHandler(mockPrisma);

    // Reset all mocks
    jest.clearAllMocks();
  });

  describe('handle - success cases', () => {
    it('should return current user successfully', async () => {
      // Arrange
      const query = new GetMeQuery(1);
      
      mockPrisma.user.findUnique.mockResolvedValue({
        id: 1,
        name: 'John Doe',
        email: 'john@example.com',
        password: 'hashed_password',
        createdAt: new Date(),
        updatedAt: new Date()
      });

      // Act
      const result = await handler.handle(query);

      // Assert
      expect(mockPrisma.user.findUnique).toHaveBeenCalledWith({
        where: { id: 1 }
      });
      expect(result).toEqual({
        id: 1,
        name: 'John Doe',
        email: 'john@example.com',
        createdAt: expect.any(Date),
        updatedAt: expect.any(Date)
      });
      expect(result.password).toBeUndefined(); // Password should not be returned
    });
  });

  describe('handle - error cases', () => {
    it('should throw error if user not found', async () => {
      // Arrange
      const query = new GetMeQuery(999);
      
      mockPrisma.user.findUnique.mockResolvedValue(null);

      // Act & Assert
      await expect(handler.handle(query)).rejects.toThrow('User not found');
    });
  });
});
