package handler

import (
	"fmt"
	"net/http"
	"strings"
)

// GetIncident responds to the link generated in the response to a New Snow
func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
	h.Logger.Debug("GetIncident Request received", "method", r.Method, "path", r.URL.Path)
	// TODO

	path := r.URL.Path
	// Expected format: /api/now/table/incident/{id}
	parts := strings.Split(path, "/")
	h.Logger.Debug("Request path", "parts", parts)

	// Parse and write query parameters
	query := r.URL.Query()
	if len(query) == 0 {
		h.Logger.Debug("No query parameters.")
	} else {
		//fmt.Fprintln(w, "Query parameters:")
		for key, values := range query {
			for _, value := range values {
				h.Logger.Debug("Query Paramater", "key", key, "value", value)
			}
		}
	}

	if len(parts) == 6 && strings.HasPrefix(path, "/api/now/table/incident/") {
		// Requested a single incident
		id := parts[5] // Extract {id}
		h.Logger.Debug("GetIncident called for specific incident", "id", id)
	} else if strings.HasPrefix(path, "/api/now/table/incident") {
		h.Logger.Debug("GetIncident called for list of incidents")

	}

	// TODO - provide an incident list if necessary

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	fmt.Fprintf(w, "{\"result\": [{}]}")
}
