package channel

import (
	"net/http"
	"strings"

	"github.com/tidbcloud/AgentTeam/internal/api"
)

// ChannelAccessHandler owns route-facing channel access resolution.
type ChannelAccessHandler struct {
	channels ChannelAccessStore
}

// Resolve handles GET /api/channels/:channelId/access.
// It returns one explicit route state for the channel id rather than relying on
// entrypoint-provided preview cache or seed data.
func (h *ChannelAccessHandler) Resolve(r api.WorkspaceScopedRequest) (api.Response, error) {
	authenticated := r.User
	workspaceID := r.WorkspaceID()

	channelID := strings.TrimSpace(r.Gin.Param("channelId"))
	if channelID == "" {
		return api.Response{}, api.NewHTTPError(http.StatusBadRequest, "channelId is required")
	}

	resolution, err := h.channels.ResolveChannelAccessForUser(r.Context(), authenticated.User.ID, workspaceID, channelID)
	if err != nil {
		return api.Response{}, err
	}
	return api.OK(channelAccessResponseFromResolution(resolution)), nil
}
