with RECURSIVE connected_players as (
	select name1 as player_name,
		name1 as group_root,
		matching_count,
		total_achievements1 as achievements,
		similarity_percentage
	from similar_pairs
	where similarity_percentage >= 70
	union
	select name2,
		name1,
		matching_count,
		total_achievements2,
		similarity_percentage
	from similar_pairs 
		where similarity_percentage >= 70
	union
	select case
			when sp.name1 = cp.player_name then sp.name2
			else sp.name1
		end,
		cp.group_root,
		sp.matching_count,
		case
			when sp.name1 = cp.player_name then sp.total_achievements2
			else sp.total_achievements1
		end,
		sp.similarity_percentage
	from connected_players cp
		join similar_pairs sp on (
			sp.name1 = cp.player_name
			or sp.name2 = cp.player_name
		)
		and sp.similarity_percentage >= 70
	where case
			when sp.name1 = cp.player_name then sp.name2
			else sp.name2
		end != cp.group_root
)
select group_root,
	count(*) as group_size,
	group_concat(distinct player_name) as connected_players,
	MIN(similarity_percentage) as min_similarity,
	AVG(similarity_percentage) as avg_similarity,
	MIN(matching_count) as min_matching,
	AVG(matching_count) as avg_matching
from connected_players
where group_root in ('Paskoo')
group by group_root
having count(*) > 1
order by count(*) desc,
	avg_similarity desc