import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { AssessmentService } from './assessment.service';
import { AssessmentSession } from './entities/assessment-session.entity';
import { AssessmentQuestion } from './entities/assessment-question.entity';
import { AssessmentAnswer } from './entities/assessment-answer.entity';
import { KnowledgeBaseService } from '../knowledge-base/knowledge-base.service';
import { KnowledgeGroupService } from '../knowledge-group/knowledge-group.service';
import { ModelConfigService } from '../model-config/model-config.service';
import { ConfigService } from '@nestjs/config';
import { TemplateService } from './services/template.service';
import { ContentFilterService } from './services/content-filter.service';
import { RagService } from '../rag/rag.service';
import { ChatService } from '../chat/chat.service';
import { I18nService } from '../i18n/i18n.service';
import { TenantService } from '../tenant/tenant.service';
import { NotFoundException } from '@nestjs/common';

describe('AssessmentService', () => {
  let service: AssessmentService;
  let sessionRepository: any;

  const mockRepository = () => ({
    delete: jest.fn(),
    find: jest.fn(),
    findOne: jest.fn(),
    save: jest.fn(),
  });

  const mockService = () => ({});

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        AssessmentService,
        { provide: getRepositoryToken(AssessmentSession), useFactory: mockRepository },
        { provide: getRepositoryToken(AssessmentQuestion), useFactory: mockRepository },
        { provide: getRepositoryToken(AssessmentAnswer), useFactory: mockRepository },
        { provide: KnowledgeBaseService, useFactory: mockService },
        { provide: KnowledgeGroupService, useFactory: mockService },
        { provide: ModelConfigService, useFactory: mockService },
        { provide: ConfigService, useFactory: mockService },
        { provide: TemplateService, useFactory: mockService },
        { provide: ContentFilterService, useFactory: mockService },
        { provide: RagService, useFactory: mockService },
        { provide: ChatService, useFactory: mockService },
        { provide: I18nService, useFactory: mockService },
        { provide: TenantService, useFactory: mockService },
      ],
    }).compile();

    service = module.get<AssessmentService>(AssessmentService);
    sessionRepository = module.get(getRepositoryToken(AssessmentSession));
  });

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

  describe('deleteSession', () => {
    it('should delete a session if it exists and belongs to the user', async () => {
      sessionRepository.delete.mockResolvedValue({ affected: 1 });
      await expect(service.deleteSession('session-id', 'user-id')).resolves.not.toThrow();
      expect(sessionRepository.delete).toHaveBeenCalledWith({ id: 'session-id', userId: 'user-id' });
    });

    it('should throw NotFoundException if no session was affected', async () => {
      sessionRepository.delete.mockResolvedValue({ affected: 0 });
      await expect(service.deleteSession('non-existent', 'user-id')).rejects.toThrow(NotFoundException);
    });
  });
});
