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 BuildingCreateBodySchema = z
	.object({
		name: z.string().min(1).max(200).openapi({ example: 'Tòa nhà A' }),
		code: z.string().min(1).max(50).openapi({ example: 'TNA-001' }),
		address: z.string().optional().openapi({ example: '123 Đường ABC, Quận 1, TP. Hồ Chí Minh' }),
		description: z.string().optional().openapi({ example: 'Tòa nhà cao cấp 20 tầng' }),
		status: EntityStatusEnum.optional().default('active').openapi({ example: 'active' }),
	})
	.openapi('BuildingCreateBody');

export const BuildingUpdateBodySchema = z
	.object({
		name: z.string().min(1).max(200).optional().openapi({ example: 'Tòa nhà A' }),
		code: z.string().min(1).max(50).optional().openapi({ example: 'TNA-001' }),
		address: z.string().optional().openapi({ example: '123 Đường ABC, Quận 1, TP. Hồ Chí Minh' }),
		description: z.string().optional().openapi({ example: 'Tòa nhà cao cấp 20 tầng' }),
		status: EntityStatusEnum.optional().openapi({ example: 'active' }),
	})
	.openapi('BuildingUpdateBody');

// Response schemas for OpenAPI documentation
export const BuildingSchema = z
	.object({
		id: uuidSchema.openapi({ example: '880e8400-e29b-41d4-a716-446655440001' }),
		name: z.string().openapi({ example: 'Tòa nhà A' }),
		code: z.string().openapi({ example: 'TNA-001' }),
		address: z.string().nullable().openapi({ example: '123 Đường ABC, Quận 1, TP. Hồ Chí Minh' }),
		description: z.string().nullable().openapi({ example: 'Tòa nhà cao cấp 20 tầng' }),
		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('Building');

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

export const BuildingResponseSchema = ApiResponseSchema(BuildingSchema).openapi('BuildingResponse');

export const BuildingListResponseSchema = ApiResponseSchema(BuildingListDataSchema).openapi('BuildingListResponse');
