import { Test, TestingModule } from '@nestjs/testing';
import { ConflictException, NotFoundException } from '@nestjs/common';
import { ActivityService } from './activity.service';
import { ActivityRepository } from './repositories/activity.repository';

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

  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: [
        ActivityService,
        { provide: ActivityRepository, useValue: mockRepo },
      ],
    }).compile();

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

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

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

      const result = await service.findAll();

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

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

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

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

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

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

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

  describe('create', () => {
    const createDto = {
      name: 'Safari',
      destination_id: 'dest-1',
      address: '123 Main St',
      time_slot: 'morning',
      contact_person: 'John',
    };

    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: 'Activity 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 existingActivity = { id: '1', name: 'Safari', destination_id: 'dest-1' };

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

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

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

    it('should throw NotFoundException when activity 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 activity', async () => {
      mockRepo.findById.mockResolvedValue(existingActivity);
      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: 'Activity deleted successfully' });
      expect(mockRepo.softDelete).toHaveBeenCalledWith('1');
    });

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

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