package mysql

import (
	"context"
	"regexp"
	"testing"

	"github.com/DATA-DOG/go-sqlmock"

	"hyapp/pkg/appcode"
)

func TestUpdateTransactionMetadataAcceptsMatchedButUnchangedRow(t *testing.T) {
	db, mock, err := sqlmock.New()
	if err != nil {
		t.Fatalf("create sqlmock: %v", err)
	}
	defer db.Close()

	mock.ExpectBegin()
	tx, err := db.Begin()
	if err != nil {
		t.Fatalf("begin transaction: %v", err)
	}
	mock.ExpectExec(regexp.QuoteMeta(`UPDATE wallet_transactions
		 SET metadata_json = ?, updated_at_ms = ?
		 WHERE app_code = ? AND transaction_id = ?`)).
		WithArgs(`{"balance_version":9}`, int64(1234), "lalu", "tx-1").
		WillReturnResult(sqlmock.NewResult(0, 0))

	repo := &Repository{}
	ctx := appcode.WithContext(context.Background(), "lalu")
	if err := repo.updateTransactionMetadata(ctx, tx, "tx-1", map[string]int64{"balance_version": 9}, 1234); err != nil {
		t.Fatalf("unchanged matched row must not fail: %v", err)
	}
	if err := mock.ExpectationsWereMet(); err != nil {
		t.Fatalf("unmet SQL expectations: %v", err)
	}
}
