package mail

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/go-chi/chi/v5"

	"github.com/ultisuite/ulti-backend/internal/api/middleware"
	"github.com/ultisuite/ulti-backend/internal/auth"
)

func TestValidateCreateFolder(t *testing.T) {
	t.Run("invalid folder_type", func(t *testing.T) {
		req := &createFolderRequest{
			AccountID:  "acc-1",
			Name:       "Work",
			FolderType: "bogus",
		}
		if verr := validateCreateFolder(req); verr == nil {
			t.Fatal("expected validation error for folder_type")
		}
	})

	t.Run("missing account_id", func(t *testing.T) {
		req := &createFolderRequest{Name: "Work"}
		if verr := validateCreateFolder(req); verr == nil {
			t.Fatal("expected validation error for account_id")
		}
	})
}

func TestListFoldersRequiresAccountID(t *testing.T) {
	svc := newFakeMailService()
	h := NewHandlerWithService(svc)
	r := chi.NewRouter()
	r.Use(middleware.WithTestClaims(&auth.Claims{Sub: testExternalID, Email: "user@example.com"}))
	r.Mount("/", h.FolderLabelRoutes())

	req := httptest.NewRequest(http.MethodGet, "/folders", nil)
	rec := httptest.NewRecorder()
	r.ServeHTTP(rec, req)

	if rec.Code != http.StatusBadRequest {
		t.Fatalf("status = %d, want %d; body = %s", rec.Code, http.StatusBadRequest, rec.Body.String())
	}
}

func TestListFoldersWithAccountID(t *testing.T) {
	svc := newFakeMailService()
	h := NewHandlerWithService(svc)
	r := chi.NewRouter()
	r.Use(middleware.WithTestClaims(&auth.Claims{Sub: testExternalID, Email: "user@example.com"}))
	r.Mount("/", h.FolderLabelRoutes())

	req := httptest.NewRequest(http.MethodGet, "/folders?account_id=acc-1", nil)
	rec := httptest.NewRecorder()
	r.ServeHTTP(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("status = %d, want %d; body = %s", rec.Code, http.StatusOK, rec.Body.String())
	}
}
