import {
	CreateStreamApiRequestSchema,
	CreateStreamApiResponseSchema,
	ListStreamsApiRequestSchema,
	ListStreamsApiResponseSchema,
	StreamInfoApiSchema,
} from './types.ts';
import type { Service } from '../api-reference.ts';

const service: Service = {
	name: 'Durable Streams',
	slug: 'streams',
	description: 'Create durable, resumable data streams with public URLs',
	host: 'streams',
	endpoints: [
		{
			id: 'create-stream',
			title: 'Create Stream',
			method: 'POST',
			path: '/stream',
			description: 'Create a new stream and receive its stream ID.',
			pathParams: [],
			queryParams: [],
			requestBody: {
				description: 'Stream creation payload.',
				fields: { schema: CreateStreamApiRequestSchema },
			},
			responseDescription: 'JSON response containing the new stream ID.',
			responseFields: { schema: CreateStreamApiResponseSchema, stripRequired: true },
			statuses: [
				{ code: 200, description: 'Stream created successfully' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 402, description: 'Payment required' },
			],
			examplePath: '/stream',
			exampleBody: {
				name: 'agent-logs',
				metadata: { exportDate: '2024-01-15' },
				headers: { 'content-type': 'application/json' },
			},
			ttlNote:
				'TTL behavior:\n- **Omitted**: Stream expires after the service default period\n- **`null` or `0`**: Stream never expires\n- **60–7,776,000**: Expires after specified seconds (values outside range are clamped)',
		},
		{
			id: 'get-stream-info',
			title: 'Get Stream Info',
			method: 'POST',
			path: '/stream/{id}/info',
			description: 'Get stream metadata, size, public URL, and expiration info.',
			pathParams: [{ name: 'id', type: 'string', description: 'The stream ID' }],
			queryParams: [],
			requestBody: {
				description: 'Empty JSON object is required.',
			},
			responseDescription: 'JSON object with stream details.',
			responseFields: { schema: StreamInfoApiSchema, stripRequired: true },
			statuses: [
				{ code: 200, description: 'Stream info returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Stream not found' },
			],
			examplePath: '/stream/stream_abc123/info',
			exampleBody: {},
		},
		{
			id: 'download-stream',
			title: 'Download Stream',
			method: 'GET',
			path: '/stream/{id}',
			description: 'Download the finalized stream contents as raw binary data.',
			pathParams: [{ name: 'id', type: 'string', description: 'The stream ID' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Raw binary stream data with original content type.',
			statuses: [
				{ code: 200, description: 'Stream data returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Stream not found' },
			],
			examplePath: '/stream/stream_abc123',
		},
		{
			id: 'list-streams',
			title: 'List Streams',
			method: 'POST',
			path: '/stream/list',
			description: 'List streams with filtering, pagination, and sorting.',
			pathParams: [],
			queryParams: [],
			requestBody: {
				description: 'Optional list filters and pagination controls.',
				fields: { schema: ListStreamsApiRequestSchema },
			},
			responseDescription: 'JSON response with stream list and total count.',
			responseFields: { schema: ListStreamsApiResponseSchema, stripRequired: true },
			statuses: [
				{ code: 200, description: 'Stream list returned' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/stream/list',
			exampleBody: { name: 'agent-logs', limit: 50 },
		},
		{
			id: 'delete-stream',
			title: 'Delete Stream',
			method: 'DELETE',
			path: '/stream/{id}',
			description: 'Delete a stream by ID.',
			pathParams: [{ name: 'id', type: 'string', description: 'The stream ID' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Stream deleted' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Stream not found' },
			],
			examplePath: '/stream/stream_abc123',
		},
		{
			id: 'append-data',
			title: 'Append Data',
			method: 'POST',
			path: '/stream/{id}/append',
			description: 'Append a binary chunk (up to 5MB) to an open stream.',
			pathParams: [{ name: 'id', type: 'string', description: 'The stream ID' }],
			queryParams: [],
			requestBody: {
				description: 'Raw binary body. Set `Content-Type: application/octet-stream`.',
			},
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Data appended' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 413, description: 'Chunk too large' },
			],
			examplePath: '/stream/stream_abc123/append',
			exampleHeaders: { 'Content-Type': 'application/octet-stream' },
			exampleBody: '<binary data>',
		},
		{
			id: 'complete-stream',
			title: 'Complete Stream',
			method: 'POST',
			path: '/stream/{id}/complete',
			description: 'Finalize stream writing and make it available for reading.',
			pathParams: [{ name: 'id', type: 'string', description: 'The stream ID' }],
			queryParams: [],
			requestBody: {
				description:
					'Empty body. Optional header: `X-Compress: gzip` for server-side compression.',
			},
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Stream completed' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/stream/stream_abc123/complete',
		},
	],
};

export default service;
