package main

import (
	"bufio"
	"bytes"
	"regexp"
	"strings"

	"github.com/asaskevich/govalidator"

	"gitlab.smoothnet.org/nhyatt/bind-response-policy-zone-creator/internal/log"
)

func parseSimple(data []byte) []string {
	var domains []string

	// convert data to reader for line-by-line reading
	r := bytes.NewReader(data)

	// process combined files line-by-line
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		line := scanner.Text()

		// skip lines where the first non-whitespace character is '#' or '//' or '|'
		if regexp.MustCompile(`^(\s+)?(#|\/\/|\|)`).MatchString(line) {
			continue
		}

		// skip lines with no characters and/or whitespace only
		if regexp.MustCompile(`^(\s+)?$`).MatchString(line) {
			continue
		}

		// split line by whitespace
		lineItems := strings.Fields(line)

		if len(lineItems) >= 1 {
			// the second item is the domain, check if its valid and add it
			if govalidator.IsDNSName(lineItems[0]) {
				domains = append(domains, lineItems[0])
			} else {
				log.Debug("host invalid", "host", lineItems[0])
			}
		}
	}

	return domains
}