package report

import (
	"strings"
	"testing"
)

func TestNormalizeListQueryCapsPageSizeAndFilters(t *testing.T) {
	query := normalizeListQuery(listQuery{Page: -1, PageSize: 1000, Keyword: "  abc  ", TargetType: "用户", ReportType: " Fraud "})
	if query.Page != 1 || query.PageSize != 100 || query.Keyword != "abc" || query.TargetType != targetTypeUser || query.ReportType != "fraud" {
		t.Fatalf("normalized query mismatch: %+v", query)
	}
}

func TestReportWhereUsesStableFilters(t *testing.T) {
	query := listQuery{
		Keyword:    "10001",
		TargetType: targetTypeRoom,
		ReportType: "fraud",
		StartAtMS:  100,
		EndAtMS:    200,
	}
	where, args := reportWhere("lalu", query)
	for _, want := range []string{
		"WHERE r.app_code = ? AND r.target_type = ?",
		"r.report_id LIKE ?",
		"r.room_id LIKE ?",
		"CAST(r.reporter_user_id AS CHAR) LIKE ?",
		"CAST(r.target_user_id AS CHAR) LIKE ?",
		"u.current_display_user_id LIKE ?",
		"u.default_display_user_id LIKE ?",
		"pretty_display_user_id_leases lease",
	} {
		if !strings.Contains(where, want) {
			t.Fatalf("where missing %q: %s", want, where)
		}
	}
	if len(args) != 14 || args[0] != "lalu" || args[1] != targetTypeRoom || args[2] != "fraud" || args[3] != int64(100) || args[4] != int64(200) || args[5] != "%10001%" || args[13] != "%10001%" {
		t.Fatalf("args mismatch: %#v", args)
	}
	if _, ok := args[12].(int64); !ok {
		t.Fatalf("pretty lease expiry arg type = %T, want int64: %#v", args[12], args)
	}
}

func TestParseImageURLsNormalizesEmptyJSON(t *testing.T) {
	urls, err := parseImageURLs("null")
	if err != nil {
		t.Fatalf("parse images failed: %v", err)
	}
	if len(urls) != 0 {
		t.Fatalf("empty images mismatch: %#v", urls)
	}
}
