import { z } from 'zod';
import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	ApartmentCreateBodySchema,
	ApartmentUpdateBodySchema,
	ApartmentResponseSchema,
	ApartmentListResponseSchema,
} from '#contracts/apartment/schema';
import { ApiResponseSchema, SoftDeleteQueryParamsSchema } from '#contracts/shared';

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

	registry.registerPath({
		method: 'post',
		path: '/api/v1/apartments',
		operationId: 'createApartment',
		tags: ['Apartments'],
		summary: 'Create apartment',
		security: [{ BearerAuth: [] }],
		request: {
			body: { required: true, content: { 'application/json': { schema: ApartmentCreateBodySchema } } },
		},
		responses: {
			200: { description: 'Created', content: { 'application/json': { schema: ApartmentResponseSchema } } },
			400: { description: 'Bad Request' },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
		},
	});

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

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

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