import {
	EmailActivityResultSchema,
	EmailConnectionConfigSchema,
	EmailSendParamsSchema,
} from './service.ts';
import { CreateAddressRequestSchema, CreateEmailDestinationRequestSchema } from './types.ts';
import type { Service } from '../api-reference.ts';

const service: Service = {
	name: 'Emails',
	slug: 'email',
	description: 'Send and receive emails with managed addresses and webhook destinations',
	endpoints: [
		{
			id: 'create-address',
			title: 'Create Address',
			sectionTitle: 'Address Management',
			method: 'POST',
			path: '/email/addresses',
			description: 'Create a managed address at the `@agentuity.email` domain.',
			pathParams: [],
			queryParams: [],
			requestBody: {
				description: 'Address creation payload.',
				fields: { schema: CreateAddressRequestSchema },
			},
			responseDescription: 'Created `EmailAddress` object.',
			statuses: [
				{ code: 200, description: 'Address created' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/addresses',
			exampleBody: { local_part: 'support' },
		},
		{
			id: 'list-addresses',
			title: 'List Addresses',
			sectionTitle: 'Address Management',
			method: 'GET',
			path: '/email/addresses',
			description: 'List managed email addresses.',
			pathParams: [],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Array of `EmailAddress` objects.',
			statuses: [
				{ code: 200, description: 'Addresses returned' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/addresses',
		},
		{
			id: 'get-address',
			title: 'Get Address',
			sectionTitle: 'Address Management',
			method: 'GET',
			path: '/email/addresses/{id}',
			description: 'Get a managed address by ID (`eaddr_...`).',
			pathParams: [{ name: 'id', type: 'string', description: 'Address ID (`eaddr_...`)' }],
			queryParams: [],
			requestBody: null,
			responseDescription: '`EmailAddress` object.',
			statuses: [
				{ code: 200, description: 'Address returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Address not found' },
			],
			examplePath: '/email/addresses/eaddr_abc123',
		},
		{
			id: 'get-connection-config',
			title: 'Get Connection Config',
			sectionTitle: 'Address Management',
			method: 'GET',
			path: '/email/addresses/{id}/connection',
			description: 'Get IMAP/POP3 connection settings and credentials for an address.',
			pathParams: [{ name: 'id', type: 'string', description: 'Address ID' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Connection configuration including IMAP and POP3 details.',
			responseFields: { schema: EmailConnectionConfigSchema },
			statuses: [
				{ code: 200, description: 'Connection config returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Address not found' },
			],
			examplePath: '/email/addresses/eaddr_abc123/connection',
		},
		{
			id: 'delete-address',
			title: 'Delete Address',
			sectionTitle: 'Address Management',
			method: 'DELETE',
			path: '/email/addresses/{id}',
			description: 'Delete a managed email address.',
			pathParams: [{ name: 'id', type: 'string', description: 'Address ID' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Address deleted' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/addresses/eaddr_abc123',
		},
		{
			id: 'create-email-destination',
			title: 'Create Destination',
			sectionTitle: 'Email Destinations',
			method: 'POST',
			path: '/email/addresses/{addressId}/destinations',
			description: 'Create an inbound webhook destination for an address.',
			pathParams: [{ name: 'addressId', type: 'string', description: 'Address ID' }],
			queryParams: [],
			requestBody: {
				description: 'Destination creation payload.',
				fields: { schema: CreateEmailDestinationRequestSchema },
			},
			responseDescription: 'Created `EmailDestination` object.',
			statuses: [
				{ code: 200, description: 'Destination created' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/addresses/eaddr_abc123/destinations',
			exampleBody: {
				type: 'url',
				config: { url: 'https://example.com/email-hook', method: 'POST' },
			},
		},
		{
			id: 'list-email-destinations',
			title: 'List Destinations',
			sectionTitle: 'Email Destinations',
			method: 'GET',
			path: '/email/addresses/{addressId}/destinations',
			description: 'List inbound destinations for an address.',
			pathParams: [{ name: 'addressId', type: 'string', description: 'Address ID' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Array of `EmailDestination` objects.',
			statuses: [
				{ code: 200, description: 'Destinations returned' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/addresses/eaddr_abc123/destinations',
		},
		{
			id: 'delete-email-destination',
			title: 'Delete Destination',
			sectionTitle: 'Email Destinations',
			method: 'DELETE',
			path: '/email/addresses/{addressId}/destinations/{destinationId}',
			description: 'Delete an inbound destination.',
			pathParams: [
				{ name: 'addressId', type: 'string', description: 'Address ID' },
				{ 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' },
			],
			examplePath: '/email/addresses/eaddr_abc123/destinations/edst_abc123',
		},
		{
			id: 'send-email',
			title: 'Send Email',
			sectionTitle: 'Sending Email',
			method: 'POST',
			path: '/email/outbound/send',
			description:
				'Send an email from an owned address. Delivery is asynchronous and total payload (including attachments) is capped at 25MB.',
			pathParams: [],
			queryParams: [],
			requestBody: {
				description: 'Outbound email payload.',
				fields: { schema: EmailSendParamsSchema },
			},
			responseDescription: 'Created `EmailOutbound` object with initial `pending` status.',
			statuses: [
				{ code: 200, description: 'Outbound email accepted' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/outbound/send',
			exampleBody: {
				from: 'support@agentuity.email',
				to: ['user@example.com'],
				subject: 'Welcome!',
				text: 'Thanks for joining Agentuity.',
			},
		},
		{
			id: 'list-inbound',
			title: 'List Inbound',
			sectionTitle: 'Inbound Email',
			method: 'GET',
			path: '/email/inbound',
			description: 'List inbound emails, optionally filtered by address.',
			pathParams: [],
			queryParams: [
				{
					name: 'address_id',
					type: 'string',
					description: 'Filter by address ID',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Array of `EmailInbound` objects.',
			statuses: [
				{ code: 200, description: 'Inbound emails returned' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/inbound?address_id=eaddr_abc123',
		},
		{
			id: 'get-inbound',
			title: 'Get Inbound',
			sectionTitle: 'Inbound Email',
			method: 'GET',
			path: '/email/inbound/{id}',
			description: 'Get a specific inbound email by ID (`einb_...`).',
			pathParams: [{ name: 'id', type: 'string', description: 'Inbound email ID (`einb_...`)' }],
			queryParams: [],
			requestBody: null,
			responseDescription: '`EmailInbound` object.',
			statuses: [
				{ code: 200, description: 'Inbound email returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Inbound email not found' },
			],
			examplePath: '/email/inbound/einb_abc123',
		},
		{
			id: 'delete-inbound',
			title: 'Delete Inbound',
			sectionTitle: 'Inbound Email',
			method: 'DELETE',
			path: '/email/inbound/{id}',
			description: 'Delete an inbound email record.',
			pathParams: [{ name: 'id', type: 'string', description: 'Inbound email ID' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Inbound email deleted' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/inbound/einb_abc123',
		},
		{
			id: 'list-outbound',
			title: 'List Outbound',
			sectionTitle: 'Outbound Email',
			method: 'GET',
			path: '/email/outbound',
			description: 'List outbound emails, optionally filtered by address.',
			pathParams: [],
			queryParams: [
				{
					name: 'address_id',
					type: 'string',
					description: 'Filter by address ID',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Array of `EmailOutbound` objects.',
			statuses: [
				{ code: 200, description: 'Outbound emails returned' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/outbound?address_id=eaddr_abc123',
		},
		{
			id: 'get-outbound',
			title: 'Get Outbound',
			sectionTitle: 'Outbound Email',
			method: 'GET',
			path: '/email/outbound/{id}',
			description: 'Get an outbound email by ID (`eout_...`).',
			pathParams: [
				{ name: 'id', type: 'string', description: 'Outbound email ID (`eout_...`)' },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: '`EmailOutbound` object including delivery status and error details.',
			statuses: [
				{ code: 200, description: 'Outbound email returned' },
				{ code: 401, description: 'Unauthorized' },
				{ code: 404, description: 'Outbound email not found' },
			],
			examplePath: '/email/outbound/eout_abc123',
		},
		{
			id: 'delete-outbound',
			title: 'Delete Outbound',
			sectionTitle: 'Outbound Email',
			method: 'DELETE',
			path: '/email/outbound/{id}',
			description: 'Delete an outbound email record.',
			pathParams: [{ name: 'id', type: 'string', description: 'Outbound email ID' }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 200, description: 'Outbound email deleted' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/outbound/eout_abc123',
		},
		{
			id: 'get-email-activity',
			title: 'Get Activity',
			sectionTitle: 'Activity',
			method: 'GET',
			path: '/email/activity/{date}',
			description: 'Get daily inbound/outbound activity over a date window.',
			pathParams: [
				{
					name: 'date',
					type: 'string',
					description: 'Date for activity lookup (YYYY-MM-DD format)',
					required: true,
				},
			],
			queryParams: [
				{
					name: 'days',
					type: 'number',
					description: 'Range in days (7–365, default: 7)',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Activity time series grouped by date.',
			responseFields: { schema: EmailActivityResultSchema },
			statuses: [
				{ code: 200, description: 'Activity returned' },
				{ code: 401, description: 'Unauthorized' },
			],
			examplePath: '/email/activity/2026-02-28?days=30',
		},
	],
};

export default service;
