import { z } from 'zod';
import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	BillCreateBodySchema,
	BillUpdateBodySchema,
	BillResponseSchema,
	BillListResponseSchema,
} from '#contracts/bill/schema';
import { ApiResponseSchema, QueryParamsSchema } from '#contracts/shared';

export function registerBillPaths(registry: OpenAPIRegistry): void {
	registry.registerPath({
		method: 'get',
		path: '/api/v1/bills',
		operationId: 'getBills',
		tags: ['Bills'],
		summary: 'Get all bills',
		security: [{ BearerAuth: [] }],
		request: {
			query: QueryParamsSchema,
		},
		responses: {
			200: { description: 'OK', content: { 'application/json': { schema: BillListResponseSchema } } },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
		},
	});

	registry.registerPath({
		method: 'post',
		path: '/api/v1/bills',
		operationId: 'createBill',
		tags: ['Bills'],
		summary: 'Create a new bill with items',
		security: [{ BearerAuth: [] }],
		request: {
			body: { required: true, content: { 'application/json': { schema: BillCreateBodySchema } } },
		},
		responses: {
			200: { description: 'Created', content: { 'application/json': { schema: BillResponseSchema } } },
			400: { description: 'Bad Request' },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
		},
	});

	registry.registerPath({
		method: 'get',
		path: '/api/v1/bills/{id}',
		operationId: 'getBillById',
		tags: ['Bills'],
		summary: 'Get bill by ID',
		security: [{ BearerAuth: [] }],
		request: { params: z.object({ id: z.uuid() }) },
		responses: {
			200: { description: 'OK', content: { 'application/json': { schema: BillResponseSchema } } },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
			404: { description: 'Not Found' },
		},
	});

	registry.registerPath({
		method: 'put',
		path: '/api/v1/bills/{id}',
		operationId: 'updateBill',
		tags: ['Bills'],
		summary: 'Update bill',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.uuid() }),
			body: { content: { 'application/json': { schema: BillUpdateBodySchema } } },
		},
		responses: {
			200: { description: 'Updated', content: { 'application/json': { schema: BillResponseSchema } } },
			400: { description: 'Bad Request' },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
			404: { description: 'Not Found' },
		},
	});

	registry.registerPath({
		method: 'delete',
		path: '/api/v1/bills/{id}',
		operationId: 'deleteBill',
		tags: ['Bills'],
		summary: 'Delete bill',
		security: [{ BearerAuth: [] }],
		request: { params: z.object({ id: z.uuid() }) },
		responses: {
			200: {
				description: 'Deleted',
				content: { 'application/json': { schema: ApiResponseSchema(z.null()) } },
			},
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
			404: { description: 'Not Found' },
		},
	});
}
