process.env.CFBD_KEY = 'test-key';
process.env.CFBD_BASE_URL = 'https://api.cfbd.test';

const mockAxiosGet = jest.fn();
jest.mock('axios', () => ({ get: (...args) => mockAxiosGet(...args) }));

const mockCache = { current: new Map() };
jest.mock('../../src/utils/redis', () => ({
  cacheGet: async (k) => mockCache.current.get(k) ?? null,
  cacheSet: async (k, v) => { mockCache.current.set(k, v); return true; },
  cacheDel: async (k) => { mockCache.current.delete(k); return true; },
}));

const adapter = require('../../src/services/adapters/cfbdAdapter');

beforeEach(() => {
  mockAxiosGet.mockReset();
  mockCache.current.clear();
});

describe('cfbdAdapter.configured', () => {
  test('reflects CFBD_KEY presence', () => {
    expect(adapter.configured()).toBe(true);
    delete process.env.CFBD_KEY;
    expect(adapter.configured()).toBe(false);
    process.env.CFBD_KEY = 'test-key';
  });
});

describe('cfbdAdapter endpoints', () => {
  test('getTeamStats returns array from CFBD payload', async () => {
    mockAxiosGet.mockResolvedValue({
      status: 200,
      data: [{ team: 'Michigan', statName: 'passingYards', statValue: 3500 }],
    });
    const stats = await adapter.getTeamStats('Michigan', 2025);
    expect(stats).toHaveLength(1);
  });

  test('getTalentComposite picks the right team from full-year payload', async () => {
    mockAxiosGet.mockResolvedValue({
      status: 200,
      data: [
        { school: 'Alabama', talent: 980 },
        { school: 'Michigan', talent: 870 },
        { school: 'Ohio State', talent: 940 },
      ],
    });
    const t = await adapter.getTalentComposite('Michigan', 2025);
    expect(t).toMatchObject({ school: 'Michigan', talent: 870 });
  });

  test('uses Bearer token header (key never embedded in query)', async () => {
    mockAxiosGet.mockResolvedValue({ status: 200, data: [] });
    await adapter.getTeamStats('Michigan', 2025);
    expect(mockAxiosGet).toHaveBeenCalledWith(
      expect.any(String),
      expect.objectContaining({
        headers: expect.objectContaining({ Authorization: 'Bearer test-key' }),
      }),
    );
  });

  test('returns [] / null when unconfigured', async () => {
    delete process.env.CFBD_KEY;
    expect(await adapter.getTeamStats('Michigan', 2025)).toEqual([]);
    expect(await adapter.getTalentComposite('Michigan', 2025)).toBeNull();
    process.env.CFBD_KEY = 'test-key';
  });
});
