package api

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

	"bookra/apps/backend/internal/config"
)

func TestDispatchReminderJobsRequiresJobRunnerKey(t *testing.T) {
	server, err := NewServer(config.Config{
		Environment: "development",
		FrontendURL: "http://localhost:3000",
		APIURL:      "http://localhost:8080",
		DemoMode:    true,
	}, nil)
	if err != nil {
		t.Fatalf("new server: %v", err)
	}
	defer server.Close()

	request := httptest.NewRequest(http.MethodPost, "/v1/internal/jobs/reminders/dispatch", nil)
	recorder := httptest.NewRecorder()

	server.Handler().ServeHTTP(recorder, request)

	if recorder.Code != http.StatusUnauthorized {
		t.Fatalf("expected 401, got %d body=%s", recorder.Code, recorder.Body.String())
	}
}

func TestDispatchReminderJobsAcceptsConfiguredJobRunnerKey(t *testing.T) {
	server, err := NewServer(config.Config{
		Environment:  "development",
		FrontendURL:  "http://localhost:3000",
		APIURL:       "http://localhost:8080",
		JobRunnerKey: "job-secret",
		DemoMode:     true,
	}, nil)
	if err != nil {
		t.Fatalf("new server: %v", err)
	}
	defer server.Close()

	request := httptest.NewRequest(http.MethodPost, "/v1/internal/jobs/reminders/dispatch", nil)
	request.Header.Set("X-Bookra-Job-Key", "job-secret")
	recorder := httptest.NewRecorder()

	server.Handler().ServeHTTP(recorder, request)

	if recorder.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", recorder.Code, recorder.Body.String())
	}
}
