// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
	"fmt"
	"strings"
	"unicode"
)

func validateTrailer(trailer string) error {
	if trailer == "" {
		return fmt.Errorf("trailer cannot be empty")
	}

	lines := strings.Split(trailer, "\n")
	firstLine := lines[0]

	if len(firstLine) > 0 && unicode.IsSpace(rune(firstLine[0])) {
		return fmt.Errorf("trailer key cannot start with whitespace: %q", trailer)
	}

	colonIdx := strings.Index(firstLine, ":")
	if colonIdx == -1 {
		return fmt.Errorf("trailer must contain ':' separator: %q", trailer)
	}

	key := firstLine[:colonIdx]

	if strings.TrimSpace(key) != key {
		return fmt.Errorf("trailer key cannot have leading or trailing whitespace: %q", key)
	}

	if strings.ContainsAny(key, " \t") {
		return fmt.Errorf("trailer key cannot contain whitespace: %q", key)
	}

	if key == "" {
		return fmt.Errorf("trailer key cannot be empty: %q", trailer)
	}

	for i, line := range lines[1:] {
		if line == "" {
			continue
		}
		if len(line) > 0 && !unicode.IsSpace(rune(line[0])) {
			return fmt.Errorf("trailer continuation line %d must start with whitespace: %q", i+2, line)
		}
	}

	return nil
}

func buildTrailersBlock(trailers []string) (string, error) {
	if len(trailers) == 0 {
		return "", nil
	}

	for _, trailer := range trailers {
		if err := validateTrailer(trailer); err != nil {
			return "", err
		}
	}

	return strings.Join(trailers, "\n"), nil
}
