package mysql

import (
	"context"
	"testing"

	"github.com/DATA-DOG/go-sqlmock"
	"hyapp/pkg/appcode"
)

func TestClaimPendingGameOutboxPrioritizesRecoveryOverPendingAtBatchOne(t *testing.T) {
	tests := []struct {
		name             string
		eventID          string
		recoveryStatus   string
		expectEmptyRetry bool
	}{
		{name: "due retry", eventID: "game-retry", recoveryStatus: "retryable"},
		{name: "expired run", eventID: "game-expired", recoveryStatus: "running", expectEmptyRetry: true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			db, mock, err := sqlmock.New()
			if err != nil {
				t.Fatalf("create sqlmock: %v", err)
			}
			defer db.Close()

			mock.ExpectBegin()
			if tt.expectEmptyRetry {
				mock.ExpectQuery(gameOutboxRetryClaimPattern()).
					WithArgs("lalu", sqlmock.AnyArg(), 1).
					WillReturnRows(gameOutboxClaimRows())
			}
			claimPattern := gameOutboxRetryClaimPattern()
			if tt.recoveryStatus == "running" {
				claimPattern = gameOutboxExpiredClaimPattern()
			}
			mock.ExpectQuery(claimPattern).
				WithArgs("lalu", sqlmock.AnyArg(), 1).
				WillReturnRows(gameOutboxClaimRows().AddRow(
					"lalu", tt.eventID, "GameOrderSettled", "order-1", int64(1001), "self", "dice", "debit",
					int64(10), `{}`, int64(200),
				))
			mock.ExpectExec(`(?s)UPDATE game_outbox\s+SET status = 'running', worker_id = \?, lock_until_ms = \?, updated_at_ms = \?\s+WHERE app_code = \? AND event_id = \?`).
				WithArgs("worker-1", sqlmock.AnyArg(), sqlmock.AnyArg(), "lalu", tt.eventID).
				WillReturnResult(sqlmock.NewResult(0, 1))
			mock.ExpectCommit()

			records, err := (&Repository{db: db}).ClaimPendingGameOutbox(
				appcode.WithContext(context.Background(), "lalu"), "worker-1", 1,
			)
			if err != nil {
				t.Fatalf("claim game recovery row: %v", err)
			}
			if len(records) != 1 || records[0].EventID != tt.eventID {
				t.Fatalf("batch=1 must claim %s before an older pending row: %+v", tt.recoveryStatus, records)
			}
			// No pending query is registered: issuing one after the recovery row filled
			// the batch makes sqlmock fail, protecting the starvation boundary directly.
			if err := mock.ExpectationsWereMet(); err != nil {
				t.Fatalf("game claim priority mismatch: %v", err)
			}
		})
	}
}

func gameOutboxClaimRows() *sqlmock.Rows {
	return sqlmock.NewRows([]string{
		"app_code", "event_id", "event_type", "order_id", "user_id", "platform_code", "game_id", "op_type",
		"coin_amount", "payload_json", "created_at_ms",
	})
}

func gameOutboxRetryClaimPattern() string {
	return `(?s)FROM game_outbox FORCE INDEX \(idx_game_outbox_status_created\).*WHERE app_code = \? AND status = 'retryable' AND next_retry_at_ms <= \? AND lock_until_ms = 0.*FOR UPDATE SKIP LOCKED`
}

func gameOutboxExpiredClaimPattern() string {
	return `(?s)FROM game_outbox FORCE INDEX \(idx_game_outbox_lock\).*WHERE app_code = \? AND status = 'running' AND lock_until_ms <= \?.*FOR UPDATE SKIP LOCKED`
}
