package site

import (
	"fmt"

	"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/pkg/validation"
)

// CreateRequest represents the HTTP request for creating a site
// Note: Domain will be extracted from SiteURL by the backend
type CreateRequest struct {
	SiteURL string `json:"site_url"`
}

// ValidationErrors represents validation errors in RFC 9457 format
type ValidationErrors struct {
	Errors map[string][]string
}

// Error implements the error interface
func (v *ValidationErrors) Error() string {
	if len(v.Errors) == 0 {
		return ""
	}
	// For backward compatibility with error logging, format as string
	var messages []string
	for field, errs := range v.Errors {
		for _, err := range errs {
			messages = append(messages, fmt.Sprintf("%s: %s", field, err))
		}
	}
	return fmt.Sprintf("validation errors: %v", messages)
}

// Validate validates the create site request fields
// Returns all validation errors grouped together in RFC 9457 format
func (r *CreateRequest) Validate() error {
	v := validation.NewValidator()
	validationErrors := make(map[string][]string)

	// Validate site URL (required)
	if err := v.ValidateURL(r.SiteURL, "site_url"); err != nil {
		errMsg := extractErrorMessage(err.Error())
		validationErrors["site_url"] = append(validationErrors["site_url"], errMsg)
	}

	// Return all errors grouped together in RFC 9457 format
	if len(validationErrors) > 0 {
		return &ValidationErrors{Errors: validationErrors}
	}

	return nil
}

// extractErrorMessage extracts the error message after the field name prefix
// Example: "domain: invalid domain format" -> "Invalid domain format"
func extractErrorMessage(fullError string) string {
	// Find the colon separator
	colonIndex := -1
	for i, char := range fullError {
		if char == ':' {
			colonIndex = i
			break
		}
	}

	if colonIndex == -1 {
		// No colon found, capitalize first letter and return
		if len(fullError) > 0 {
			return string(fullError[0]-32) + fullError[1:]
		}
		return fullError
	}

	// Extract message after colon and trim spaces
	message := fullError[colonIndex+1:]
	if len(message) > 0 && message[0] == ' ' {
		message = message[1:]
	}

	// Capitalize first letter
	if len(message) > 0 {
		firstChar := message[0]
		if firstChar >= 'a' && firstChar <= 'z' {
			message = string(firstChar-32) + message[1:]
		}
	}

	return message
}

// CreateResponse represents the HTTP response after creating a site
type CreateResponse struct {
	ID                       string `json:"id"`
	Domain                   string `json:"domain"`
	SiteURL                  string `json:"site_url"`
	APIKey                   string `json:"api_key"` // Only returned once at creation
	Status                   string `json:"status"`
	VerificationToken        string `json:"verification_token"`
	SearchIndexName          string `json:"search_index_name"`
	VerificationInstructions string `json:"verification_instructions"` // DNS TXT record setup instructions
}
