import { z } from 'zod';
import { type OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import {
	ContractFileCreateBodySchema,
	ContractFileUpdateBodySchema,
	ContractFileResponseSchema,
	ContractFileListResponseSchema,
} from '#contracts/contract-file/schema';
import { ApiResponseSchema, SoftDeleteQueryParamsSchema, ErrorResponseSchema } from '#contracts/shared';

export function registerContractFilePaths(registry: OpenAPIRegistry): void {
	// -------------------------------------------------------------------------
	// GET /api/v1/contract-files
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'get',
		path: '/api/v1/contract-files',
		operationId: 'getContractFiles',
		tags: ['Contract Files'],
		summary: 'Get all contract files',
		description: 'Returns paginated list of contract file attachments. By default, soft-deleted records are excluded.',
		security: [{ BearerAuth: [] }],
		request: {
			query: SoftDeleteQueryParamsSchema,
		},
		responses: {
			200: {
				description: 'OK',
				content: { 'application/json': { schema: ContractFileListResponseSchema } },
			},
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

	// -------------------------------------------------------------------------
	// POST /api/v1/contract-files
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'post',
		path: '/api/v1/contract-files',
		operationId: 'createContractFile',
		tags: ['Contract Files'],
		summary: 'Link file to contract',
		description: 'Link an uploaded file to a contract as an attachment.',
		security: [{ BearerAuth: [] }],
		request: {
			body: {
				required: true,
				content: { 'application/json': { schema: ContractFileCreateBodySchema } },
			},
		},
		responses: {
			201: {
				description: 'File linked to contract successfully',
				content: { 'application/json': { schema: ContractFileResponseSchema } },
			},
			400: { description: 'Bad Request', content: { 'application/json': { schema: ErrorResponseSchema } } },
			401: { description: 'Unauthorized', content: { 'application/json': { schema: ErrorResponseSchema } } },
		},
	});

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

	// -------------------------------------------------------------------------
	// PUT /api/v1/contract-files/{id}
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'put',
		path: '/api/v1/contract-files/{id}',
		operationId: 'updateContractFile',
		tags: ['Contract Files'],
		summary: 'Update contract file',
		description: 'Update contract file metadata. All fields are optional.',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.uuid() }),
			body: {
				required: true,
				content: { 'application/json': { schema: ContractFileUpdateBodySchema } },
			},
		},
		responses: {
			200: {
				description: 'Contract file updated successfully',
				content: { 'application/json': { schema: ContractFileResponseSchema } },
			},
			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/contract-files/{id}
	// -------------------------------------------------------------------------
	registry.registerPath({
		method: 'delete',
		path: '/api/v1/contract-files/{id}',
		operationId: 'deleteContractFile',
		tags: ['Contract Files'],
		summary: 'Delete contract file (soft delete)',
		security: [{ BearerAuth: [] }],
		request: {
			params: z.object({ id: z.uuid() }),
		},
		responses: {
			200: {
				description: 'Contract file 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 } } },
		},
	});
}
