import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	NotificationTemplateCreateBodySchema,
	NotificationTemplateUpdateBodySchema,
	NotificationTemplateResponseSchema,
	NotificationTemplateListResponseSchema,
} from '#contracts/notification-template/schema';
import { ApiResponseSchema, ErrorResponseSchema, QueryParamsSchema } from '#contracts/shared';
import { z } from 'zod';

export function registerNotificationTemplatePaths(registry: OpenAPIRegistry): void {
	// -------------------------------------------------------------------------
	// GET /notification-templates
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/notification-templates',
		operationId: 'getNotificationTemplates',
		tags: ['Notification Templates'],
		summary: 'Get all notification templates',
		security: [{ BearerAuth: [] }],
		request: { query: QueryParamsSchema },
		responses: {
			200: {
				description: 'OK',
				content: { 'application/json': { schema: NotificationTemplateListResponseSchema } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
			403: { description: 'Forbidden', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// POST /notification-templates
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'post',
		path: '/api/v1/notification-templates',
		operationId: 'createNotificationTemplate',
		tags: ['Notification Templates'],
		summary: 'Create a new notification template',
		description: 'Trường `code` là tuỳ chọn. Nếu không truyền, hệ thống sẽ tự sinh từ `name`.',
		security: [{ BearerAuth: [] }],
		request: {
			body: {
				required: true,
				content: { 'application/json': { schema: NotificationTemplateCreateBodySchema } },
			},
		},
		responses: {
			200: {
				description: 'Created',
				content: { 'application/json': { schema: NotificationTemplateResponseSchema } },
			},
			400: { description: 'Bad Request', content: { 'application/json': { schema: ErrorResponseSchema } } },
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
			403: { description: 'Forbidden', content: { 'application/json': { schema: ErrorResponseSchema } } },
			409: { description: 'Conflict (code already exists)', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// GET /notification-templates/:id
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/notification-templates/{id}',
		operationId: 'getNotificationTemplateById',
		tags: ['Notification Templates'],
		summary: 'Get a notification template by ID',
		security: [{ BearerAuth: [] }],
		request: { params: z.object({ id: z.string().uuid() }) },
		responses: {
			200: {
				description: 'OK',
				content: { 'application/json': { schema: NotificationTemplateResponseSchema } },
			},
			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 } } },
		},
	});

	// -------------------------------------------------------------------------
	// PUT /notification-templates/:id
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'put',
		path: '/api/v1/notification-templates/{id}',
		operationId: 'updateNotificationTemplate',
		tags: ['Notification Templates'],
		summary: 'Update a notification template',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.string().uuid() }),
			body: {
				required: true,
				content: { 'application/json': { schema: NotificationTemplateUpdateBodySchema } },
			},
		},
		responses: {
			200: {
				description: 'Updated',
				content: { 'application/json': { schema: NotificationTemplateResponseSchema } },
			},
			400: { description: 'Bad Request', content: { 'application/json': { schema: ErrorResponseSchema } } },
			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 /notification-templates/:id
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'delete',
		path: '/api/v1/notification-templates/{id}',
		operationId: 'deleteNotificationTemplate',
		tags: ['Notification Templates'],
		summary: 'Delete a notification template',
		security: [{ BearerAuth: [] }],
		request: { params: z.object({ id: z.string().uuid() }) },
		responses: {
			200: {
				description: 'Deleted',
				content: { 'application/json': { schema: ApiResponseSchema(z.object({ message: z.string() })) } },
			},
			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 } } },
		},
	});
}
