import { z } from 'zod';
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
import { ApiResponseSchema, uuidSchema } from '../shared';

extendZodWithOpenApi(z);

// ============================================================================
// ContractFile Response Schema
// ============================================================================

export const ContractFileSchema = z
	.object({
		id: uuidSchema.openapi({ description: 'File record ID' }),
		contract_id: uuidSchema.openapi({ description: 'Contract ID' }),
		file_id: uuidSchema.openapi({ description: 'File ID (from files table)' }),
		created_by: uuidSchema
			.nullable()
			.openapi({ description: 'Created by' }),
		updated_by: uuidSchema
			.nullable()
			.openapi({ description: 'Updated by' }),
		created_at: z
			.string()
			.datetime()
			.openapi({ description: 'Created at' }),
		updated_at: z
			.string()
			.datetime()
			.openapi({ description: 'Updated at' }),
		deleted_at: z
			.string()
			.datetime()
			.nullable()
			.openapi({ description: 'Deleted at (soft delete)' }),
	})
	.openapi('ContractFile');

// ============================================================================
// Request Schemas
// ============================================================================

export const ContractFileCreateBodySchema = z
	.object({
		contract_id: uuidSchema.openapi({ description: 'Contract ID' }),
		file_ids: z.array(uuidSchema).min(1).openapi({ description: 'File IDs' }),
	})
	.openapi('ContractFileCreateBody');

export const ContractFileUpdateBodySchema = z
	.object({})
	.openapi('ContractFileUpdateBody');

// ============================================================================
// Response Schemas
// ============================================================================

export const ContractFileResponseSchema = ApiResponseSchema(ContractFileSchema).openapi(
	'ContractFileResponse',
);

export const ContractFileListResponseSchema = ApiResponseSchema(
	z.object({
		items: z.array(ContractFileSchema),
		total: z.number(),
		page: z.number(),
		limit: z.number(),
	}),
).openapi('ContractFileListResponse');

export type ContractFile = z.infer<typeof ContractFileSchema>;
export type ContractFileCreateBody = z.infer<typeof ContractFileCreateBodySchema>;
export type ContractFileUpdateBody = z.infer<typeof ContractFileUpdateBodySchema>;
