package api

import (
	"net/http"
	"testing"

	"github.com/gin-gonic/gin"
	"github.com/pingcap/dbaas/billing2/utils/ginutils"
	"github.com/pingcap/dbaas/billing2/utils/tests"
)

func newSnapshotTestRouter() *gin.Engine {
	router := gin.New()
	router.Use(ginutils.ErrorMiddleWare)
	InitializeSnapshotRouter(ginutils.MyRoute{Route: router})
	return router
}

func TestPreviewRawUserUnitCostRerunRejectsInvalidWindow(t *testing.T) {
	router := newSnapshotTestRouter()

	w := tests.PerformRequest(router, "POST", "/tenants/1/billing_config_id/1/snapshot/reruns/raw_user_unit_costs/preview", &SnapshotRerunPreviewReq{
		StartDate: "2026-01-02T00:00:00Z",
		EndDate:   "2026-01-01T00:00:00Z",
	})
	if w.Code != http.StatusBadRequest {
		t.Fatalf("expected status %d, got %d: %s", http.StatusBadRequest, w.Code, w.Body.String())
	}
}

func TestPreviewRawUserUnitCostRerunRejectsPublish(t *testing.T) {
	router := newSnapshotTestRouter()

	w := tests.PerformRequest(router, "POST", "/tenants/1/billing_config_id/1/snapshot/reruns/raw_user_unit_costs/preview", &SnapshotRerunPreviewReq{
		StartDate: "2026-01-01T00:00:00Z",
		EndDate:   "2026-01-02T00:00:00Z",
		Publish:   true,
	})
	if w.Code != http.StatusBadRequest {
		t.Fatalf("expected status %d, got %d: %s", http.StatusBadRequest, w.Code, w.Body.String())
	}
}
