package handlers

import (
	"net/http"
	"testing"

	"github.com/treytartt/honeydue-api/internal/i18n"
	"github.com/treytartt/honeydue-api/internal/models"
	"github.com/treytartt/honeydue-api/internal/testutil"
)

func init() {
	// Initialize i18n so the custom error handler can localize error messages.
	// Other handler tests get this from testutil.SetupTestDB, but these tests
	// don't need a database.
	i18n.Init()
}

func TestDeleteFile_MissingURL_Returns400(t *testing.T) {
	// Use a test storage service — DeleteFile won't reach storage since validation fails first
	storageSvc := newTestStorageService("/var/uploads")
	handler := NewUploadHandler(storageSvc, nil)

	e := testutil.SetupTestRouter()

	// Register route with mock auth middleware
	testUser := &models.User{FirstName: "Test", Email: "test@test.com"}
	testUser.ID = 1
	authGroup := e.Group("/api")
	authGroup.Use(testutil.MockAuthMiddleware(testUser))
	authGroup.DELETE("/uploads/", handler.DeleteFile)

	// Send request with empty JSON body (url field missing)
	w := testutil.MakeRequest(e, http.MethodDelete, "/api/uploads/", map[string]string{}, "test-token")
	testutil.AssertStatusCode(t, w, http.StatusBadRequest)
}

func TestDeleteFile_EmptyURL_Returns400(t *testing.T) {
	storageSvc := newTestStorageService("/var/uploads")
	handler := NewUploadHandler(storageSvc, nil)

	e := testutil.SetupTestRouter()

	// Register route with mock auth middleware
	testUser := &models.User{FirstName: "Test", Email: "test@test.com"}
	testUser.ID = 1
	authGroup := e.Group("/api")
	authGroup.Use(testutil.MockAuthMiddleware(testUser))
	authGroup.DELETE("/uploads/", handler.DeleteFile)

	// Send request with empty url field
	w := testutil.MakeRequest(e, http.MethodDelete, "/api/uploads/", map[string]string{"url": ""}, "test-token")
	testutil.AssertStatusCode(t, w, http.StatusBadRequest)
}
