package tickets

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

	"epigas.gitea.cloud/RiskRancher/core/pkg/domain"
)

type InlineUpdateRequest struct {
	Severity               string `json:"severity"`
	Comment                string `json:"comment"`
	Description            string `json:"description"`
	RecommendedRemediation string `json:"recommended_remediation"`
	Actor                  string `json:"actor"`
	Status                 string `json:"status"`
	Assignee               string `json:"assignee"`
}

type BulkUpdateRequest struct {
	TicketIDs []int  `json:"ticket_ids"`
	Status    string `json:"status"`
	Comment   string `json:"comment"`
	Assignee  string `json:"assignee"`
	Actor     string `json:"actor"`
}

type MagistrateReviewRequest struct {
	Action        string `json:"action"`
	Actor         string `json:"actor"`
	Comment       string `json:"comment"`
	ExtensionDays int    `json:"extension_days"`
}

func (h *Handler) HandleUpdateTicket(w http.ResponseWriter, r *http.Request) {
	id, _ := strconv.Atoi(r.PathValue("id"))
	var req InlineUpdateRequest
	json.NewDecoder(r.Body).Decode(&req)

	if err := h.Store.UpdateTicketInline(r.Context(), id, req.Severity, req.Description, req.RecommendedRemediation, req.Comment, req.Actor, req.Status, req.Assignee); err != nil {
		http.Error(w, "Database error", http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusOK)
}

// HandleGetTickets fetches a list of tickets via the API
func (h *Handler) HandleGetTickets(w http.ResponseWriter, r *http.Request) {
	tickets, err := h.Store.GetTickets(r.Context())
	if err != nil {
		http.Error(w, "Database error", http.StatusInternalServerError)
		return
	}

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

// HandleCreateTicket creates a single ticket via the API
func (h *Handler) HandleCreateTicket(w http.ResponseWriter, r *http.Request) {
	var t domain.Ticket
	if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
		http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
		return
	}

	if err := h.Store.CreateTicket(r.Context(), &t); err != nil {
		http.Error(w, "Failed to create ticket", http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusCreated)
	json.NewEncoder(w).Encode(t)
}
