package handler

import (
	"net/http"

	"github.com/gin-gonic/gin"

	sessionManager "github.com/pardnchiu/agenvoy/internal/session"
	"github.com/pardnchiu/go-pkg/filesystem/keychain"
)

func GetKey() gin.HandlerFunc {
	return func(c *gin.Context) {
		key := c.Query("key")
		if key == "" {
			c.JSON(http.StatusBadRequest, gin.H{"error": "key is required"})
			return
		}
		if !sessionManager.IsKeyExist(key) {
			c.JSON(http.StatusForbidden, gin.H{"error": "key not registered"})
			return
		}

		value := keychain.Get(key)
		if value == "" {
			c.JSON(http.StatusNotFound, gin.H{"error": "key not found"})
			return
		}
		c.JSON(http.StatusOK, gin.H{"key": key, "value": value})
	}
}
