import { z } from 'zod';
import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	BuildingCreateBodySchema,
	BuildingUpdateBodySchema,
	BuildingResponseSchema,
	BuildingListResponseSchema,
} from '#contracts/building/schema';
import { ApiResponseSchema, SoftDeleteQueryParamsSchema } from '#contracts/shared';

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

	registry.registerPath({
		method: 'post',
		path: '/api/v1/buildings',
		operationId: 'createBuilding',
		tags: ['Buildings'],
		summary: 'Create building',
		security: [{ BearerAuth: [] }],
		request: {
			body: { required: true, content: { 'application/json': { schema: BuildingCreateBodySchema } } },
		},
		responses: {
			200: { description: 'Created', content: { 'application/json': { schema: BuildingResponseSchema } } },
			400: { description: 'Bad Request' },
			401: { description: 'Unauthorized' },
			403: { description: 'Forbidden' },
		},
	});

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

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

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