import { z } from 'zod';
import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	PostCreateBodySchema,
	PostUpdateBodySchema,
	PostResponseSchema,
	PostListResponseSchema,
} from '#contracts/post/schema';
import { ApiResponseSchema, QueryParamsSchema } from '#contracts/shared';

export function registerPostPaths(registry: OpenAPIRegistry): void {
	registry.registerPath({
		method: 'get',
		path: '/api/v1/posts',
		operationId: 'getPosts',
		tags: ['Posts'],
		summary: 'Get posts (role-filtered)',
		security: [{ BearerAuth: [] }],
		request: { query: QueryParamsSchema },
		responses: {
			200: { description: 'OK', content: { 'application/json': { schema: PostListResponseSchema } } },
			401: { description: 'Unauthorized' },
		},
	});

	registry.registerPath({
		method: 'post',
		path: '/api/v1/posts',
		operationId: 'createPost',
		tags: ['Posts'],
		summary: 'Create a post (admin only)',
		security: [{ BearerAuth: [] }],
		request: {
			body: { required: true, content: { 'application/json': { schema: PostCreateBodySchema } } },
		},
		responses: {
			200: { description: 'Created', content: { 'application/json': { schema: PostResponseSchema } } },
			400: { description: 'Bad Request' },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
		},
	});

	registry.registerPath({
		method: 'get',
		path: '/api/v1/posts/{id}',
		operationId: 'getPostById',
		tags: ['Posts'],
		summary: 'Get post detail',
		security: [{ BearerAuth: [] }],
		request: { params: z.object({ id: z.string().uuid() }) },
		responses: {
			200: { description: 'OK', content: { 'application/json': { schema: PostResponseSchema } } },
			401: { description: 'Unauthorized' },
			404: { description: 'Not Found' },
		},
	});

	registry.registerPath({
		method: 'put',
		path: '/api/v1/posts/{id}',
		operationId: 'updatePost',
		tags: ['Posts'],
		summary: 'Update a post (admin only)',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.string().uuid() }),
			body: { content: { 'application/json': { schema: PostUpdateBodySchema } } },
		},
		responses: {
			200: { description: 'Updated', content: { 'application/json': { schema: PostResponseSchema } } },
			400: { description: 'Bad Request' },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
			404: { description: 'Not Found' },
		},
	});

	registry.registerPath({
		method: 'delete',
		path: '/api/v1/posts/{id}',
		operationId: 'deletePost',
		tags: ['Posts'],
		summary: 'Soft-delete a post (admin only)',
		security: [{ BearerAuth: [] }],
		request: { params: z.object({ id: z.string().uuid() }) },
		responses: {
			200: {
				description: 'Deleted',
				content: { 'application/json': { schema: ApiResponseSchema(z.null()) } },
			},
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
			404: { description: 'Not Found' },
		},
	});
}
