const UpdateUserProfileCommandHandler = require('../../../src/application/user/commands/UpdateUserProfileCommandHandler');
const UpdateUserProfileCommand = require('../../../src/application/user/commands/UpdateUserProfileCommand');

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

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

    handler = new UpdateUserProfileCommandHandler(mockPrisma);

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

  describe('handle - success cases', () => {
    it('should update user profile successfully', async () => {
      // Arrange
      const command = new UpdateUserProfileCommand(1, 'Jane Updated');
      
      mockPrisma.user.update.mockResolvedValue({
        id: 1,
        name: 'Jane Updated',
        email: 'jane@example.com',
        password: 'hashed_password',
        createdAt: new Date(),
        updatedAt: new Date()
      });

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

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

  describe('handle - validation errors', () => {
    it('should throw error if name is missing', async () => {
      // Arrange
      const command = new UpdateUserProfileCommand(1, '');

      // Act & Assert
      await expect(handler.handle(command)).rejects.toThrow('Name is required');
    });

    it('should throw error if name is null', async () => {
      // Arrange
      const command = new UpdateUserProfileCommand(1, null);

      // Act & Assert
      await expect(handler.handle(command)).rejects.toThrow('Name is required');
    });

    it('should throw error if name is undefined', async () => {
      // Arrange
      const command = new UpdateUserProfileCommand(1, undefined);

      // Act & Assert
      await expect(handler.handle(command)).rejects.toThrow('Name is required');
    });
  });
});
