import { z } from 'zod';
import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	BedCreateBodySchema,
	BedUpdateBodySchema,
	BedResponseSchema,
	BedListResponseSchema,
} from '#contracts/bed/schema';
import { ApiResponseSchema, SoftDeleteQueryParamsSchema } from '#contracts/shared';

export function registerBedPaths(registry: OpenAPIRegistry): void {
	registry.registerPath({
		method: 'get',
		path: '/api/v1/beds',
		operationId: 'getBeds',
		tags: ['Beds'],
		summary: 'Get all beds',
		description:
			'Returns list of beds. By default, soft-deleted records are excluded (deleted_at==null). To include soft-deleted records, omit the filters parameter or use filters=deleted_at!=null.',
		security: [{ BearerAuth: [] }],
		request: {
			query: SoftDeleteQueryParamsSchema,
		},
		responses: {
			200: { description: 'OK', content: { 'application/json': { schema: BedListResponseSchema } } },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
		},
	});

	registry.registerPath({
		method: 'post',
		path: '/api/v1/beds',
		operationId: 'createBed',
		tags: ['Beds'],
		summary: 'Create bed',
		security: [{ BearerAuth: [] }],
		request: {
			body: { required: true, content: { 'application/json': { schema: BedCreateBodySchema } } },
		},
		responses: {
			200: { description: 'Created', content: { 'application/json': { schema: BedResponseSchema } } },
			400: { description: 'Bad Request' },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
		},
	});

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

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

	registry.registerPath({
		method: 'delete',
		path: '/api/v1/beds/{id}',
		operationId: 'deleteBed',
		tags: ['Beds'],
		summary: 'Delete bed',
		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' },
		},
	});

	registry.registerPath({
		method: 'put',
		path: '/api/v1/beds/{id}/restore',
		operationId: 'restoreBed',
		tags: ['Beds'],
		summary: 'Restore a soft-deleted bed',
		description:
			'Restores a soft-deleted bed by setting deleted_at = null. Use force=true to bypass unique constraint checks.',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.uuid() }),
			query: z.object({
				force: z.coerce.boolean().optional().openapi({
					description: 'Bypass unique constraint checks (default: false)',
				}),
			}),
		},
		responses: {
			200: {
				description: 'Bed restored successfully',
				content: { 'application/json': { schema: ApiResponseSchema(z.string()) } },
			},
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
			404: { description: 'Not Found' },
			409: { description: 'Conflict - restore would violate unique constraints' },
		},
	});

	registry.registerPath({
		method: 'delete',
		path: '/api/v1/beds/{id}/force',
		operationId: 'forceDeleteBed',
		tags: ['Beds'],
		summary: 'Permanently delete a bed',
		description: 'Permanently deletes a bed from the database. Cannot be undone.',
		security: [{ BearerAuth: [] }],
		request: { params: z.object({ id: z.uuid() }) },
		responses: {
			200: {
				description: 'Bed permanently deleted',
				content: { 'application/json': { schema: ApiResponseSchema(z.string()) } },
			},
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
			404: { description: 'Not Found' },
		},
	});
}
