package captcha

import (
	"context"
	"crypto/rand"
	"encoding/base64"
	"errors"
	"fmt"
	"html"
	"math/big"
	"strings"
	"time"

	"github.com/redis/go-redis/v9"
)

var (
	ErrDependencyUnavailable = errors.New("captcha dependency unavailable")
	ErrInvalid               = errors.New("captcha invalid")
)

type Item struct {
	CaptchaID string `json:"captcha_id"`
	Image     string `json:"image"`
	ExpiresIn int64  `json:"expires_in"`
}

func Generate(ctx context.Context, redisClient *redis.Client, namespace string, ttl time.Duration) (*Item, error) {
	if redisClient == nil {
		return nil, ErrDependencyUnavailable
	}
	captchaID, err := randomToken(16)
	if err != nil {
		return nil, err
	}
	code, err := randomCaptchaCode(4)
	if err != nil {
		return nil, err
	}
	if err := redisClient.Set(ctx, key(namespace, captchaID), strings.ToUpper(code), ttl).Err(); err != nil {
		return nil, err
	}
	return &Item{
		CaptchaID: captchaID,
		Image:     imageDataURL(code),
		ExpiresIn: int64(ttl.Seconds()),
	}, nil
}

func Verify(ctx context.Context, redisClient *redis.Client, namespace string, captchaID string, captchaCode string) error {
	if redisClient == nil {
		return ErrDependencyUnavailable
	}
	captchaID = strings.TrimSpace(captchaID)
	captchaCode = strings.TrimSpace(captchaCode)
	if captchaID == "" || captchaCode == "" {
		return ErrInvalid
	}

	redisKey := key(namespace, captchaID)
	stored, err := redisClient.Get(ctx, redisKey).Result()
	if errors.Is(err, redis.Nil) {
		return ErrInvalid
	}
	if err != nil {
		return err
	}
	_ = redisClient.Del(ctx, redisKey).Err()
	if strings.ToUpper(captchaCode) != stored {
		return ErrInvalid
	}
	return nil
}

func key(namespace string, id string) string {
	namespace = strings.Trim(strings.ToLower(strings.TrimSpace(namespace)), ":")
	if namespace == "" {
		namespace = "default"
	}
	return "captcha:" + namespace + ":" + id
}

func randomToken(length int) (string, error) {
	buf := make([]byte, length)
	if _, err := rand.Read(buf); err != nil {
		return "", err
	}
	return base64.RawURLEncoding.EncodeToString(buf), nil
}

func randomCaptchaCode(length int) (string, error) {
	const alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
	result := make([]byte, length)
	for i := range result {
		n, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphabet))))
		if err != nil {
			return "", err
		}
		result[i] = alphabet[n.Int64()]
	}
	return string(result), nil
}

func imageDataURL(code string) string {
	safeCode := html.EscapeString(strings.ToUpper(code))
	svg := fmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" width="132" height="44" viewBox="0 0 132 44">
<rect width="132" height="44" rx="8" fill="#eef5f7"/>
<path d="M8 32 C32 2, 62 52, 124 12" stroke="#0f766e" stroke-width="2" fill="none" opacity=".28"/>
<path d="M10 13 C42 42, 86 0, 122 31" stroke="#2563eb" stroke-width="2" fill="none" opacity=".22"/>
<text x="66" y="29" text-anchor="middle" font-family="Menlo,Consolas,monospace" font-size="24" font-weight="700" letter-spacing="4" fill="#111827">%s</text>
</svg>`, safeCode)
	return "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString([]byte(svg))
}
