import { Test, TestingModule } from '@nestjs/testing';
import { AppService } from './app.service';
import { ConfigService } from './config.service';
import { DatasetsService } from './datasets.service';
import { DbService } from './db.service';
import { HandbrakeService } from './handbrake.service';
import { MaintenanceService } from './maintenance.service';
import { TaskQueueService } from './task-queue.service';
import { WatcherService } from './watcher.service';

// Mock chokidar
jest.mock('chokidar', () => ({
  watch: jest.fn(),
}));

describe('AppService', () => {
  let service: AppService;
  let configService: jest.Mocked<ConfigService>;

  beforeEach(async () => {
    const mockConfigService = {
      getSettings: jest.fn(),
      setSettings: jest.fn(),
      deleteSetting: jest.fn(),
    };

    const mockDbService = {};
    const mockWatcherService = {};
    const mockMaintenanceService = {};
    const mockHandbrakeService = {};
    const mockDatasetsService = {};
    const mockTaskQueueService = {};

    const module: TestingModule = await Test.createTestingModule({
      providers: [
        AppService,
        {
          provide: DbService,
          useValue: mockDbService,
        },
        {
          provide: WatcherService,
          useValue: mockWatcherService,
        },
        {
          provide: ConfigService,
          useValue: mockConfigService,
        },
        {
          provide: MaintenanceService,
          useValue: mockMaintenanceService,
        },
        {
          provide: HandbrakeService,
          useValue: mockHandbrakeService,
        },
        {
          provide: DatasetsService,
          useValue: mockDatasetsService,
        },
        {
          provide: TaskQueueService,
          useValue: mockTaskQueueService,
        },
      ],
    }).compile();

    service = module.get<AppService>(AppService);
    configService = module.get(ConfigService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  describe('getSettings', () => {
    it('should call configService.getSettings with key and defaultValue', () => {
      const mockResult = 'testValue';
      configService.getSettings.mockReturnValue(mockResult);

      const result = service.getSettings('testKey', 'default');

      expect(configService.getSettings).toHaveBeenCalledWith(
        'testKey',
        'default',
      );
      expect(result).toEqual(mockResult);
    });
  });

  describe('setSettings', () => {
    it('should call configService.setSettings', () => {
      const settings = { key: 'value' };
      const mockResult = true;
      configService.setSettings.mockReturnValue(mockResult);

      const result = service.setSettings(settings);

      expect(configService.setSettings).toHaveBeenCalledWith(settings);
      expect(result).toEqual(mockResult);
    });
  });

  describe('deleteSetting', () => {
    it('should call configService.deleteSetting', () => {
      const mockResult = true;
      configService.deleteSetting.mockReturnValue(mockResult);

      const result = service.deleteSetting('testKey');

      expect(configService.deleteSetting).toHaveBeenCalledWith('testKey');
      expect(result).toEqual(mockResult);
    });
  });
});
