package docs

import (
	"context"
	"encoding/json"
	"testing"
	"time"

	"github.com/google/uuid"
	"github.com/stretchr/testify/require"

	"github.com/yan1h/agent-coding-workflow/internal/jobs"
)

type fakeEnqueuer struct {
	count    int
	lastType jobs.JobType
}

func (f *fakeEnqueuer) Enqueue(_ context.Context, typ jobs.JobType, _ json.RawMessage, _ time.Time, _ int32) (*jobs.Job, error) {
	f.count++
	f.lastType = typ
	return &jobs.Job{ID: uuid.New(), Type: typ}, nil
}

func TestService_EnqueueCommitSummary_Enabled(t *testing.T) {
	enq := &fakeEnqueuer{}
	svc := NewService(enq, true)
	require.NoError(t, svc.EnqueueCommitSummary(context.Background(), uuid.New(), nil, "main", "abc"))
	require.Equal(t, 1, enq.count)
	require.Equal(t, JobTypeCommitSummary, enq.lastType)
}

func TestService_EnqueueCommitSummary_Disabled(t *testing.T) {
	enq := &fakeEnqueuer{}
	svc := NewService(enq, false)
	require.NoError(t, svc.EnqueueCommitSummary(context.Background(), uuid.New(), nil, "main", "abc"))
	require.Equal(t, 0, enq.count, "disabled: no enqueue")
}

func TestService_EnqueueCommitSummary_RequiresSHA(t *testing.T) {
	svc := NewService(&fakeEnqueuer{}, true)
	require.Error(t, svc.EnqueueCommitSummary(context.Background(), uuid.New(), nil, "main", ""))
}
