import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	SystemConfigCreateBodySchema,
	SystemConfigUpdateBodySchema,
	SystemConfigResponseSchema,
	SystemConfigListResponseSchema,
	SystemConfigQueryParamsSchema,
} from '#contracts/system-config/schema';
import { ErrorResponseSchema } from '#contracts/shared';
import { z } from 'zod';

export function registerSystemConfigPaths(registry: OpenAPIRegistry): void {
	// -------------------------------------------------------------------------
	// GET /system-configs
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/system-configs',
		operationId: 'getSystemConfigs',
		tags: ['System Configs'],
		summary: 'Get all system configurations',
		security: [{ BearerAuth: [] }],
		request: { query: SystemConfigQueryParamsSchema },
		responses: {
			200: {
				description: 'OK',
				content: { 'application/json': { schema: SystemConfigListResponseSchema } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
			403: { description: 'Forbidden', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// POST /system-configs
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'post',
		path: '/api/v1/system-configs',
		operationId: 'createSystemConfig',
		tags: ['System Configs'],
		summary: 'Create a new system configuration',
		description: 'Tạo một cấu hình toàn hệ thống mới',
		security: [{ BearerAuth: [] }],
		request: {
			body: {
				required: true,
				content: { 'application/json': { schema: SystemConfigCreateBodySchema } },
			},
		},
		responses: {
			200: {
				description: 'Created',
				content: { 'application/json': { schema: SystemConfigResponseSchema } },
			},
			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 (config_key already exists)', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// GET /system-configs/:id
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/system-configs/{id}',
		operationId: 'getSystemConfigById',
		tags: ['System Configs'],
		summary: 'Get a system configuration by ID',
		security: [{ BearerAuth: [] }],
		request: { params: z.object({ id: z.string().uuid() }) },
		responses: {
			200: {
				description: 'OK',
				content: { 'application/json': { schema: SystemConfigResponseSchema } },
			},
			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 /system-configs/:id
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'put',
		path: '/api/v1/system-configs/{id}',
		operationId: 'updateSystemConfig',
		tags: ['System Configs'],
		summary: 'Update a system configuration',
		description: 'Cập nhật cấu hình toàn hệ thống',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.string().uuid() }),
			body: {
				required: true,
				content: { 'application/json': { schema: SystemConfigUpdateBodySchema } },
			},
		},
		responses: {
			200: {
				description: 'OK',
				content: { 'application/json': { schema: SystemConfigResponseSchema } },
			},
			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 /system-configs/:id
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'delete',
		path: '/api/v1/system-configs/{id}',
		operationId: 'deleteSystemConfig',
		tags: ['System Configs'],
		summary: 'Delete a system configuration',
		description: 'Xóa cấu hình toàn hệ thống',
		security: [{ BearerAuth: [] }],
		request: { params: z.object({ id: z.string().uuid() }) },
		responses: {
			200: {
				description: 'OK',
				content: { 'application/json': { schema: z.object({ success: z.boolean() }) } },
			},
			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 } } },
		},
	});
}
