import { z } from 'zod';
import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	ContractCreateBodySchema,
	ContractUpdateBodySchema,
	ContractResponseSchema,
	ContractListResponseSchema,
} from '#contracts/contract/schema';
import { ApiResponseSchema, SoftDeleteQueryParamsSchema, ErrorResponseSchema } from '#contracts/shared';

export function registerContractPaths(registry: OpenAPIRegistry): void {
	// -------------------------------------------------------------------------
	// GET /api/v1/contracts
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/contracts',
		operationId: 'getContracts',
		tags: ['Contracts'],
		summary: 'Get all contracts',
		description:
			'Returns paginated list of contracts. By default, soft-deleted records are excluded (deleted_at==null).',
		security: [{ BearerAuth: [] }],
		request: {
			query: SoftDeleteQueryParamsSchema,
		},
		responses: {
			200: {
				description: 'OK',
				content: { 'application/json': { schema: ContractListResponseSchema } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// POST /api/v1/contracts
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'post',
		path: '/api/v1/contracts',
		operationId: 'createContract',
		tags: ['Contracts'],
		summary: 'Create a new contract',
		description: 'Create a contract for a resident. Requires resident_id and contract_number.',
		security: [{ BearerAuth: [] }],
		request: {
			body: {
				required: true,
				content: { 'application/json': { schema: ContractCreateBodySchema } },
			},
		},
		responses: {
			201: {
				description: 'Contract created successfully',
				content: { 'application/json': { schema: ContractResponseSchema } },
			},
			400: { description: 'Bad Request', content: { 'application/json': { schema: ErrorResponseSchema } } },
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// GET /api/v1/contracts/{id}
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/contracts/{id}',
		operationId: 'getContractById',
		tags: ['Contracts'],
		summary: 'Get contract by ID',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.uuid() }),
		},
		responses: {
			200: {
				description: 'OK',
				content: { 'application/json': { schema: ContractResponseSchema } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
			404: { description: 'Not Found', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// PUT /api/v1/contracts/{id}
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'put',
		path: '/api/v1/contracts/{id}',
		operationId: 'updateContract',
		tags: ['Contracts'],
		summary: 'Update contract',
		description: 'Update contract details. All fields are optional.',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.uuid() }),
			body: {
				required: true,
				content: { 'application/json': { schema: ContractUpdateBodySchema } },
			},
		},
		responses: {
			200: {
				description: 'Contract updated successfully',
				content: { 'application/json': { schema: ContractResponseSchema } },
			},
			400: { description: 'Bad Request', content: { 'application/json': { schema: ErrorResponseSchema } } },
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
			404: { description: 'Not Found', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// DELETE /api/v1/contracts/{id}
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'delete',
		path: '/api/v1/contracts/{id}',
		operationId: 'deleteContract',
		tags: ['Contracts'],
		summary: 'Delete contract (soft delete)',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.uuid() }),
		},
		responses: {
			200: {
				description: 'Contract deleted successfully',
				content: { 'application/json': { schema: ApiResponseSchema(z.boolean()) } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
			404: { description: 'Not Found', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});
}
