package sync

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestCalculateNextRetry(t *testing.T) {
	processor := NewSyncQueueProcessor(nil)

	tests := []struct {
		name     string
		attempts int32
		minDelay time.Duration
		maxDelay time.Duration
	}{
		{"Attempt 0", 0, -100 * time.Millisecond, 1 * time.Second},
		{"Attempt 1", 1, 59 * time.Second, 61 * time.Second},
		{"Attempt 2", 2, 4*time.Minute + 59*time.Second, 5*time.Minute + 1*time.Second},
		{"Attempt 3", 3, 14*time.Minute + 59*time.Second, 15*time.Minute + 1*time.Second},
		{"Attempt 4", 4, 59*time.Minute + 59*time.Second, 60*time.Minute + 1*time.Second},
		{"Attempt 5", 5, 23*time.Hour + 59*time.Minute, 24*time.Hour + 1*time.Minute},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			nextRetry := processor.calculateNextRetry(tt.attempts)
			delay := nextRetry.Sub(time.Now())

			assert.GreaterOrEqual(t, delay, tt.minDelay, "delay should be at least minDelay")
			assert.LessOrEqual(t, delay, tt.maxDelay, "delay should be at most maxDelay")
		})
	}
}

func TestSyncTypeConstants(t *testing.T) {
	assert.Equal(t, "progress", SyncTypeProgress)
	assert.Equal(t, "note", SyncTypeNote)
	assert.Equal(t, "highlight", SyncTypeHighlight)
	assert.Equal(t, "bookmark", SyncTypeBookmark)
}

func TestSyncStatusConstants(t *testing.T) {
	assert.Equal(t, "pending", SyncStatusPending)
	assert.Equal(t, "processing", SyncStatusProcessing)
	assert.Equal(t, "completed", SyncStatusCompleted)
	assert.Equal(t, "failed", SyncStatusFailed)
}

func TestPriorityConstants(t *testing.T) {
	assert.Equal(t, 1, PriorityUserInitiated)
	assert.Equal(t, 2, PriorityBookCompletion)
	assert.Equal(t, 3, PriorityCriticalNote)
	assert.Equal(t, 5, PriorityPageTurn)
	assert.Equal(t, 7, PriorityCheckpoint)
	assert.Equal(t, 10, PriorityBackgroundSync)
}
