const wc = require('../../src/data/worldcup2026');

describe('worldcup2026 static reference data', () => {
  describe('VENUES', () => {
    test('has 16 venues (the official 2026 host venue count)', () => {
      const count = Object.keys(wc.VENUES).length;
      expect(count).toBe(16);
    });

    test('every venue has altitude_ft, climate, country, city', () => {
      for (const [name, data] of Object.entries(wc.VENUES)) {
        expect(typeof data.altitude_ft).toBe('number');
        expect(typeof data.climate).toBe('string');
        expect(typeof data.country).toBe('string');
        expect(typeof data.city).toBe('string');
        expect(['USA', 'Canada', 'Mexico']).toContain(data.country);
        // Sanity check — no venue at impossible altitude.
        expect(data.altitude_ft).toBeGreaterThanOrEqual(0);
        expect(data.altitude_ft).toBeLessThan(10000);
        // Test the data is shaped right, not the actual venue altitudes.
        if (!name) throw new Error('empty venue name'); // touch `name`
      }
    });

    test('Mexico City venue is the highest-altitude host site', () => {
      const altitudes = Object.values(wc.VENUES).map((v) => v.altitude_ft);
      const max = Math.max(...altitudes);
      expect(wc.VENUES['Estadio Azteca'].altitude_ft).toBe(max);
      expect(max).toBeGreaterThan(7000); // ~7,349 ft per public elevation data
    });
  });

  describe('altitudeImpact', () => {
    test('high above 4,000 ft', () => {
      expect(wc.altitudeImpact(7349)).toBe('high');
      expect(wc.altitudeImpact(5138)).toBe('high');
      expect(wc.altitudeImpact(4001)).toBe('high');
    });
    test('moderate between 1,500 and 4,000 ft', () => {
      expect(wc.altitudeImpact(1765)).toBe('moderate');
      expect(wc.altitudeImpact(2000)).toBe('moderate');
    });
    test('none at sea level / typical US altitudes', () => {
      expect(wc.altitudeImpact(7)).toBe('none');
      expect(wc.altitudeImpact(820)).toBe('none');
      expect(wc.altitudeImpact(1499)).toBe('none');
    });
    test('returns none for nullable inputs (graceful)', () => {
      expect(wc.altitudeImpact(null)).toBe('none');
      expect(wc.altitudeImpact(undefined)).toBe('none');
      expect(wc.altitudeImpact(NaN)).toBe('none');
    });
  });

  describe('isHomeContinent', () => {
    test('returns true for the three 2026 hosts', () => {
      expect(wc.isHomeContinent('USA')).toBe(true);
      expect(wc.isHomeContinent('Canada')).toBe(true);
      expect(wc.isHomeContinent('Mexico')).toBe(true);
    });
    test('returns false for European squads', () => {
      expect(wc.isHomeContinent('France')).toBe(false);
      expect(wc.isHomeContinent('Brazil')).toBe(false);
    });
    test('returns false for unknown teams', () => {
      expect(wc.isHomeContinent('Atlantis')).toBe(false);
      expect(wc.isHomeContinent(null)).toBe(false);
    });
  });

  describe('penalty / corner / free-kick role lookups', () => {
    test('isPenaltyTaker — known taker', () => {
      expect(wc.isPenaltyTaker('Lionel Messi', 'Argentina')).toBe(true);
      expect(wc.isPenaltyTaker('Harry Kane', 'England')).toBe(true);
    });
    test('isPenaltyTaker — case-insensitive', () => {
      expect(wc.isPenaltyTaker('lionel messi', 'Argentina')).toBe(true);
    });
    test('isPenaltyTaker — wrong team returns false', () => {
      expect(wc.isPenaltyTaker('Lionel Messi', 'France')).toBe(false);
    });
    test('isPenaltyTaker — null inputs return false', () => {
      expect(wc.isPenaltyTaker(null, 'Argentina')).toBe(false);
      expect(wc.isPenaltyTaker('X', null)).toBe(false);
    });
    test('isCornerTaker — multi-name array picks up secondaries', () => {
      // England has 3+ corner takers — verify the second is found too.
      expect(wc.isCornerTaker('Phil Foden', 'England')).toBe(true);
      expect(wc.isCornerTaker('Trent Alexander-Arnold', 'England')).toBe(true);
    });
    test('isFreeKickTaker — sparse map (not every team has one)', () => {
      expect(wc.isFreeKickTaker('Lionel Messi', 'Argentina')).toBe(true);
      // Australia not in FK takers map at all
      expect(wc.isFreeKickTaker('Anyone', 'Australia')).toBe(false);
    });
  });

  describe('getTournamentHistory', () => {
    test('returns career WC stats for documented players', () => {
      const messi = wc.getTournamentHistory('Lionel Messi');
      expect(messi.wc_goals_career).toBeGreaterThanOrEqual(3);
      expect(messi.wc_appearances).toBeGreaterThan(0);
    });
    test('returns null for unknown player', () => {
      expect(wc.getTournamentHistory('Nobody Joe')).toBeNull();
      expect(wc.getTournamentHistory(null)).toBeNull();
    });
  });

  describe('immutability', () => {
    test('VENUES is frozen (cannot mutate top-level)', () => {
      expect(Object.isFrozen(wc.VENUES)).toBe(true);
    });
    test('CONCACAF_TEAMS is frozen', () => {
      expect(Object.isFrozen(wc.CONCACAF_TEAMS)).toBe(true);
    });
  });
});
