package commerce

import (
	"encoding/json"
	"testing"
	"time"
)

func TestAdminUserUpdateRequestTracksExplicitNull(t *testing.T) {
	var req AdminUserUpdateRequest
	if err := json.Unmarshal([]byte(`{"email":null,"phone":null,"member_level_id":null,"agent_tier":null}`), &req); err != nil {
		t.Fatalf("decode request: %v", err)
	}
	for _, key := range []string{"email", "phone", "member_level_id", "agent_tier"} {
		if !req.has(key) {
			t.Fatalf("expected %q to be marked present", key)
		}
	}
	if req.has("nickname") {
		t.Fatal("omitted nickname must not be marked present")
	}
}

func TestAdminUserUpdateRequestInternalFieldsRemainUsable(t *testing.T) {
	status := "banned"
	req := AdminUserUpdateRequest{Status: &status}
	if req.Status == nil || *req.Status != status {
		t.Fatal("internal status request was not retained")
	}
}

func TestAdminInstanceMapUsesFrontendFieldNames(t *testing.T) {
	now := time.Date(2026, time.March, 12, 10, 0, 0, 0, time.UTC)
	item := adminInstanceMap(Instance{
		ID: 9, UserID: 2, OrderID: 3, ProductID: 4, Status: "active", Cycle: "monthly",
		UnitPrice: 1299, NextRenewAmount: 1299, AutoRenew: 1, CreatedAt: now,
	})
	for _, key := range []string{"id", "user_id", "order_id", "product_id", "status", "cycle", "unit_price", "next_renew_amount", "auto_renew", "created_at"} {
		if _, ok := item[key]; !ok {
			t.Fatalf("missing API field %q", key)
		}
	}
	for _, key := range []string{"ID", "Status", "UnitPrice", "NextRenewAmount"} {
		if _, ok := item[key]; ok {
			t.Fatalf("unexpected Go field name %q", key)
		}
	}
}
