package queue

import (
	"context"
	"errors"
	"testing"

	"wucher/internal/config"
	"wucher/internal/shared/pkg/logger"
	"wucher/internal/shared/pkg/uuidv7"
)

type stubFileProcessingHandler struct {
	err error
}

func (h stubFileProcessingHandler) Handle(context.Context, *FileProcessingEnvelope) error {
	return h.err
}

func TestFileProcessingWorkerProcessDeliveryDuplicate(t *testing.T) {
	worker := NewFileProcessingWorker(WorkerConfig{}, nil, NewFileProcessingJSONSerializer("v1", QueueTypeStandard, ""), stubFileProcessingHandler{}, nil, stubIdempotencyStore{state: IdempotencyStateDuplicate}, nil, nil, logger.New(config.LoggingConfig{Format: "json"}))
	fileUUID, _ := uuidv7.String(uuidv7.MustNew())
	message, err := NewFileProcessingJSONSerializer("v1", QueueTypeStandard, "").Serialize(context.Background(), FileProcessingJob{
		FileUUID:  fileUUID,
		Bucket:    "bucket",
		ObjectKey: "files/doc.pdf",
		SizeBytes: 10,
	})
	if err != nil {
		t.Fatalf("serialize: %v", err)
	}

	result := worker.processDelivery(context.Background(), &Delivery{ID: message.ID, Body: message.Body, Attributes: message.Attributes, ReceiveCount: 1})
	if result.decision.Action != RetryActionAck {
		t.Fatalf("expected ack decision, got %s", result.decision.Action)
	}
	if worker.Metrics().Snapshot().Duplicates != 1 {
		t.Fatalf("expected duplicate metric to increment")
	}
}

func TestFileProcessingWorkerProcessDeliveryMalformedPayload(t *testing.T) {
	worker := NewFileProcessingWorker(WorkerConfig{}, nil, NewFileProcessingJSONSerializer("v1", QueueTypeStandard, ""), stubFileProcessingHandler{}, nil, stubIdempotencyStore{}, nil, nil, logger.New(config.LoggingConfig{Format: "json"}))
	result := worker.processDelivery(context.Background(), &Delivery{ID: "msg-1", Body: []byte(`{"bad":true}`), ReceiveCount: 1})
	if result.decision.Action != RetryActionDeadLetter {
		t.Fatalf("expected dead-letter decision, got %s", result.decision.Action)
	}
	if worker.Metrics().Snapshot().Malformed != 1 {
		t.Fatalf("expected malformed metric to increment")
	}
}

func TestFileProcessingWorkerProcessDeliveryTransientError(t *testing.T) {
	worker := NewFileProcessingWorker(WorkerConfig{}, nil, NewFileProcessingJSONSerializer("v1", QueueTypeStandard, ""), stubFileProcessingHandler{err: Transient(errors.New("db timeout"))}, nil, stubIdempotencyStore{}, nil, nil, logger.New(config.LoggingConfig{Format: "json"}))
	fileUUID, _ := uuidv7.String(uuidv7.MustNew())
	message, err := NewFileProcessingJSONSerializer("v1", QueueTypeStandard, "").Serialize(context.Background(), FileProcessingJob{
		FileUUID:  fileUUID,
		Bucket:    "bucket",
		ObjectKey: "files/doc.pdf",
		SizeBytes: 10,
	})
	if err != nil {
		t.Fatalf("serialize: %v", err)
	}

	result := worker.processDelivery(context.Background(), &Delivery{ID: message.ID, Body: message.Body, Attributes: message.Attributes, ReceiveCount: 2})
	if result.decision.Action != RetryActionRetry {
		t.Fatalf("expected retry decision, got %s", result.decision.Action)
	}
	if result.decision.Delay <= 0 {
		t.Fatalf("expected positive retry delay")
	}
}
