package main

import (
	"log"
	"regexp"
	"sort"
)

func cleanBadDomains(domains []string) []string {
	// remove duplicates
	total := len(domains)
	all := make(map[string]bool)
	list := []string{}
	for _, item := range domains {
		if _, value := all[item]; !value {
			all[item] = true
			list = append(list, item)
		}
	}
	domains = list
	log.Printf("[INFO] Duplicate items removed: %d\n", total-len(domains))

	// remove hosts that are too long
	total = len(domains)
	list = []string{}
	for _, blocklistItem := range domains {
		if len([]rune(blocklistItem)) > 255 {
			continue
		}
		list = append(list, blocklistItem)
	}
	domains = list
	log.Printf("[INFO] Hosts with too many characters removed: %d\n", total-len(domains))

	// remove allow-listed matches
	total = len(domains)
	list = []string{}
	for _, blocklistItem := range domains {
		var match bool

		for _, allowlistItem := range config.Config.AllowLists {
			r, err := regexp.Compile(allowlistItem)
			if err != nil {
				log.Printf("[ERROR] Allow list item (%s) is not valid regex: %v\n", allowlistItem, err)
				break
			}
			if r.MatchString(blocklistItem) {
				match = true
				break
			}
		}
		if !match {
			list = append(list, blocklistItem)
		}
	}
	domains = list
	log.Printf("[INFO] Allowed hosts removed: %d\n", total-len(domains))

	log.Printf("[INFO] Total domains in list at end: %d.\n", len(domains))
	sort.Strings(domains)
	return domains
}
