import { Test, TestingModule } from '@nestjs/testing';
import { ConflictException, NotFoundException } from '@nestjs/common';
import { HotelService } from './hotel.service';
import { HotelRepository } from './repositories/hotel.repository';

describe('HotelService', () => {
  let service: HotelService;

  const mockRepo = {
    findAllWithDestination: jest.fn(),
    findByIdWithDestination: jest.fn(),
    findByNameAndDestination: jest.fn(),
    findById: jest.fn(),
    save: jest.fn(),
    update: jest.fn(),
    softDelete: jest.fn(),
  };

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        HotelService,
        { provide: HotelRepository, useValue: mockRepo },
      ],
    }).compile();

    service = module.get<HotelService>(HotelService);
  });

  afterEach(() => jest.clearAllMocks());

  describe('findAll', () => {
    it('should return all hotels with destinations', async () => {
      const hotels = [{ id: '1', name: 'Grand Hotel' }];
      mockRepo.findAllWithDestination.mockResolvedValue(hotels);

      const result = await service.findAll();

      expect(result).toEqual(hotels);
      expect(mockRepo.findAllWithDestination).toHaveBeenCalledTimes(1);
    });

    it('should return empty array when no hotels exist', async () => {
      mockRepo.findAllWithDestination.mockResolvedValue([]);
      const result = await service.findAll();
      expect(result).toEqual([]);
    });
  });

  describe('findById', () => {
    it('should return the hotel when found', async () => {
      const hotel = { id: '1', name: 'Grand Hotel' };
      mockRepo.findByIdWithDestination.mockResolvedValue(hotel);

      const result = await service.findById('1');

      expect(result).toEqual(hotel);
      expect(mockRepo.findByIdWithDestination).toHaveBeenCalledWith('1');
    });

    it('should throw NotFoundException when hotel not found', async () => {
      mockRepo.findByIdWithDestination.mockResolvedValue(null);

      await expect(service.findById('999')).rejects.toThrow(NotFoundException);
    });
  });

  describe('create', () => {
    const createDto = {
      name: 'Grand Hotel',
      destination_id: 'dest-1',
      address: '123 Main St',
      contact_person: 'Jane',
    };

    it('should create and return success response', async () => {
      mockRepo.findByNameAndDestination.mockResolvedValue(null);
      mockRepo.save.mockResolvedValue({ id: 'new-1' });

      const result = await service.create(createDto as any);

      expect(result).toEqual({ id: 'new-1', message: 'Hotel created successfully' });
      expect(mockRepo.save).toHaveBeenCalledTimes(1);
    });

    it('should throw ConflictException when name+destination already exists', async () => {
      mockRepo.findByNameAndDestination.mockResolvedValue({ id: 'existing' });

      await expect(service.create(createDto as any)).rejects.toThrow(ConflictException);
      expect(mockRepo.save).not.toHaveBeenCalled();
    });
  });

  describe('update', () => {
    const existingHotel = { id: '1', name: 'Grand Hotel', destination_id: 'dest-1' };

    it('should update and return success response', async () => {
      mockRepo.findById.mockResolvedValue(existingHotel);
      mockRepo.update.mockResolvedValue(undefined);

      const result = await service.update('1', { address: 'New Address' } as any);

      expect(result).toEqual({ id: '1', message: 'Hotel updated successfully' });
    });

    it('should throw NotFoundException when hotel not found', async () => {
      mockRepo.findById.mockResolvedValue(null);

      await expect(service.update('999', {} as any)).rejects.toThrow(NotFoundException);
    });

    it('should throw ConflictException when name+destination conflicts with another hotel', async () => {
      mockRepo.findById.mockResolvedValue(existingHotel);
      mockRepo.findByNameAndDestination.mockResolvedValue({ id: 'other-id' });

      await expect(
        service.update('1', { name: 'New Name' } as any),
      ).rejects.toThrow(ConflictException);
    });
  });

  describe('remove', () => {
    it('should soft delete and return success message', async () => {
      mockRepo.findById.mockResolvedValue({ id: '1' });
      mockRepo.softDelete.mockResolvedValue(undefined);

      const result = await service.remove('1');

      expect(result).toEqual({ message: 'Hotel deleted successfully' });
      expect(mockRepo.softDelete).toHaveBeenCalledWith('1');
    });

    it('should throw NotFoundException when hotel not found', async () => {
      mockRepo.findById.mockResolvedValue(null);

      await expect(service.remove('999')).rejects.toThrow(NotFoundException);
    });
  });
});
