import numpy as np
import numba as nb
from itertools import permutations, combinations
from fastcache import lru_cache
from tqdm import tqdm
from tprint import tprint

def round_robin_tournament(rankings: list[list[int]]) -> list[list[int]]:
    nb_candidates = rankings.shape[1]
    points = np.zeros((nb_candidates, nb_candidates), dtype=int)
    for rkng in rankings:
        arg_rkng = np.argsort(rkng)
        print(rkng)
        for a, b in combinations(range(len(rkng)), 2):
            points[a][b] += arg_rkng[a] < arg_rkng[b]
            points[b][a] += arg_rkng[a] > arg_rkng[b]
    return points


def minimax_condorcet(rankings: list[list[int]]) -> list[list[int]]:
    tournament = round_robin_tournament(rankings)
    #TODO


if __name__ == '__main__':
    ranks = np.array([[0, 1, 2, 3, 4],
                      [0, 1, 3, 2, 4],
                      [4, 1, 2, 0, 3],
                      [4, 1, 0, 2, 3],
                      [4, 1, 3, 2, 0]])

    ranks = np.array([[1, 3, 2],
                      [1, 2, 3],
                      [2, 1, 3]])

    print(round_robin_tournament(ranks))

    # print(borda([[1, 2, 3],
    #              [3, 1, 2]]))

    # def take(idx: int, iterator: iter) -> iter:
    #     for _ in range(idx):
    #         yield next(iterator)
    # ranks = np.array(list(take(20000, permutations(range(20)))))
    # for _ in tqdm(range(500)):
    #     selected_lines = np.random.randint(ranks.shape[0], size=20000)
    #     selected = ranks[selected_lines,:]
    #     if (borda(selected) != simple_borda(selected)).any():
    #         print(*borda(selected))
    #         print(*simple_borda(selected))
    #     # tprint(selected)
    #     # print(ranks)
    #     # print(kendalltau_dist(ranks[5], ranks[-1]))
    #     # print(np_kendalltau_dist(ranks[5], ranks[-1]))



