package handlers

import (
	"encoding/json"
	"net/http"

	"github.com/rede5/gohorsejobs/backend/internal/api/middleware"
)

// GetMyApplications lists applications for the authenticated user
// @Summary      Get My Applications
// @Description  List all applications for the logged-in user
// @Tags         Applications
// @Accept       json
// @Produce      json
// @Success      200    {array}   models.ApplicationWithDetails
// @Failure      401    {string}  string  "Unauthorized"
// @Failure      500    {string}  string  "Internal Server Error"
// @Router       /api/v1/applications/me [get]
func (h *ApplicationHandler) GetMyApplications(w http.ResponseWriter, r *http.Request) {
	// Get user ID from context (set by authMiddleware)
	userID, ok := r.Context().Value(middleware.ContextUserID).(string)
	if !ok || userID == "" {
		http.Error(w, "Unauthorized", http.StatusUnauthorized)
		return
	}

	userIDStr := userID

	apps, err := h.Service.GetApplicationsByUser(userIDStr)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(apps)
}
