package mail

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

func TestSearchMessages(t *testing.T) {
	svc := newFakeMailService()
	router := newTestMailRouter(svc)

	req := httptest.NewRequest(http.MethodGet, "/search?q=hello", nil)
	rec := httptest.NewRecorder()
	router.ServeHTTP(rec, req)

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

func TestSearchMessagesBySender(t *testing.T) {
	svc := newFakeMailService()
	router := newTestMailRouter(svc)

	req := httptest.NewRequest(http.MethodGet, "/search?sender=alice@example.com", nil)
	rec := httptest.NewRecorder()
	router.ServeHTTP(rec, req)

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

func TestSearchMessagesFromEmailParam(t *testing.T) {
	svc := newFakeMailService()
	router := newTestMailRouter(svc)

	req := httptest.NewRequest(http.MethodGet, "/search?from=alice@example.com", nil)
	rec := httptest.NewRecorder()
	router.ServeHTTP(rec, req)

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

func TestSearchMessagesRequiresFilter(t *testing.T) {
	svc := newFakeMailService()
	router := newTestMailRouter(svc)

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

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