import { z } from 'zod';
import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	RoomCreateBodySchema,
	RoomUpdateBodySchema,
	RoomResponseSchema,
	RoomListResponseSchema,
} from '#contracts/room/schema';
import { ApiResponseSchema, SoftDeleteQueryParamsSchema } from '#contracts/shared';

export function registerRoomPaths(registry: OpenAPIRegistry): void {
	registry.registerPath({
		method: 'get',
		path: '/api/v1/rooms',
		operationId: 'getRooms',
		tags: ['Rooms'],
		summary: 'Get all rooms',
		description:
			'Returns list of rooms. 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: RoomListResponseSchema } } },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
		},
	});

	registry.registerPath({
		method: 'post',
		path: '/api/v1/rooms',
		operationId: 'createRoom',
		tags: ['Rooms'],
		summary: 'Create room',
		security: [{ BearerAuth: [] }],
		request: {
			body: { required: true, content: { 'application/json': { schema: RoomCreateBodySchema } } },
		},
		responses: {
			200: { description: 'Created', content: { 'application/json': { schema: RoomResponseSchema } } },
			400: { description: 'Bad Request' },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
		},
	});

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

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

	registry.registerPath({
		method: 'delete',
		path: '/api/v1/rooms/{id}',
		operationId: 'deleteRoom',
		tags: ['Rooms'],
		summary: 'Delete room',
		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/rooms/{id}/restore',
		operationId: 'restoreRoom',
		tags: ['Rooms'],
		summary: 'Restore a soft-deleted room',
		description:
			'Restores a soft-deleted room 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: 'Room 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/rooms/{id}/force',
		operationId: 'forceDeleteRoom',
		tags: ['Rooms'],
		summary: 'Permanently delete a room',
		description: 'Permanently deletes a room from the database. Cannot be undone.',
		security: [{ BearerAuth: [] }],
		request: { params: z.object({ id: z.uuid() }) },
		responses: {
			200: {
				description: 'Room permanently deleted',
				content: { 'application/json': { schema: ApiResponseSchema(z.string()) } },
			},
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
			404: { description: 'Not Found' },
		},
	});
}
