import { ConflictException } from '@nestjs/common';
import { SupplierService } from './supplier.service';

/**
 * findOrCreateByName is the Movement Dashboard type-and-create path.
 * Tenant scope stays inside the repository (CLS). The caller cannot
 * pass a tenant id — these tests lock that boundary.
 */
describe('SupplierService.findOrCreateByName', () => {
  const repo = {
    findByNameAndDestination: jest.fn(),
    save: jest.fn(),
    update: jest.fn(),
  };
  const service = new SupplierService(repo as never, {} as never);

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

  it('creates a transport supplier when the name is new for that destination', async () => {
    repo.findByNameAndDestination.mockResolvedValue(null);
    repo.save.mockImplementation(async (row: { name: string }) => ({ id: 's1', ...row }));

    const res = await service.findOrCreateByName({
      name: '  Acme Cabs ',
      destination_id: 'dest-1',
      supplier_types: ['transport'],
    });

    expect(repo.findByNameAndDestination).toHaveBeenCalledWith('Acme Cabs', 'dest-1');
    expect(repo.findByNameAndDestination.mock.calls[0]).toHaveLength(2);
    expect(repo.save).toHaveBeenCalledWith(
      expect.objectContaining({
        name: 'Acme Cabs',
        destination_id: 'dest-1',
        supplier_types: ['transport'],
        contact_person: 'N/A',
        phone: '',
      }),
    );
    expect(repo.save.mock.calls[0][0]).not.toHaveProperty('tenant_id');
    expect(res).toEqual({
      id: 's1',
      name: 'Acme Cabs',
      created: true,
      supplier_types: ['transport'],
    });
  });

  it('adds the missing activity type onto an existing transport supplier', async () => {
    repo.findByNameAndDestination.mockResolvedValue({
      id: 's1',
      name: 'Acme',
      supplier_types: ['transport'],
    });

    const res = await service.findOrCreateByName({
      name: 'Acme',
      destination_id: 'dest-1',
      supplier_types: ['activity'],
    });

    expect(repo.save).not.toHaveBeenCalled();
    expect(repo.update).toHaveBeenCalledWith('s1', {
      supplier_types: ['transport', 'activity'],
    });
    expect(res).toEqual({
      id: 's1',
      name: 'Acme',
      created: false,
      supplier_types: ['transport', 'activity'],
    });
  });

  it('does not write when the supplier already has the requested type', async () => {
    repo.findByNameAndDestination.mockResolvedValue({
      id: 's1',
      name: 'Acme',
      supplier_types: ['transport', 'activity'],
    });

    const res = await service.findOrCreateByName({
      name: 'Acme',
      destination_id: 'dest-1',
      supplier_types: ['activity'],
    });

    expect(repo.update).not.toHaveBeenCalled();
    expect(repo.save).not.toHaveBeenCalled();
    expect(res.created).toBe(false);
    expect(res.supplier_types).toEqual(['transport', 'activity']);
  });

  it('rejects an empty name before any repository call', async () => {
    await expect(
      service.findOrCreateByName({ name: '   ', destination_id: 'dest-1' }),
    ).rejects.toBeInstanceOf(ConflictException);
    expect(repo.findByNameAndDestination).not.toHaveBeenCalled();
  });
});
