import { describe, it, expect, vi, beforeEach } from 'vitest';
import { getMembership, getLeagueMembers, getPrimaryLeagueIdForDriver } from './leagueMembership';
import { LeagueMembershipService } from './services/leagues/LeagueMembershipService';

// Mock the LeagueMembershipService
vi.mock('./services/leagues/LeagueMembershipService', () => ({
  LeagueMembershipService: {
    getMembership: vi.fn(),
    getLeagueMembers: vi.fn(),
    getAllMembershipsForDriver: vi.fn(),
  },
}));

describe('leagueMembership', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe('getMembership', () => {
    it('should call LeagueMembershipService.getMembership with correct parameters', () => {
      const mockResult = { leagueId: 'league-123', driverId: 'driver-456', role: 'member' };
      vi.mocked(LeagueMembershipService.getMembership).mockResolvedValue(mockResult);

      const result = getMembership('league-123', 'driver-456');

      expect(LeagueMembershipService.getMembership).toHaveBeenCalledWith('league-123', 'driver-456');
      expect(result).toBeInstanceOf(Promise);
    });
  });

  describe('getLeagueMembers', () => {
    it('should call LeagueMembershipService.getLeagueMembers with correct parameter', () => {
      const mockResult = [
        { leagueId: 'league-123', driverId: 'driver-456', role: 'member' },
        { leagueId: 'league-123', driverId: 'driver-789', role: 'admin' },
      ];
      vi.mocked(LeagueMembershipService.getLeagueMembers).mockResolvedValue(mockResult);

      const result = getLeagueMembers('league-123');

      expect(LeagueMembershipService.getLeagueMembers).toHaveBeenCalledWith('league-123');
      expect(result).toBeInstanceOf(Promise);
    });
  });

  describe('getPrimaryLeagueIdForDriver', () => {
    it('should return null when driver has no memberships', () => {
      vi.mocked(LeagueMembershipService.getAllMembershipsForDriver).mockReturnValue([]);

      const result = getPrimaryLeagueIdForDriver('driver-456');

      expect(LeagueMembershipService.getAllMembershipsForDriver).toHaveBeenCalledWith('driver-456');
      expect(result).toBeNull();
    });

    it('should return the first league ID when driver has memberships', () => {
      const mockMemberships = [
        { leagueId: 'league-123', driverId: 'driver-456', role: 'member' },
        { leagueId: 'league-456', driverId: 'driver-456', role: 'admin' },
      ];
      vi.mocked(LeagueMembershipService.getAllMembershipsForDriver).mockReturnValue(mockMemberships);

      const result = getPrimaryLeagueIdForDriver('driver-456');

      expect(LeagueMembershipService.getAllMembershipsForDriver).toHaveBeenCalledWith('driver-456');
      expect(result).toBe('league-123');
    });

    it('should return the first league ID regardless of role', () => {
      const mockMemberships = [
        { leagueId: 'league-789', driverId: 'driver-456', role: 'member' },
        { leagueId: 'league-123', driverId: 'driver-456', role: 'owner' },
      ];
      vi.mocked(LeagueMembershipService.getAllMembershipsForDriver).mockReturnValue(mockMemberships);

      const result = getPrimaryLeagueIdForDriver('driver-456');

      expect(result).toBe('league-789');
    });
  });
});