jest.mock('src/config/api', () => ({
  __esModule: true,
  default: { get: jest.fn() },
}));

import api from 'src/config/api';

import { fetchCollaboratorOfferContractPreview } from './microloanService';

const mockGet = (api as unknown as { get: jest.Mock }).get;
const URL = '/microloans/collaborator/offers/offer-1/contract-preview';

describe('fetchCollaboratorOfferContractPreview', () => {
  beforeEach(() => {
    mockGet.mockReset();
    mockGet.mockResolvedValue({ data: { markdown: 'contract-md' } });
  });

  it('sends only amount and installments when no bank data is provided', async () => {
    await fetchCollaboratorOfferContractPreview('offer-1', {
      amount: 1000,
      installments: 12,
    });

    expect(mockGet).toHaveBeenCalledWith(URL, {
      params: { amount: 1000, installments: 12 },
    });
  });

  it('includes bankAccountNumber and bankName when provided', async () => {
    await fetchCollaboratorOfferContractPreview('offer-1', {
      amount: 1000,
      installments: 12,
      bankAccountNumber: '002010077777777771',
      bankName: 'BANAMEX',
    });

    expect(mockGet).toHaveBeenCalledWith(URL, {
      params: {
        amount: 1000,
        installments: 12,
        bankAccountNumber: '002010077777777771',
        bankName: 'BANAMEX',
      },
    });
  });

  it('omits empty bank fields', async () => {
    await fetchCollaboratorOfferContractPreview('offer-1', {
      amount: 1000,
      installments: 12,
      bankAccountNumber: '',
      bankName: '',
    });

    expect(mockGet).toHaveBeenCalledWith(URL, {
      params: { amount: 1000, installments: 12 },
    });
  });

  it('returns the markdown from the response', async () => {
    const result = await fetchCollaboratorOfferContractPreview('offer-1', {
      amount: 1000,
      installments: 12,
    });

    expect(result).toBe('contract-md');
  });
});
