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 RoomCreateBodySchema = z
	.object({
		apartment_id: uuidSchema.openapi({ example: '990e8400-e29b-41d4-a716-446655440001' }),
		name: z.string().min(1).max(200).openapi({ example: 'Phòng 101A' }),
		code: z.string().min(1).max(50).openapi({ example: 'P-101A' }),
		floor: z.number().int().optional().openapi({ example: 1 }),
		status: EntityStatusEnum.optional().default('active').openapi({ example: 'active' }),
	})
	.openapi('RoomCreateBody');

export const RoomUpdateBodySchema = z
	.object({
		apartment_id: uuidSchema.optional().openapi({ example: '990e8400-e29b-41d4-a716-446655440001' }),
		name: z.string().min(1).max(200).optional().openapi({ example: 'Phòng 101A' }),
		code: z.string().min(1).max(50).optional().openapi({ example: 'P-101A' }),
		floor: z.number().int().optional().openapi({ example: 1 }),
		status: EntityStatusEnum.optional().openapi({ example: 'active' }),
	})
	.openapi('RoomUpdateBody');

// Response schemas for OpenAPI documentation
export const RoomSchema = z
	.object({
		id: uuidSchema.openapi({ example: 'aa0e8400-e29b-41d4-a716-446655440001' }),
		apartment_id: uuidSchema.openapi({ example: '990e8400-e29b-41d4-a716-446655440001' }),
		name: z.string().openapi({ example: 'Phòng 101A' }),
		code: z.string().openapi({ example: 'P-101A' }),
		floor: z.number().int().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('Room');

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

export const RoomResponseSchema = ApiResponseSchema(RoomSchema).openapi('RoomResponse');

export const RoomListResponseSchema = ApiResponseSchema(RoomListDataSchema).openapi('RoomListResponse');
