package handlers

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

// DeleteMediaFile deletes a previously uploaded media file from Meta's servers.
// POST /api/media-delete  {"account_id","media_id"}
func (h *Handlers) DeleteMediaFile(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}

	var req struct {
		AccountID string `json:"account_id"`
		MediaID   string `json:"media_id"`
	}
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	if req.MediaID == "" {
		http.Error(w, "media_id is required", http.StatusBadRequest)
		return
	}

	baClient, err := h.getBusinessAPIClient(req.AccountID)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	if err := baClient.DeleteMedia(r.Context(), req.MediaID); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	writeJSON(w, map[string]string{"status": "ok"})
}
