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

extendZodWithOpenApi(z);

// Enums
const EntityStatusEnum = z.enum(['active', 'inactive']);

// Request body schemas for validation and OpenAPI documentation
export const ApartmentCreateBodySchema = z
	.object({
		building_id: uuidSchema.openapi({ example: '880e8400-e29b-41d4-a716-446655440001' }),
		name: z.string().min(1).max(200).openapi({ example: 'Căn hộ 101' }),
		code: z.string().min(1).max(50).openapi({ example: 'CH-101' }),
		floor: z.number().int().optional().openapi({ example: 1 }),
		status: EntityStatusEnum.optional().default('active').openapi({ example: 'active' }),
	})
	.openapi('ApartmentCreateBody');

export const ApartmentUpdateBodySchema = z
	.object({
		building_id: uuidSchema.optional().openapi({ example: '880e8400-e29b-41d4-a716-446655440001' }),
		name: z.string().min(1).max(200).optional().openapi({ example: 'Căn hộ 101' }),
		code: z.string().min(1).max(50).optional().openapi({ example: 'CH-101' }),
		floor: z.number().int().optional().openapi({ example: 1 }),
		status: EntityStatusEnum.optional().openapi({ example: 'active' }),
	})
	.openapi('ApartmentUpdateBody');

// Response schemas for OpenAPI documentation
export const ApartmentSchema = z
	.object({
		id: uuidSchema.openapi({ example: '990e8400-e29b-41d4-a716-446655440001' }),
		building_id: uuidSchema.openapi({ example: '880e8400-e29b-41d4-a716-446655440001' }),
		name: z.string().openapi({ example: 'Căn hộ 101' }),
		code: z.string().openapi({ example: 'CH-101' }),
		floor: z.number().nullable().openapi({ example: 1 }),
		status: EntityStatusEnum.openapi({ example: 'active' }),
		created_at: z.iso.datetime().openapi({ example: '2024-01-15T08:30:00Z' }),
		updated_at: z.iso.datetime().nullable().openapi({ example: '2024-01-20T10:00:00Z' }),
		deleted_at: z.iso.datetime().nullable().openapi({ example: null }),
	})
	.openapi('Apartment');

export const ApartmentListDataSchema = z
	.object({
		count: z.number().openapi({ example: 10 }),
		rows: z.array(ApartmentSchema),
		page: z.number().openapi({ example: 1 }),
		pageSize: z.number().openapi({ example: 10 }),
	})
	.openapi('ApartmentListData');

export const ApartmentResponseSchema = ApiResponseSchema(ApartmentSchema).openapi('ApartmentResponse');

export const ApartmentListResponseSchema = ApiResponseSchema(ApartmentListDataSchema).openapi('ApartmentListResponse');
