package pipeline

import (
	"errors"
	"strings"
	"unicode/utf8"

	"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)

func validateNormalizeRetry(retry *contracts.NormalizeRetry) error {
	if retry == nil {
		return errors.New("normalize retry directive is missing")
	}
	if !utf8.ValidString(retry.ReasonCode) {
		return errors.New("normalize retry directive reason code has invalid UTF-8")
	}
	if strings.TrimSpace(retry.ReasonCode) == "" {
		return errors.New("normalize retry directive reason code is blank")
	}
	if len(retry.ReasonCode) > contracts.MaxNormalizeRetryReasonCodeBytes {
		return errors.New("normalize retry directive reason code exceeds maximum length")
	}
	if !utf8.ValidString(retry.Message) {
		return errors.New("normalize retry directive message has invalid UTF-8")
	}
	if strings.TrimSpace(retry.Message) == "" {
		return errors.New("normalize retry directive message is blank")
	}
	if len(retry.Message) > contracts.MaxNormalizeRetryMessageBytes {
		return errors.New("normalize retry directive message exceeds maximum length")
	}
	if !utf8.ValidString(retry.CorrectionGuidance) {
		return errors.New("normalize retry directive correction guidance has invalid UTF-8")
	}
	if retry.CorrectionGuidance != "" && strings.TrimSpace(retry.CorrectionGuidance) == "" {
		return errors.New("normalize retry directive correction guidance is blank")
	}
	if len(retry.CorrectionGuidance) > contracts.MaxNormalizeRetryCorrectionGuidanceBytes {
		return errors.New("normalize retry directive correction guidance exceeds maximum length")
	}
	return nil
}
