package mail

import (
	"strings"

	"github.com/ultisuite/ulti-backend/internal/api/apivalidate"
)

const (
	maxSignatureRequestBody = 256 << 10 // 256 KiB
	maxSignatureName        = 128
	maxSignatureHTMLContent = 64 << 10 // 64 KiB
)

type createSignatureRequest struct {
	Name string `json:"name"`
	HTML string `json:"html"`
}

type updateSignatureRequest struct {
	Name string `json:"name"`
	HTML string `json:"html"`
}

func validateCreateSignature(req *createSignatureRequest) *apivalidate.ValidationError {
	var details []apivalidate.FieldDetail
	name := strings.TrimSpace(req.Name)
	if name == "" {
		details = append(details, apivalidate.FieldDetail{Field: "name", Message: "required"})
	} else if len(name) > maxSignatureName {
		details = append(details, apivalidate.FieldDetail{Field: "name", Message: "too long"})
	}
	if len(req.HTML) > maxSignatureHTMLContent {
		details = append(details, apivalidate.FieldDetail{Field: "html", Message: "too long"})
	}
	if len(details) == 0 {
		req.Name = name
		return nil
	}
	return apivalidate.NewValidationError(details...)
}

func validateUpdateSignature(req *updateSignatureRequest) *apivalidate.ValidationError {
	var details []apivalidate.FieldDetail
	name := strings.TrimSpace(req.Name)
	if name == "" {
		details = append(details, apivalidate.FieldDetail{Field: "name", Message: "required"})
	} else if len(name) > maxSignatureName {
		details = append(details, apivalidate.FieldDetail{Field: "name", Message: "too long"})
	}
	if len(req.HTML) > maxSignatureHTMLContent {
		details = append(details, apivalidate.FieldDetail{Field: "html", Message: "too long"})
	}
	if len(details) == 0 {
		req.Name = name
		return nil
	}
	return apivalidate.NewValidationError(details...)
}
