import {
	BatchPublishMessagesRequestSchema,
	BatchPublishResponseSchema,
	ConsumeMessagesResponseSchema,
	CreateDestinationRequestSchema,
	CreateQueueRequestSchema,
	GetQueueHeadResponseSchema,
	GetQueueTailResponseSchema,
	ListDeliveryLogsResponseSchema,
	ListDestinationsResponseSchema,
	ListDlqMessagesResponseSchema,
	ListMessagesResponseSchema,
	ListQueuesResponseSchema,
	PublishMessageRequestSchema,
	ReceiveMessageResponseSchema,
	UpdateQueueRequestSchema,
} from './types.ts';
import type { Service } from '../api-reference.ts';

const service: Service = {
	name: 'Message Queues',
	slug: 'queues',
	description: 'Publish, consume, and manage messages with worker and pub/sub queues',
	endpoints: [
		{
			id: 'create-queue',
			title: 'Create Queue',
			sectionTitle: 'Queue Management',
			method: 'POST',
			path: '/queue/create',
			description: 'Create a queue. If `name` is omitted, a name is auto-generated.',
			pathParams: [],
			queryParams: [],
			requestBody: {
				description: 'Queue creation payload.',
				fields: { schema: CreateQueueRequestSchema },
			},
			responseDescription: 'Queue object including settings, stats, and timestamps.',
			statuses: [
				{ code: 200, description: 'Queue created' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 402, description: 'Payment required' },
			],
			examplePath: '/queue/create',
			exampleBody: {
				name: 'order-processing',
				queue_type: 'worker',
				description: 'Processes customer orders',
				settings: { default_max_retries: 3, default_visibility_timeout_seconds: 60 },
			},
		},
		{
			id: 'get-queue',
			title: 'Get Queue',
			sectionTitle: 'Queue Management',
			method: 'GET',
			path: '/queue/get/{name}',
			description: 'Get a queue by name.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Queue object.',
			statuses: [
				{ code: 200, description: 'Queue returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/get/order-processing',
		},
		{
			id: 'list-queues',
			title: 'List Queues',
			sectionTitle: 'Queue Management',
			method: 'GET',
			path: '/queue/list',
			description: 'List queues with filtering and pagination.',
			pathParams: [],
			queryParams: [
				{
					name: 'limit',
					type: 'number',
					description: 'Maximum queues to return',
					required: false,
				},
				{
					name: 'offset',
					type: 'number',
					description: 'Offset for pagination',
					required: false,
				},
				{ name: 'name', type: 'string', description: 'Filter by queue name', required: false },
				{
					name: 'queue_type',
					type: 'string',
					description: '`worker` or `pubsub`',
					required: false,
				},
				{
					name: 'status',
					type: 'string',
					description: '`active` or `paused`',
					required: false,
				},
				{ name: 'sort', type: 'string', description: 'Sort field', required: false },
				{ name: 'direction', type: 'string', description: '`asc` or `desc`', required: false },
			],
			requestBody: null,
			responseDescription: 'Response with queue array and optional total count.',
			responseFields: { schema: ListQueuesResponseSchema },
			statuses: [
				{ code: 200, description: 'Queues returned' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/queue/list?limit=10&queue_type=worker',
		},
		{
			id: 'update-queue',
			title: 'Update Queue',
			sectionTitle: 'Queue Management',
			method: 'PATCH',
			path: '/queue/update/{name}',
			description: 'Partially update queue description/settings.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: {
				description: 'Partial queue update payload.',
				fields: { schema: UpdateQueueRequestSchema },
			},
			responseDescription: 'Updated queue object.',
			statuses: [
				{ code: 200, description: 'Queue updated' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/update/order-processing',
			exampleBody: { description: 'Updated', settings: { default_max_retries: 5 } },
		},
		{
			id: 'delete-queue',
			title: 'Delete Queue',
			sectionTitle: 'Queue Management',
			method: 'DELETE',
			path: '/queue/delete/{name}',
			description: 'Delete a queue and associated resources.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Queue deleted' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/delete/order-processing',
		},
		{
			id: 'pause-queue',
			title: 'Pause Queue',
			sectionTitle: 'Queue Management',
			method: 'POST',
			path: '/queue/pause/{name}',
			description: 'Pause queue processing.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: { description: 'Empty JSON object is required.' },
			responseDescription: 'Queue object with `paused_at` set.',
			statuses: [
				{ code: 200, description: 'Queue paused' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/pause/order-processing',
			exampleBody: {},
		},
		{
			id: 'resume-queue',
			title: 'Resume Queue',
			sectionTitle: 'Queue Management',
			method: 'POST',
			path: '/queue/resume/{name}',
			description: 'Resume queue processing.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: { description: 'Empty JSON object is required.' },
			responseDescription: 'Queue object with `paused_at` cleared.',
			statuses: [
				{ code: 200, description: 'Queue resumed' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/resume/order-processing',
			exampleBody: {},
		},
		{
			id: 'publish-message',
			title: 'Publish Message',
			sectionTitle: 'Message Operations',
			method: 'POST',
			path: '/queue/messages/publish/{name}',
			description: 'Publish a single message to a queue.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [
				{
					name: 'sync',
					type: 'boolean',
					description: 'Set `true` for synchronous publish',
					required: false,
				},
			],
			requestBody: {
				description: 'Message payload and optional delivery controls.',
				fields: { schema: PublishMessageRequestSchema },
			},
			responseDescription: 'Published message object.',
			statuses: [
				{ code: 200, description: 'Message published' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/messages/publish/order-processing',
			exampleBody: {
				payload: { orderId: 123, action: 'process' },
				metadata: { priority: 'high' },
			},
		},
		{
			id: 'batch-publish',
			title: 'Batch Publish',
			sectionTitle: 'Message Operations',
			method: 'POST',
			path: '/queue/messages/batch/{name}',
			description: 'Publish up to 1000 messages in one request.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: {
				description: 'Batch publish payload.',
				fields: { schema: BatchPublishMessagesRequestSchema },
			},
			responseDescription:
				'Batch publish response with created messages and optional failed indexes.',
			responseFields: { schema: BatchPublishResponseSchema },
			statuses: [
				{ code: 200, description: 'Batch publish completed' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/messages/batch/order-processing',
			exampleBody: { messages: [{ payload: { orderId: 1 } }, { payload: { orderId: 2 } }] },
		},
		{
			id: 'get-message',
			title: 'Get Message',
			sectionTitle: 'Message Operations',
			method: 'GET',
			path: '/queue/messages/get/{name}/{messageId}',
			description: 'Get a message by message ID (`msg_...`).',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'messageId', type: 'string', description: 'Message ID (`msg_...`)' },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Message object.',
			statuses: [
				{ code: 200, description: 'Message returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Message or queue not found' },
			],
			examplePath: '/queue/messages/get/order-processing/msg_abc123',
		},
		{
			id: 'get-message-by-offset',
			title: 'Get Message by Offset',
			sectionTitle: 'Message Operations',
			method: 'GET',
			path: '/queue/messages/offset/{name}/{offset}',
			description: 'Get a message by numeric offset.',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'offset', type: 'number', description: 'Message offset' },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Message object.',
			statuses: [
				{ code: 200, description: 'Message returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Message or queue not found' },
			],
			examplePath: '/queue/messages/offset/order-processing/42',
		},
		{
			id: 'list-messages',
			title: 'List Messages',
			sectionTitle: 'Message Operations',
			method: 'GET',
			path: '/queue/messages/list/{name}',
			description: 'List messages with pagination and optional state filter.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [
				{
					name: 'limit',
					type: 'number',
					description: 'Maximum messages to return',
					required: false,
				},
				{
					name: 'offset',
					type: 'number',
					description: 'Offset for pagination',
					required: false,
				},
				{
					name: 'state',
					type: 'string',
					description: '`pending`, `processing`, `completed`, `failed`, or `dead`',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Response with message list and optional total count.',
			responseFields: { schema: ListMessagesResponseSchema },
			statuses: [
				{ code: 200, description: 'Messages returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/messages/list/order-processing?limit=20&state=pending',
		},
		{
			id: 'delete-message',
			title: 'Delete Message',
			sectionTitle: 'Message Operations',
			method: 'DELETE',
			path: '/queue/messages/delete/{name}/{messageId}',
			description: 'Delete a message by ID.',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'messageId', type: 'string', description: 'Message ID' },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Message deleted' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Message or queue not found' },
			],
			examplePath: '/queue/messages/delete/order-processing/msg_abc123',
		},
		{
			id: 'replay-message',
			title: 'Replay Message',
			sectionTitle: 'Message Operations',
			method: 'POST',
			path: '/queue/messages/replay/{name}/{messageId}',
			description: 'Replay a message back into the queue flow.',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'messageId', type: 'string', description: 'Message ID' },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Replayed message object.',
			statuses: [
				{ code: 200, description: 'Message replayed' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Message or queue not found' },
			],
			examplePath: '/queue/messages/replay/order-processing/msg_abc123',
		},
		{
			id: 'consume-messages',
			title: 'Consume Messages',
			sectionTitle: 'Message Operations',
			method: 'GET',
			path: '/queue/consume/{name}',
			description:
				'Log-style consumption starting from offset. Does not mark messages as processing.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [
				{ name: 'offset', type: 'number', description: 'Starting offset', required: true },
				{
					name: 'limit',
					type: 'number',
					description: 'Maximum messages to return',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Response containing consumed messages.',
			responseFields: { schema: ConsumeMessagesResponseSchema },
			statuses: [
				{ code: 200, description: 'Messages consumed' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/consume/order-processing?offset=0&limit=10',
		},
		{
			id: 'receive-message',
			title: 'Receive Message',
			sectionTitle: 'Message Operations',
			method: 'GET',
			path: '/queue/receive/{name}',
			description:
				'Atomically receive and lock the next pending message. Must ack/nack when done.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [
				{
					name: 'timeout',
					type: 'number',
					description: 'Long-poll timeout in seconds (0–30)',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Response containing a message or `null` when none available.',
			responseFields: { schema: ReceiveMessageResponseSchema },
			statuses: [
				{ code: 200, description: 'Receive operation completed' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/receive/order-processing?timeout=10',
		},
		{
			id: 'ack-message',
			title: 'Acknowledge Message',
			sectionTitle: 'Message Operations',
			method: 'POST',
			path: '/queue/ack/{name}/{messageId}',
			description: 'Mark message as successfully processed (completed).',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'messageId', type: 'string', description: 'Message ID' },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Message acknowledged' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Message or queue not found' },
			],
			examplePath: '/queue/ack/order-processing/msg_abc123',
		},
		{
			id: 'nack-message',
			title: 'Negative Acknowledge',
			sectionTitle: 'Message Operations',
			method: 'POST',
			path: '/queue/nack/{name}/{messageId}',
			description: 'Return message for retry. After max retries, moves to DLQ.',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'messageId', type: 'string', description: 'Message ID' },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Message negatively acknowledged' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Message or queue not found' },
			],
			examplePath: '/queue/nack/order-processing/msg_abc123',
		},
		{
			id: 'get-queue-head',
			title: 'Get Queue Head',
			sectionTitle: 'Message Operations',
			method: 'GET',
			path: '/queue/head/{name}',
			description: 'Get offset of the oldest message.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'JSON object containing the head offset.',
			responseFields: { schema: GetQueueHeadResponseSchema },
			statuses: [
				{ code: 200, description: 'Queue head returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/head/order-processing',
		},
		{
			id: 'get-queue-tail',
			title: 'Get Queue Tail',
			sectionTitle: 'Message Operations',
			method: 'GET',
			path: '/queue/tail/{name}',
			description: 'Get offset of the newest message.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'JSON object containing the tail offset.',
			responseFields: { schema: GetQueueTailResponseSchema },
			statuses: [
				{ code: 200, description: 'Queue tail returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/tail/order-processing',
		},
		{
			id: 'create-destination',
			title: 'Create Destination',
			sectionTitle: 'Destinations',
			method: 'POST',
			path: '/queue/destinations/create/{name}',
			description: 'Create an HTTP destination for queue deliveries.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: {
				description: 'Destination creation payload.',
				fields: { schema: CreateDestinationRequestSchema },
			},
			responseDescription: 'Created destination object.',
			statuses: [
				{ code: 200, description: 'Destination created' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/destinations/create/order-processing',
			exampleBody: {
				name: 'my-webhook',
				destination_type: 'http',
				config: { url: 'https://example.com/webhook' },
				enabled: true,
			},
		},
		{
			id: 'list-destinations',
			title: 'List Destinations',
			sectionTitle: 'Destinations',
			method: 'GET',
			path: '/queue/destinations/list/{name}',
			description: 'List queue destinations.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Response containing destination list.',
			responseFields: { schema: ListDestinationsResponseSchema },
			statuses: [
				{ code: 200, description: 'Destinations returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/destinations/list/order-processing',
		},
		{
			id: 'update-destination',
			title: 'Update Destination',
			sectionTitle: 'Destinations',
			method: 'PATCH',
			path: '/queue/destinations/update/{name}/{destinationId}',
			description: 'Partially update destination fields.',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'destinationId', type: 'string', description: 'Destination ID' },
			],
			queryParams: [],
			requestBody: {
				description: 'Partial destination update payload.',
			},
			responseDescription: 'Updated destination object.',
			statuses: [
				{ code: 200, description: 'Destination updated' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue or destination not found' },
			],
			examplePath: '/queue/destinations/update/order-processing/dst_abc123',
			exampleBody: { timeout_seconds: 20 },
		},
		{
			id: 'delete-destination',
			title: 'Delete Destination',
			sectionTitle: 'Destinations',
			method: 'DELETE',
			path: '/queue/destinations/delete/{name}/{destinationId}',
			description: 'Delete a queue destination.',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'destinationId', type: 'string', description: 'Destination ID' },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Destination deleted' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue or destination not found' },
			],
			examplePath: '/queue/destinations/delete/order-processing/dst_abc123',
		},
		{
			id: 'list-delivery-logs',
			title: 'List Delivery Logs',
			sectionTitle: 'Destinations',
			method: 'GET',
			path: '/queue/destinations/deliveries/{name}/{destinationId}',
			description: 'List destination delivery logs with optional status filter.',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'destinationId', type: 'string', description: 'Destination ID' },
			],
			queryParams: [
				{
					name: 'limit',
					type: 'number',
					description: 'Maximum logs to return',
					required: false,
				},
				{
					name: 'offset',
					type: 'number',
					description: 'Offset for pagination',
					required: false,
				},
				{
					name: 'status',
					type: 'string',
					description: 'Delivery status filter',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Response containing delivery log entries.',
			responseFields: { schema: ListDeliveryLogsResponseSchema },
			statuses: [
				{ code: 200, description: 'Delivery logs returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue or destination not found' },
			],
			examplePath: '/queue/destinations/deliveries/order-processing/dst_abc123?limit=20',
		},
		{
			id: 'list-dlq-messages',
			title: 'List DLQ Messages',
			sectionTitle: 'Dead Letter Queue',
			method: 'GET',
			path: '/queue/dlq/list/{name}',
			description: 'List dead-letter queue messages.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [
				{
					name: 'limit',
					type: 'number',
					description: 'Maximum messages to return',
					required: false,
				},
				{
					name: 'offset',
					type: 'number',
					description: 'Offset for pagination',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Response containing DLQ messages and optional total.',
			responseFields: { schema: ListDlqMessagesResponseSchema },
			statuses: [
				{ code: 200, description: 'DLQ messages returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/dlq/list/order-processing?limit=20',
		},
		{
			id: 'replay-dlq-message',
			title: 'Replay DLQ Message',
			sectionTitle: 'Dead Letter Queue',
			method: 'POST',
			path: '/queue/dlq/replay/{name}/{messageId}',
			description: 'Replay a DLQ message back to the queue.',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'messageId', type: 'string', description: 'DLQ message ID' },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Replayed message object.',
			statuses: [
				{ code: 200, description: 'DLQ message replayed' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue or message not found' },
			],
			examplePath: '/queue/dlq/replay/order-processing/msg_abc123',
		},
		{
			id: 'purge-dlq',
			title: 'Purge DLQ',
			sectionTitle: 'Dead Letter Queue',
			method: 'DELETE',
			path: '/queue/dlq/purge/{name}',
			description: 'Delete all messages in the dead-letter queue.',
			pathParams: [{ name: 'name', type: 'string', description: 'Queue name' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'DLQ purged' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue not found' },
			],
			examplePath: '/queue/dlq/purge/order-processing',
		},
		{
			id: 'delete-dlq-message',
			title: 'Delete DLQ Message',
			sectionTitle: 'Dead Letter Queue',
			method: 'DELETE',
			path: '/queue/dlq/delete/{name}/{messageId}',
			description: 'Delete a specific message from the dead-letter queue.',
			pathParams: [
				{ name: 'name', type: 'string', description: 'Queue name' },
				{ name: 'messageId', type: 'string', description: 'DLQ message ID' },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'DLQ message deleted' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Queue or message not found' },
			],
			examplePath: '/queue/dlq/delete/order-processing/msg_abc123',
		},
	],
};

export default service;
