import { z } from 'zod';
import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	NotificationMutateBodySchema,
	NotificationResponseSchema,
	NotificationListResponseSchema,
	UnreadCountResponseSchema,
	NotificationPreferenceSchema,
	NotificationPreferenceUpdateSchema,
	NotificationDeviceSchema,
	NotificationDeviceRegisterSchema,
} from '#contracts/notification/schema';
import { ApiResponseSchema, ErrorResponseSchema, QueryParamsSchema } from '#contracts/shared';

export function registerNotificationPaths(registry: OpenAPIRegistry): void {
	// -------------------------------------------------------------------------
	// GET /notifications
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/notifications',
		operationId: 'getNotifications',
		tags: ['Notifications'],
		summary: 'Get notifications of current user',
		description: 'Returns paginated notifications for the authenticated user, including unread count.',
		security: [{ BearerAuth: [] }],
		request: {
			query: QueryParamsSchema,
		},
		responses: {
			200: {
				description: 'Notifications retrieved successfully',
				content: { 'application/json': { schema: NotificationListResponseSchema } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// POST /notifications
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'post',
		path: '/api/v1/notifications',
		operationId: 'createNotification',
		tags: ['Notifications'],
		summary: 'Create and send notification',
		security: [{ BearerAuth: [] }],
		request: {
			body: {
				required: true,
				content: { 'application/json': { schema: NotificationMutateBodySchema } },
			},
		},
		responses: {
			200: {
				description: 'Notification created and sent successfully',
				content: { 'application/json': { schema: NotificationResponseSchema } },
			},
			400: { description: 'Bad Request', content: { 'application/json': { schema: ErrorResponseSchema } } },
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// GET /notifications/{id}
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/notifications/{id}',
		operationId: 'getNotificationById',
		tags: ['Notifications'],
		summary: 'Get notification by ID',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.string() }),
		},
		responses: {
			200: {
				description: 'Notification retrieved successfully',
				content: { 'application/json': { schema: NotificationResponseSchema } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
			403: { description: 'Forbidden', content: { 'application/json': { schema: ErrorResponseSchema } } },
			404: { description: 'Not Found', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// DELETE /notifications/{id}
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'delete',
		path: '/api/v1/notifications/{id}',
		operationId: 'deleteNotification',
		tags: ['Notifications'],
		summary: 'Delete notification',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.string() }),
		},
		responses: {
			200: {
				description: 'Notification deleted successfully',
				content: { 'application/json': { schema: ApiResponseSchema(z.null()) } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
			403: { description: 'Forbidden', content: { 'application/json': { schema: ErrorResponseSchema } } },
			404: { description: 'Not Found', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// GET /notifications/unread-count
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/notifications/unread-count',
		operationId: 'getUnreadNotificationCount',
		tags: ['Notifications'],
		summary: 'Get unread notification count',
		description: 'Get the number of unread notifications for the current user',
		security: [{ BearerAuth: [] }],
		responses: {
			200: {
				description: 'Unread count retrieved successfully',
				content: { 'application/json': { schema: UnreadCountResponseSchema } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// GET /notifications/subscribe
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/notifications/subscribe',
		operationId: 'subscribeToNotifications',
		tags: ['Notifications'],
		summary: 'Subscribe to notifications via SSE',
		description: 'Subscribe to real-time notifications using Server-Sent Events',
		security: [{ BearerAuth: [] }],
		responses: {
			200: {
				description: 'SSE connection established',
				content: { 'text/event-stream': { schema: z.string() } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// GET /notifications/preferences
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/notifications/preferences',
		operationId: 'getNotificationPreferences',
		tags: ['Notifications'],
		summary: 'Get notification preferences',
		description: 'Get notification preferences for the current user',
		security: [{ BearerAuth: [] }],
		responses: {
			200: {
				description: 'Preferences retrieved successfully',
				content: { 'application/json': { schema: ApiResponseSchema(z.array(NotificationPreferenceSchema)) } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// PUT /notifications/preferences
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'put',
		path: '/api/v1/notifications/preferences',
		operationId: 'updateNotificationPreferences',
		tags: ['Notifications'],
		summary: 'Update notification preferences',
		description: 'Update notification preferences for the current user',
		security: [{ BearerAuth: [] }],
		request: {
			body: {
				required: true,
				content: { 'application/json': { schema: NotificationPreferenceUpdateSchema } },
			},
		},
		responses: {
			200: {
				description: 'Preferences updated successfully',
				content: { 'application/json': { schema: ApiResponseSchema(z.array(NotificationPreferenceSchema)) } },
			},
			400: { description: 'Bad Request', content: { 'application/json': { schema: ErrorResponseSchema } } },
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// PUT /notifications/mark-as-read
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'put',
		path: '/api/v1/notifications/mark-as-read',
		operationId: 'markNotificationsAsRead',
		tags: ['Notifications'],
		summary: 'Mark notifications as read',
		security: [{ BearerAuth: [] }],
		request: {
			query: QueryParamsSchema,
		},
		responses: {
			200: {
				description: 'Marked notifications as read successfully',
				content: { 'application/json': { schema: ApiResponseSchema(z.unknown()) } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// GET /notifications/devices
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/notifications/devices',
		operationId: 'getNotificationDevices',
		tags: ['Notifications'],
		summary: 'Get user notification devices',
		description: 'Get list of active notification devices for the current user',
		security: [{ BearerAuth: [] }],
		responses: {
			200: {
				description: 'Devices retrieved successfully',
				content: { 'application/json': { schema: ApiResponseSchema(z.array(NotificationDeviceSchema)) } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// POST /notifications/devices
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'post',
		path: '/api/v1/notifications/devices',
		operationId: 'registerNotificationDevice',
		tags: ['Notifications'],
		summary: 'Register notification device',
		description: 'Register a new device for push notifications',
		security: [{ BearerAuth: [] }],
		request: {
			body: {
				required: true,
				content: { 'application/json': { schema: NotificationDeviceRegisterSchema } },
			},
		},
		responses: {
			200: {
				description: 'Device registered successfully',
				content: { 'application/json': { schema: ApiResponseSchema(NotificationDeviceSchema) } },
			},
			400: { description: 'Bad Request', content: { 'application/json': { schema: ErrorResponseSchema } } },
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// DELETE /notifications/devices/{id}
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'delete',
		path: '/api/v1/notifications/devices/{id}',
		operationId: 'unregisterNotificationDevice',
		tags: ['Notifications'],
		summary: 'Remove notification device',
		description: 'Deactivate a notification device for the current user',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.string() }),
		},
		responses: {
			200: {
				description: 'Device unregistered successfully',
				content: { 'application/json': { schema: ApiResponseSchema(z.null()) } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
			403: { description: 'Forbidden', content: { 'application/json': { schema: ErrorResponseSchema } } },
			404: { description: 'Not Found', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});
}
