import type { Service } from '../api-reference.ts';
import {
	CreateWebhookDestinationRequestSchema,
	CreateWebhookRequestSchema,
	UpdateWebhookDestinationRequestSchema,
	UpdateWebhookRequestSchema,
	WebhookReceiptSchema,
	WebhookSchema,
} from './types.ts';

const service: Service = {
	name: 'Webhooks',
	slug: 'webhooks',
	description: 'Manage webhook endpoints, destinations, receipts, deliveries, and analytics',
	endpoints: [
		{
			id: 'create-webhook',
			title: 'Create Webhook',
			sectionTitle: 'Webhook Management',
			method: 'POST',
			path: '/webhook/create',
			description: 'Create a new webhook endpoint.',
			pathParams: [],
			queryParams: [],
			requestBody: {
				description: 'Webhook creation payload.',
				fields: { schema: CreateWebhookRequestSchema },
			},
			responseDescription:
				'Returns the created webhook. The ingest URL is only returned at creation.',
			responseFields: { schema: WebhookSchema },
			statuses: [
				{ code: 201, description: 'Webhook created' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
			],
			examplePath: '/webhook/create',
			exampleBody: { name: 'Payment Events' },
		},
		{
			id: 'get-webhook',
			title: 'Get Webhook',
			sectionTitle: 'Webhook Management',
			method: 'GET',
			path: '/webhook/get/{webhookId}',
			description: 'Get a specific webhook by ID.',
			pathParams: [
				{
					name: 'webhookId',
					type: 'string',
					description: 'Webhook ID (wh_ prefix)',
					required: true,
				},
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Returns the webhook object.',
			statuses: [
				{ code: 200, description: 'Webhook returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook not found' },
			],
			examplePath: '/webhook/get/wh_abc123',
		},
		{
			id: 'list-webhooks',
			title: 'List Webhooks',
			sectionTitle: 'Webhook Management',
			method: 'GET',
			path: '/webhook/list',
			description: 'List all webhooks with optional pagination.',
			pathParams: [],
			queryParams: [
				{
					name: 'limit',
					type: 'number',
					description: 'Maximum results to return',
					required: false,
				},
				{ name: 'offset', type: 'number', description: 'Pagination offset', required: false },
			],
			requestBody: null,
			responseDescription: 'Returns paginated list of webhooks.',
			statuses: [
				{ code: 200, description: 'Webhooks returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
			],
			examplePath: '/webhook/list',
		},
		{
			id: 'update-webhook',
			title: 'Update Webhook',
			sectionTitle: 'Webhook Management',
			method: 'PUT',
			path: '/webhook/update/{webhookId}',
			description: "Update a webhook's name or description.",
			pathParams: [
				{ name: 'webhookId', type: 'string', description: 'Webhook ID', required: true },
			],
			queryParams: [],
			requestBody: {
				description: 'Webhook update payload.',
				fields: { schema: UpdateWebhookRequestSchema },
			},
			responseDescription: 'Returns the updated webhook.',
			statuses: [
				{ code: 200, description: 'Webhook updated' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook not found' },
			],
			examplePath: '/webhook/update/wh_abc123',
			exampleBody: { name: 'Updated Webhook' },
		},
		{
			id: 'delete-webhook',
			title: 'Delete Webhook',
			sectionTitle: 'Webhook Management',
			method: 'DELETE',
			path: '/webhook/delete/{webhookId}',
			description: 'Delete a webhook and all associated destinations, receipts, and deliveries.',
			pathParams: [
				{ name: 'webhookId', type: 'string', description: 'Webhook ID', required: true },
			],
			queryParams: [],
			requestBody: null,
			responseDescription:
				'Deletes the webhook and all associated destinations, receipts, and deliveries.',
			statuses: [
				{ code: 204, description: 'Webhook deleted' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook not found' },
			],
			examplePath: '/webhook/delete/wh_abc123',
		},
		{
			id: 'create-webhook-destination',
			title: 'Create Destination',
			sectionTitle: 'Destinations',
			method: 'POST',
			path: '/webhook/destination-create/{webhookId}',
			description: 'Add a destination to a webhook.',
			pathParams: [
				{ name: 'webhookId', type: 'string', description: 'Webhook ID', required: true },
			],
			queryParams: [],
			requestBody: {
				description: 'Destination creation payload.',
				fields: { schema: CreateWebhookDestinationRequestSchema },
			},
			responseDescription: 'Returns the created destination.',
			statuses: [
				{ code: 201, description: 'Destination created' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook not found' },
			],
			examplePath: '/webhook/destination-create/wh_abc123',
			exampleBody: { type: 'url', config: { url: 'https://example.com/handler' } },
		},
		{
			id: 'list-webhook-destinations',
			title: 'List Destinations',
			sectionTitle: 'Destinations',
			method: 'GET',
			path: '/webhook/destination-list/{webhookId}',
			description: 'List all destinations for a webhook.',
			pathParams: [
				{ name: 'webhookId', type: 'string', description: 'Webhook ID', required: true },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Returns list of destinations.',
			statuses: [
				{ code: 200, description: 'Destinations returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook not found' },
			],
			examplePath: '/webhook/destination-list/wh_abc123',
		},
		{
			id: 'update-webhook-destination',
			title: 'Update Destination',
			sectionTitle: 'Destinations',
			method: 'PUT',
			path: '/webhook/destination-update/{webhookId}/{destinationId}',
			description: "Update a destination's configuration.",
			pathParams: [
				{ name: 'webhookId', type: 'string', description: 'Webhook ID', required: true },
				{
					name: 'destinationId',
					type: 'string',
					description: 'Destination ID (whds_ prefix)',
					required: true,
				},
			],
			queryParams: [],
			requestBody: {
				description: 'Destination update payload.',
				fields: { schema: UpdateWebhookDestinationRequestSchema },
			},
			responseDescription: 'Returns the updated destination.',
			statuses: [
				{ code: 200, description: 'Destination updated' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook or destination not found' },
			],
			examplePath: '/webhook/destination-update/wh_abc123/whds_def456',
			exampleBody: { config: { url: 'https://example.com/new-handler' } },
		},
		{
			id: 'delete-webhook-destination',
			title: 'Delete Destination',
			sectionTitle: 'Destinations',
			method: 'DELETE',
			path: '/webhook/destination-delete/{webhookId}/{destinationId}',
			description: 'Delete a destination from a webhook.',
			pathParams: [
				{ name: 'webhookId', type: 'string', description: 'Webhook ID', required: true },
				{
					name: 'destinationId',
					type: 'string',
					description: 'Destination ID',
					required: true,
				},
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 204, description: 'Destination deleted' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook or destination not found' },
			],
			examplePath: '/webhook/destination-delete/wh_abc123/whds_def456',
		},
		{
			id: 'list-webhook-receipts',
			title: 'List Receipts',
			sectionTitle: 'Receipts',
			method: 'GET',
			path: '/webhook/receipt-list/{webhookId}',
			description: 'List incoming request records (receipts) for a webhook.',
			pathParams: [
				{ name: 'webhookId', type: 'string', description: 'Webhook ID', required: true },
			],
			queryParams: [
				{
					name: 'limit',
					type: 'number',
					description: 'Maximum results to return',
					required: false,
				},
				{ name: 'offset', type: 'number', description: 'Pagination offset', required: false },
			],
			requestBody: null,
			responseDescription: 'Returns incoming request records (receipts) for a webhook.',
			statuses: [
				{ code: 200, description: 'Receipts returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook not found' },
			],
			examplePath: '/webhook/receipt-list/wh_abc123',
		},
		{
			id: 'get-webhook-receipt',
			title: 'Get Receipt',
			sectionTitle: 'Receipts',
			method: 'GET',
			path: '/webhook/receipt-get/{webhookId}/{receiptId}',
			description: 'Get a specific receipt by ID.',
			pathParams: [
				{ name: 'webhookId', type: 'string', description: 'Webhook ID', required: true },
				{
					name: 'receiptId',
					type: 'string',
					description: 'Receipt ID (whrc_ prefix)',
					required: true,
				},
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Returns the receipt with headers and payload.',
			responseFields: { schema: WebhookReceiptSchema },
			statuses: [
				{ code: 200, description: 'Receipt returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook or receipt not found' },
			],
			examplePath: '/webhook/receipt-get/wh_abc123/whrc_def456',
		},
		{
			id: 'list-webhook-deliveries',
			title: 'List Deliveries',
			sectionTitle: 'Deliveries',
			method: 'GET',
			path: '/webhook/delivery-list/{webhookId}',
			description: 'List delivery attempts for a webhook.',
			pathParams: [
				{ name: 'webhookId', type: 'string', description: 'Webhook ID', required: true },
			],
			queryParams: [
				{
					name: 'limit',
					type: 'number',
					description: 'Maximum results to return',
					required: false,
				},
				{ name: 'offset', type: 'number', description: 'Pagination offset', required: false },
			],
			requestBody: null,
			responseDescription: 'Returns list of delivery attempts.',
			statuses: [
				{ code: 200, description: 'Deliveries returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook not found' },
			],
			examplePath: '/webhook/delivery-list/wh_abc123',
		},
		{
			id: 'retry-webhook-delivery',
			title: 'Retry Delivery',
			sectionTitle: 'Deliveries',
			method: 'POST',
			path: '/webhook/delivery-retry/{webhookId}/{deliveryId}',
			description:
				"Retry a failed delivery. Only deliveries with 'failed' status can be retried.",
			pathParams: [
				{ name: 'webhookId', type: 'string', description: 'Webhook ID', required: true },
				{
					name: 'deliveryId',
					type: 'string',
					description: 'Delivery ID (whdv_ prefix)',
					required: true,
				},
			],
			queryParams: [],
			requestBody: null,
			responseDescription:
				"Retries a failed delivery. Only deliveries with 'failed' status can be retried.",
			statuses: [
				{ code: 200, description: 'Delivery retried' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
				{ code: 404, description: 'Webhook or delivery not found' },
			],
			examplePath: '/webhook/delivery-retry/wh_abc123/whdv_def456',
			exampleBody: {},
		},
		{
			id: 'get-webhook-org-analytics',
			title: 'Get Org Analytics',
			sectionTitle: 'Analytics',
			method: 'GET',
			path: '/webhook/analytics/org',
			description: 'Get aggregate webhook analytics for the organization.',
			pathParams: [],
			queryParams: [
				{ name: 'start', type: 'string', description: 'ISO 8601 start date', required: false },
				{ name: 'end', type: 'string', description: 'ISO 8601 end date', required: false },
				{
					name: 'granularity',
					type: 'string',
					description: "'minute', 'hour', or 'day'",
					required: false,
				},
			],
			requestBody: null,
			responseDescription:
				'Returns aggregate analytics including total received, delivered, and failed counts.',
			statuses: [
				{ code: 200, description: 'Analytics returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
			],
			examplePath: '/webhook/analytics/org',
		},
		{
			id: 'get-webhook-org-timeseries',
			title: 'Get Analytics Time Series',
			sectionTitle: 'Analytics',
			method: 'GET',
			path: '/webhook/analytics/org/timeseries',
			description: 'Get time series webhook analytics for the organization.',
			pathParams: [],
			queryParams: [
				{ name: 'start', type: 'string', description: 'ISO 8601 start date', required: false },
				{ name: 'end', type: 'string', description: 'ISO 8601 end date', required: false },
				{
					name: 'granularity',
					type: 'string',
					description: "'minute', 'hour', or 'day'",
					required: false,
				},
			],
			requestBody: null,
			responseDescription:
				'Returns time series data with received, delivered, and failed counts per time bucket.',
			statuses: [
				{ code: 200, description: 'Time series data returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing Bearer token' },
			],
			examplePath: '/webhook/analytics/org/timeseries',
		},
	],
};

export default service;
