package handlers

import (
	"context"
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/orchard9/rdev/pkg/api"
)

// QueryBlocked returns all blocked features in a project.
// GET /projects/{id}/sdlc/query/blocked
func (h *SDLCHandler) QueryBlocked(w http.ResponseWriter, r *http.Request) {
	projectID := chi.URLParam(r, "id")

	ctx, cancel := context.WithTimeout(r.Context(), TimeoutStandard)
	defer cancel()

	blocked, err := h.sdlcService.QueryBlocked(ctx, projectID)
	if err != nil {
		writeSDLCError(w, r, err)
		return
	}

	api.WriteSuccess(w, r, blocked)
}

// QueryReady returns features ready for work in a project.
// GET /projects/{id}/sdlc/query/ready
func (h *SDLCHandler) QueryReady(w http.ResponseWriter, r *http.Request) {
	projectID := chi.URLParam(r, "id")

	ctx, cancel := context.WithTimeout(r.Context(), TimeoutStandard)
	defer cancel()

	ready, err := h.sdlcService.QueryReady(ctx, projectID)
	if err != nil {
		writeSDLCError(w, r, err)
		return
	}

	api.WriteSuccess(w, r, ready)
}

// QueryNeedsApproval returns features awaiting approval in a project.
// GET /projects/{id}/sdlc/query/needs-approval
func (h *SDLCHandler) QueryNeedsApproval(w http.ResponseWriter, r *http.Request) {
	projectID := chi.URLParam(r, "id")

	ctx, cancel := context.WithTimeout(r.Context(), TimeoutStandard)
	defer cancel()

	pending, err := h.sdlcService.QueryNeedsApproval(ctx, projectID)
	if err != nil {
		writeSDLCError(w, r, err)
		return
	}

	api.WriteSuccess(w, r, pending)
}
