package cloudflare

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

// Handler handles Cloudflare admin endpoints
type Handler struct {
	client *Client
}

// NewHandler creates a new Cloudflare handler
func NewHandler() *Handler {
	return &Handler{
		client: NewClient(),
	}
}

// PurgeURLsRequest contains URLs to purge from cache
type PurgeURLsRequest struct {
	URLs []string `json:"urls"`
}

// PurgeTagsRequest contains cache tags to purge
type PurgeTagsRequest struct {
	Tags []string `json:"tags"`
}

// PurgeHostsRequest contains hostnames to purge
type PurgeHostsRequest struct {
	Hosts []string `json:"hosts"`
}

// GetZones godoc
// @Summary      List Cloudflare Zones
// @Description  Returns all zones associated with the Cloudflare account
// @Tags         Admin - Cloudflare
// @Accept       json
// @Produce      json
// @Success      200  {array}   Zone
// @Failure      500  {string}  string  "Internal Server Error"
// @Security     BearerAuth
// @Router       /api/v1/admin/cloudflare/zones [get]
func (h *Handler) GetZones(w http.ResponseWriter, r *http.Request) {
	zones, err := h.client.GetZones()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

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

// PurgeAll godoc
// @Summary      Purge All Cache
// @Description  Purges all cached content for the configured zone
// @Tags         Admin - Cloudflare
// @Accept       json
// @Produce      json
// @Success      200  {object}  PurgeResponse
// @Failure      500  {string}  string  "Internal Server Error"
// @Security     BearerAuth
// @Router       /api/v1/admin/cloudflare/cache/purge-all [post]
func (h *Handler) PurgeAll(w http.ResponseWriter, r *http.Request) {
	result, err := h.client.PurgeAll()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

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

// PurgeByURLs godoc
// @Summary      Purge Cache by URLs
// @Description  Purges specific URLs from cache
// @Tags         Admin - Cloudflare
// @Accept       json
// @Produce      json
// @Param        body  body      PurgeURLsRequest  true  "URLs to purge"
// @Success      200   {object}  PurgeResponse
// @Failure      400   {string}  string  "Bad Request"
// @Failure      500   {string}  string  "Internal Server Error"
// @Security     BearerAuth
// @Router       /api/v1/admin/cloudflare/cache/purge-urls [post]
func (h *Handler) PurgeByURLs(w http.ResponseWriter, r *http.Request) {
	var req PurgeURLsRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid request body", http.StatusBadRequest)
		return
	}

	if len(req.URLs) == 0 {
		http.Error(w, "URLs array is required", http.StatusBadRequest)
		return
	}

	result, err := h.client.PurgeByURLs(req.URLs)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

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

// PurgeByTags godoc
// @Summary      Purge Cache by Tags
// @Description  Purges content by cache tags (Enterprise only)
// @Tags         Admin - Cloudflare
// @Accept       json
// @Produce      json
// @Param        body  body      PurgeTagsRequest  true  "Tags to purge"
// @Success      200   {object}  PurgeResponse
// @Failure      400   {string}  string  "Bad Request"
// @Failure      500   {string}  string  "Internal Server Error"
// @Security     BearerAuth
// @Router       /api/v1/admin/cloudflare/cache/purge-tags [post]
func (h *Handler) PurgeByTags(w http.ResponseWriter, r *http.Request) {
	var req PurgeTagsRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid request body", http.StatusBadRequest)
		return
	}

	if len(req.Tags) == 0 {
		http.Error(w, "Tags array is required", http.StatusBadRequest)
		return
	}

	result, err := h.client.PurgeByTags(req.Tags)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

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

// PurgeByHosts godoc
// @Summary      Purge Cache by Hosts
// @Description  Purges content by hostnames
// @Tags         Admin - Cloudflare
// @Accept       json
// @Produce      json
// @Param        body  body      PurgeHostsRequest  true  "Hosts to purge"
// @Success      200   {object}  PurgeResponse
// @Failure      400   {string}  string  "Bad Request"
// @Failure      500   {string}  string  "Internal Server Error"
// @Security     BearerAuth
// @Router       /api/v1/admin/cloudflare/cache/purge-hosts [post]
func (h *Handler) PurgeByHosts(w http.ResponseWriter, r *http.Request) {
	var req PurgeHostsRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid request body", http.StatusBadRequest)
		return
	}

	if len(req.Hosts) == 0 {
		http.Error(w, "Hosts array is required", http.StatusBadRequest)
		return
	}

	result, err := h.client.PurgeByHosts(req.Hosts)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

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