import { RequirementService } from './requirement.service';

describe('RequirementService with V2.2 composite requirement key', () => {
  const makeService = () => {
    const prisma = {
      requirement: {
        create: jest.fn(),
        delete: jest.fn(),
        findFirst: jest.fn(),
        findMany: jest.fn(),
        update: jest.fn(),
      },
    };

    return {
      prisma,
      service: new RequirementService(prisma as any),
    };
  };

  it('creates requirements with a partition-key-scoped business code', async () => {
    const { prisma, service } = makeService();
    prisma.requirement.create.mockResolvedValue({ id: 'req-1', productId: 'product-1', code: 'REQ-001' });

    await service.create('product-1', {
      code: 'REQ-001',
      title: 'Payment',
      creatorId: 'user-1',
    });

    expect(prisma.requirement.create).toHaveBeenCalledWith({
      data: expect.objectContaining({
        productId: 'product-1',
        code: 'REQ-001',
        title: 'Payment',
      }),
    });
  });

  it('updates requirements by id plus product id', async () => {
    const { prisma, service } = makeService();
    prisma.requirement.findFirst.mockResolvedValue({
      id: 'req-1',
      productId: 'product-1',
      status: 'draft',
    });
    prisma.requirement.update.mockResolvedValue({ id: 'req-1', productId: 'product-1' });

    await service.update('product-1', 'req-1', { title: 'Payment v2' });

    expect(prisma.requirement.update).toHaveBeenCalledWith({
      where: { id_productId: { id: 'req-1', productId: 'product-1' } },
      data: { title: 'Payment v2' },
    });
  });

  it('deletes requirements by id plus product id', async () => {
    const { prisma, service } = makeService();
    prisma.requirement.findFirst.mockResolvedValue({
      id: 'req-1',
      productId: 'product-1',
      status: 'draft',
    });
    prisma.requirement.delete.mockResolvedValue({ id: 'req-1', productId: 'product-1' });

    await service.remove('product-1', 'req-1');

    expect(prisma.requirement.delete).toHaveBeenCalledWith({
      where: { id_productId: { id: 'req-1', productId: 'product-1' } },
    });
  });
});
