// Marketplace Service Tests

import { marketplaceService } from '@/core/iru/marketplace.service';
import prisma from '@/shared/database/prisma';

jest.mock('@/shared/database/prisma', () => ({
  iruOffering: {
    findMany: jest.fn(),
    findUnique: jest.fn(),
  },
  iruInquiry: {
    findFirst: jest.fn(),
    create: jest.fn(),
    findUnique: jest.fn(),
  },
}));

describe('MarketplaceService', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('getOfferings', () => {
    it('should return active offerings', async () => {
      const mockOfferings = [
        {
          id: '1',
          offeringId: 'IRU-OFF-001',
          name: 'Tier 1 IRU',
          capacityTier: 1,
          institutionalType: 'CentralBank',
          pricingModel: 'Fixed',
          basePrice: 100000,
          currency: 'USD',
          status: 'active',
          displayOrder: 0,
        },
      ];

      (prisma.iruOffering.findMany as jest.Mock).mockResolvedValue(mockOfferings);

      const offerings = await marketplaceService.getOfferings();

      expect(offerings).toHaveLength(1);
      expect(offerings[0].offeringId).toBe('IRU-OFF-001');
    });

    it('should filter by capacity tier', async () => {
      (prisma.iruOffering.findMany as jest.Mock).mockResolvedValue([]);

      await marketplaceService.getOfferings({ capacityTier: 1 });

      expect(prisma.iruOffering.findMany).toHaveBeenCalledWith(
        expect.objectContaining({
          where: expect.objectContaining({
            capacityTier: 1,
          }),
        })
      );
    });
  });

  describe('submitInquiry', () => {
    it('should create inquiry successfully', async () => {
      const mockOffering = {
        id: '1',
        offeringId: 'IRU-OFF-001',
        status: 'active',
      };

      (prisma.iruOffering.findUnique as jest.Mock).mockResolvedValue(mockOffering);
      (prisma.iruInquiry.findFirst as jest.Mock).mockResolvedValue(null);
      (prisma.iruInquiry.create as jest.Mock).mockResolvedValue({
        inquiryId: 'INQ-12345678',
        status: 'submitted',
      });

      const result = await marketplaceService.submitInquiry({
        offeringId: 'IRU-OFF-001',
        organizationName: 'Test Bank',
        institutionalType: 'CentralBank',
        jurisdiction: 'US',
        contactEmail: 'test@bank.com',
        contactName: 'Test User',
      });

      expect(result.inquiryId).toBe('INQ-12345678');
      expect(result.status).toBe('submitted');
    });

    it('should reject inquiry for inactive offering', async () => {
      const mockOffering = {
        id: '1',
        offeringId: 'IRU-OFF-001',
        status: 'inactive',
      };

      (prisma.iruOffering.findUnique as jest.Mock).mockResolvedValue(mockOffering);

      await expect(
        marketplaceService.submitInquiry({
          offeringId: 'IRU-OFF-001',
          organizationName: 'Test Bank',
          institutionalType: 'CentralBank',
          jurisdiction: 'US',
          contactEmail: 'test@bank.com',
          contactName: 'Test User',
        })
      ).rejects.toThrow('is not active');
    });
  });
});
