package service_test

import (
	"testing"

	"bit.admedia.com/scm/ad/adpilot-indexing-repo-sync.com/internal/domain"
	"bit.admedia.com/scm/ad/adpilot-indexing-repo-sync.com/internal/service"
)

func TestForceSyncAllowed(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name    string
		status  string
		hasRun  bool
		force   bool
		allowed bool
	}{
		{name: "override ready", status: domain.IndexingRunStatusCompleted, hasRun: true, force: true, allowed: true},
		{name: "ready without override", status: domain.IndexingRunStatusCompleted, hasRun: true, force: false, allowed: false},
		{name: "running", status: domain.IndexingRunStatusRunning, hasRun: true, allowed: true},
		{name: "failed", status: domain.IndexingRunStatusFailed, hasRun: true, allowed: true},
		{name: "no run", hasRun: false, allowed: true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			meta := newMockMetadataStore()
			if tt.hasRun {
				meta.indexingRuns["run_1"] = domain.IndexingRun{
					RepoID: "ad/example",
					Status: tt.status,
				}
			}
			svc := service.NewSyncService(nil, nil, meta, nil)
			got := svc.ForceSyncAllowedForTest("ad/example", tt.force)
			if got != tt.allowed {
				t.Fatalf("allowed=%v, want %v", got, tt.allowed)
			}
		})
	}
}
