import { logger } from '@goodgo/logger';

import { featureRepository } from '../feature.repository';
import { FeatureService } from '../feature.service';

// EN: Mock the logger to avoid console output during tests
// VI: Mock logger để tránh output console trong tests
jest.mock('@goodgo/logger');

// EN: Mock feature repository
// VI: Mock feature repository
jest.mock('../feature.repository', () => ({
  featureRepository: {
    create: jest.fn(),
    findById: jest.fn(),
    update: jest.fn(),
    delete: jest.fn(),
    findAll: jest.fn(),
  },
}));

describe('FeatureService', () => {
  let featureService: FeatureService;

  beforeEach(() => {
    // EN: Clear all mocks before each test
    // VI: Xóa tất cả mocks trước mỗi test
    jest.clearAllMocks();
    featureService = new FeatureService();
  });

  describe('create', () => {
    it('should create a feature successfully', async () => {
      // EN: Arrange
      // VI: Chuẩn bị
      const testData = { name: 'test-feature', title: 'Test Feature', description: 'A test feature' };
      const mockFeature = {
        id: 'test-id',
        name: testData.name,
        title: testData.title,
        description: testData.description,
        config: {},
        enabled: true,
        version: '1.0.0',
        tags: [],
        createdAt: new Date(),
        updatedAt: new Date(),
      };
      (featureRepository.create as jest.Mock).mockResolvedValue(mockFeature);

      // EN: Act
      // VI: Thực hiện
      const result = await featureService.create(testData);

      // EN: Assert
      // VI: Kiểm tra
      expect(featureRepository.create).toHaveBeenCalledWith(testData);
      expect(logger.info).toHaveBeenCalledWith('Creating feature / Tạo feature', { data: testData });
      expect(logger.info).toHaveBeenCalledWith('Feature created successfully / Feature đã được tạo thành công', { featureId: mockFeature.id });
      expect(result).toEqual(mockFeature);
    });

    it('should handle minimal data', async () => {
      // EN: Arrange
      // VI: Chuẩn bị
      const minimalData = { name: 'minimal-feature' };
      const mockFeature = {
        id: 'minimal-id',
        name: minimalData.name,
        config: {},
        enabled: true,
        version: '1.0.0',
        tags: [],
        createdAt: new Date(),
        updatedAt: new Date(),
      };
      (featureRepository.create as jest.Mock).mockResolvedValue(mockFeature);

      // EN: Act
      // VI: Thực hiện
      const result = await featureService.create(minimalData);

      // EN: Assert
      // VI: Kiểm tra
      expect(logger.info).toHaveBeenCalledWith('Creating feature / Tạo feature', { data: minimalData });
      expect(result).toEqual(mockFeature);
    });

    it('should handle complex data structures', async () => {
      // EN: Arrange
      // VI: Chuẩn bị
      const complexData = {
        name: 'advanced-feature',
        title: 'Advanced Feature',
        description: 'Feature with complex data',
        config: { enabled: true, priority: 1 },
        tags: ['advanced', 'complex']
      };
      const mockFeature = {
        id: 'complex-id',
        name: complexData.name,
        title: complexData.title,
        description: complexData.description,
        config: complexData.config,
        enabled: true,
        version: '1.0.0',
        tags: complexData.tags,
        createdAt: new Date(),
        updatedAt: new Date(),
      };
      (featureRepository.create as jest.Mock).mockResolvedValue(mockFeature);

      // EN: Act
      // VI: Thực hiện
      const result = await featureService.create(complexData);

      // EN: Assert
      // VI: Kiểm tra
      expect(logger.info).toHaveBeenCalledWith('Creating feature / Tạo feature', { data: complexData });
      expect(result).toEqual(mockFeature);
    });
  });
});