// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gerrit

import (
	"context"
	"maps"
	"slices"
	"testing"

	"golang.org/x/oscar/internal/secret"
	"golang.org/x/oscar/internal/storage"
	"golang.org/x/oscar/internal/testutil"
)

func TestLoadTxtar(t *testing.T) {
	check := testutil.Checker(t)

	lg := testutil.Slogger(t)
	db := storage.MemDB()
	sdb := secret.Empty()
	c := New("gerrit-test", lg, db, sdb, nil)

	tc := c.Testing()
	check(tc.LoadTxtar("testdata/change.txt"))
	ch := tc.change(1)
	if ch == nil {
		t.Fatal("could not find loaded change")
	}

	tests := []changeTests{
		{
			"ChangeLabels",
			wa(c.ChangeLabels),
			map[string]string{
				"Hold": "put change on hold",
			},
			func(got, want any) bool {
				g := got.(map[string]*LabelInfo)
				w := want.(map[string]string)
				m := make(map[string]string)
				for k, l := range g {
					m[k] = l.Description
				}
				return maps.Equal(w, m)
			},
		},
		{
			"ChangeOwner",
			wa(c.ChangeOwner),
			"gopher@golang.org",
			func(got, want any) bool {
				return got.(*AccountInfo).Email == want
			},
		},
		{
			"ChangeDescription",
			wa(c.ChangeDescription),
			"gerrit: test change",
			nil,
		},
		{
			"ChangeHashtags",
			wa(c.ChangeHashtags),
			[]string{"tag1", "tag2"},
			func(got, want any) bool {
				g := got.([]string)
				w := want.([]string)
				return slices.Equal(g, w)
			},
		},
		{
			"ChangeMessages",
			wa(c.ChangeMessages),
			[]string{
				"message 1",
				"message 2",
			},
			func(got, want any) bool {
				g := got.([]*ChangeMessageInfo)
				w := want.([]string)
				for i, m := range g {
					if m.Message != w[i] {
						return false
					}
				}
				return true
			},
		},
	}

	testChangeTests(t, ch, tests)

	want := "comment 1"
	if got := tc.comments[1][0].Message; got != want {
		t.Errorf("change #1: got comment %q, want %q", got, want)
	}
}

func TestTestingChanges(t *testing.T) {
	check := testutil.Checker(t)
	ctx := context.Background()

	tc := newTestingClient()
	tc.queryLimit = 1000 // grab everything in one batch
	check(tc.LoadTxtar("testdata/uniquetimes.txt"))

	cnt := 0
	// There should be six changes matching the criteria, but skip the first one.
	for _, err := range tc.changes(ctx, "test", "2020-03-01 10:10:10.00000000", "2020-08-01 10:10:10.00000000", 1) {
		check(err)
		cnt++
	}

	if cnt != 5 {
		t.Errorf("want 5 changes; got %d", cnt)
	}
}
