import { Test, TestingModule } from '@nestjs/testing';
import { <%= classify(name) %>Service } from './<%= dasherize(name) %>.service';
import { TenantContextService, Tenant } from '@lexmata/nestjs-multi-tenant';

describe('<%= classify(name) %>Service', () => {
  let service: <%= classify(name) %>Service;

  const mockTenant: Tenant = {
    id: 'test-tenant-id',
    name: 'Test Tenant',
  };

  const mockTenantContextService = {
    getTenant: jest.fn().mockReturnValue(mockTenant),
    getTenantId: jest.fn().mockReturnValue(mockTenant.id),
    hasTenant: jest.fn().mockReturnValue(true),
  };

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        <%= classify(name) %>Service,
        { provide: TenantContextService, useValue: mockTenantContextService },
      ],
    }).compile();

    service = module.get<<%= classify(name) %>Service>(<%= classify(name) %>Service);
  });

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

  describe('findAll', () => {
    it('should return all <%= name %> for the tenant', async () => {
      const result = await service.findAll(mockTenant);
      expect(result).toBeDefined();
      expect(result.tenantId).toBe(mockTenant.id);
    });
  });

  describe('findOne', () => {
    it('should return a single <%= name %>', async () => {
      const result = await service.findOne('1', mockTenant.id);
      expect(result).toBeDefined();
      expect(result.id).toBe('1');
      expect(result.tenantId).toBe(mockTenant.id);
    });
  });

  describe('create', () => {
    it('should create a new <%= name %>', async () => {
      const data = { name: 'Test' };
      const result = await service.create(data, mockTenant);
      expect(result).toBeDefined();
      expect(result.tenantId).toBe(mockTenant.id);
    });
  });

  describe('update', () => {
    it('should update a <%= name %>', async () => {
      const data = { name: 'Updated' };
      const result = await service.update('1', data, mockTenant.id);
      expect(result).toBeDefined();
      expect(result.id).toBe('1');
      expect(result.tenantId).toBe(mockTenant.id);
    });
  });

  describe('remove', () => {
    it('should remove a <%= name %>', async () => {
      const result = await service.remove('1', mockTenant.id);
      expect(result).toBeDefined();
      expect(result.id).toBe('1');
      expect(result.deleted).toBe(true);
    });
  });

  describe('getCurrentTenant', () => {
    it('should return the current tenant from context', () => {
      const result = service.getCurrentTenant();
      expect(result).toEqual(mockTenant);
      expect(mockTenantContextService.getTenant).toHaveBeenCalled();
    });
  });

  describe('getCurrentTenantId', () => {
    it('should return the current tenant ID from context', () => {
      const result = service.getCurrentTenantId();
      expect(result).toBe(mockTenant.id);
      expect(mockTenantContextService.getTenantId).toHaveBeenCalled();
    });
  });
});


