//go:build integration

package mail_test

import (
	"testing"

	"github.com/ultisuite/ulti-backend/internal/integrationtest"
)

func TestMailSettingsCRUD(t *testing.T) {
	h := integrationtest.RequireHarness(t)
	client, claims := integrationtest.RequireUserClient(t, h)

	resp, err := client.Get("/api/v1/mail/settings")
	integrationtest.FailIf(err, t, "GET settings")
	integrationtest.FailUnlessStatus(t, resp, 200)
	integrationtest.AssertUserProvisioned(t, h, claims.Sub)

	var defaults map[string]any
	integrationtest.DecodeJSON(t, resp, &defaults)
	if defaults["density"] != "default" {
		t.Fatalf("density = %v, want default", defaults["density"])
	}

	resp, err = client.Patch("/api/v1/mail/settings", map[string]string{"density": "compact"})
	integrationtest.FailIf(err, t, "PATCH settings")
	integrationtest.FailUnlessStatus(t, resp, 200)

	var updated map[string]any
	integrationtest.DecodeJSON(t, resp, &updated)
	if updated["density"] != "compact" {
		t.Fatalf("density = %v, want compact", updated["density"])
	}

	resp, err = client.Get("/api/v1/mail/settings")
	integrationtest.FailIf(err, t, "GET settings after patch")
	integrationtest.FailUnlessStatus(t, resp, 200)
	integrationtest.DecodeJSON(t, resp, &updated)
	if updated["density"] != "compact" {
		t.Fatalf("persisted density = %v, want compact", updated["density"])
	}
}
