package routes

import (
	"bssapp-backend/auth"
	"bssapp-backend/queries"
	"encoding/json"
	"net/http"

	"github.com/gorilla/mux"
)

// GET /api/statements/{accountCode}/details?startdate=...&enddate=...&parislemler=1&parislemler=2
func GetStatementDetailsHandler(w http.ResponseWriter, r *http.Request) {

	claims, ok := auth.GetClaimsFromContext(r.Context())
	if !ok || claims == nil {
		http.Error(w, "unauthorized", http.StatusUnauthorized)
		return
	}

	_ = mux.Vars(r)

	belgeNo := r.URL.Query().Get("belgeno")
	if belgeNo == "" {
		http.Error(w, "belgeno is required", http.StatusBadRequest)
		return
	}

	details, err := queries.GetStatementDetails(r.Context(), belgeNo)
	if err != nil {
		http.Error(w, "Error fetching statement details: "+err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	if err := json.NewEncoder(w).Encode(details); err != nil {
		http.Error(w, "Error encoding response: "+err.Error(), http.StatusInternalServerError)
	}
}
