package queue

import (
	"context"
	"testing"

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

func TestFileProcessingJSONSerializerRoundTrip(t *testing.T) {
	serializer := NewFileProcessingJSONSerializer("v1", QueueTypeStandard, "")
	fileUUID, err := uuidv7.String(uuidv7.MustNew())
	if err != nil {
		t.Fatalf("uuid string: %v", err)
	}

	ctx := WithCorrelationID(context.Background(), "upload-req-1")
	message, err := serializer.Serialize(ctx, FileProcessingJob{
		FileUUID:  fileUUID,
		Bucket:    "wucher-file-dev",
		ObjectKey: "files/2026/04/03/a.pdf",
		MimeType:  "application/pdf",
		SizeBytes: 1234,
	})
	if err != nil {
		t.Fatalf("serialize: %v", err)
	}

	envelope, err := serializer.Deserialize(message.Body, message.Attributes)
	if err != nil {
		t.Fatalf("deserialize: %v", err)
	}
	if envelope.MessageID == "" {
		t.Fatalf("expected message id")
	}
	if envelope.CorrelationID != "upload-req-1" {
		t.Fatalf("unexpected correlation id: %q", envelope.CorrelationID)
	}
	if envelope.Payload.FileUUID != fileUUID {
		t.Fatalf("unexpected file uuid: %q", envelope.Payload.FileUUID)
	}
	if envelope.Payload.ObjectKey != "files/2026/04/03/a.pdf" {
		t.Fatalf("unexpected object key: %q", envelope.Payload.ObjectKey)
	}
}

func TestFileProcessingJSONSerializerRejectsInvalidPayload(t *testing.T) {
	serializer := NewFileProcessingJSONSerializer("v1", QueueTypeStandard, "")
	_, err := serializer.Deserialize([]byte(`{"version":"v1","message_id":"m1","kind":"file_manager.processing","occurred_at":"2026-04-03T00:00:00Z","payload":{"bucket":"b","object_key":"k","size_bytes":1}}`), nil)
	if err == nil {
		t.Fatalf("expected invalid payload error")
	}
}
