// Session 51 — GET /api/team/:abbr (Team Hub endpoint).

const request = require('supertest');

jest.mock('../../src/services/teamService', () => ({
  getTeamHub: jest.fn(),
}));
const teamService = require('../../src/services/teamService');
const app = require('../../src/app');

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

describe('GET /api/team/:abbr', () => {
  it('returns the hub for a known team', async () => {
    teamService.getTeamHub.mockResolvedValue({ team: { name: 'New York Yankees', abbr: 'NYY', sport: 'mlb' }, roster: [{ player: 'Aaron Judge' }] });
    const res = await request(app).get('/api/team/NYY?sport=mlb');
    expect(res.status).toBe(200);
    expect(res.body.team.name).toBe('New York Yankees');
    expect(teamService.getTeamHub).toHaveBeenCalledWith('mlb', 'NYY');
  });

  it('404s an unknown team with a helpful message', async () => {
    teamService.getTeamHub.mockResolvedValue(null);
    const res = await request(app).get('/api/team/ZZZ?sport=mlb');
    expect(res.status).toBe(404);
    expect(res.body.error).toMatch(/abbreviation/i);
  });

  it('defaults sport to mlb', async () => {
    teamService.getTeamHub.mockResolvedValue({ team: { abbr: 'BOS' }, roster: [] });
    await request(app).get('/api/team/BOS');
    expect(teamService.getTeamHub).toHaveBeenCalledWith('mlb', 'BOS');
  });
});
