package services

import (
	"errors"
	"fmt"
	"regexp"

	"github.com/CubeCraft-Creations/Extrudex/backend/internal/dtos"
	"github.com/CubeCraft-Creations/Extrudex/backend/internal/models"
)

// colorHexPattern validates hex color strings like #FF0000 or #ff0000.
var colorHexPattern = regexp.MustCompile(`^#[0-9A-Fa-f]{6}$`)

// validateFilamentSpool performs validation on a FilamentSpool entity.
// Returns a descriptive error on failure.
func validateFilamentSpool(s *models.FilamentSpool) error {
	if s.Name == "" {
		return errors.New("name is required")
	}
	if s.MaterialBaseID <= 0 {
		return errors.New("material_base_id is required")
	}
	if s.MaterialFinishID <= 0 {
		return errors.New("material_finish_id is required")
	}
	if !colorHexPattern.MatchString(s.ColorHex) {
		return fmt.Errorf("color_hex must be a valid hex color (e.g., #FF0000)")
	}
	if s.InitialGrams <= 0 {
		return errors.New("initial_grams must be greater than 0")
	}
	if s.RemainingGrams < 0 {
		return errors.New("remaining_grams must be >= 0")
	}
	return nil
}

// ValidateCreateFilamentRequest validates a creation DTO.
func ValidateCreateFilamentRequest(req dtos.CreateFilamentRequest) error {
	if req.Name == "" {
		return errors.New("name is required")
	}
	if req.MaterialBaseID <= 0 {
		return errors.New("material_base_id is required")
	}
	if req.MaterialFinishID <= 0 {
		return errors.New("material_finish_id is required")
	}
	if !colorHexPattern.MatchString(req.ColorHex) {
		return fmt.Errorf("color_hex must be a valid hex color (e.g., #FF0000)")
	}
	if req.InitialGrams <= 0 {
		return errors.New("initial_grams must be greater than 0")
	}
	if req.RemainingGrams < 0 {
		return errors.New("remaining_grams must be >= 0")
	}
	return nil
}

// ValidateUpdateFilamentRequest validates partial update fields.
func ValidateUpdateFilamentRequest(req dtos.UpdateFilamentRequest) error {
	if req.ColorHex != nil && !colorHexPattern.MatchString(*req.ColorHex) {
		return fmt.Errorf("color_hex must be a valid hex color (e.g., #FF0000)")
	}
	if req.InitialGrams != nil && *req.InitialGrams <= 0 {
		return errors.New("initial_grams must be greater than 0")
	}
	if req.RemainingGrams != nil && *req.RemainingGrams < 0 {
		return errors.New("remaining_grams must be >= 0")
	}
	return nil
}
