package app

import (
	"context"
	"testing"
	"time"

	"sub2api-cn-relay-manager/internal/config"
)

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

	batchCalls := 0
	reconcileCalls := 0
	startBackgroundSchedulers(context.Background(), config.StartupConfig{
		Database: config.DatabaseConfig{SQLiteDSN: "file:test.db"},
		Reconcile: config.ReconcileConfig{
			WorkerEnabled: true,
			PollInterval:  15 * time.Minute,
		},
	}, backgroundSchedulers{
		runBatchImport: func(context.Context, string) {
			batchCalls++
		},
		runReconcile: func(context.Context, string, time.Duration) {
			reconcileCalls++
		},
	})

	if batchCalls != 1 {
		t.Fatalf("batchCalls = %d, want 1", batchCalls)
	}
	if reconcileCalls != 1 {
		t.Fatalf("reconcileCalls = %d, want 1", reconcileCalls)
	}
}

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

	reconcileCalls := 0
	startBackgroundSchedulers(context.Background(), config.StartupConfig{
		Database: config.DatabaseConfig{SQLiteDSN: "file:test.db"},
		Reconcile: config.ReconcileConfig{
			WorkerEnabled: false,
			PollInterval:  15 * time.Minute,
		},
	}, backgroundSchedulers{
		runBatchImport: func(context.Context, string) {},
		runReconcile: func(context.Context, string, time.Duration) {
			reconcileCalls++
		},
	})

	if reconcileCalls != 0 {
		t.Fatalf("reconcileCalls = %d, want 0", reconcileCalls)
	}
}
