package config

import (
	"errors"
	"fmt"
	"regexp"
	"strings"
)

var validUserNameRe = regexp.MustCompile("^[a-z_][a-z0-9_-]{0,31}$")

func (c Config) validate() error {
	var errs []error

	if c.Meta.Host == "" {
		errs = append(errs, errors.New("meta.host is required"))
	}

	if strings.HasPrefix(c.Meta.Host, "http") {
		errs = append(errs, errors.New("meta.host shouldn't include protocol"))
	}

	if !isDirExists(c.Repo.Dir) {
		errs = append(errs, fmt.Errorf("repo.dir seems to be an invalid path"))
	}

	if err := checkPort(c.Server.Port); err != nil {
		errs = append(errs, fmt.Errorf("server.port %w", err))
	}

	if c.SSH.Enable {
		if !validUserNameRe.MatchString(c.SSH.User) {
			errs = append(errs, fmt.Errorf("ssh.user must be correct linux user name(^[a-z_][a-z0-9_-]{0,31}$)"))
		}
	}

	return errors.Join(errs...)
}

func checkPort(port int) error {
	if port < 1 || port > 65535 {
		return fmt.Errorf("must be between 1 and 65535, got %d", port)
	}
	return nil
}
