import { z } from 'zod';
import { FLOOR_TYPES, WALL_FINISHES } from '../types/room.js';

const floorTypeEnum = z.enum(FLOOR_TYPES);
const wallFinishEnum = z.enum(WALL_FINISHES);

const pointSchema = z.object({
  x: z.number(),
  y: z.number(),
});

export const createRoomSchema = z.object({
  name: z.string().min(1, 'Name is required').max(255),
  shape: z.array(pointSchema).optional(),
  width: z.number().positive('Width must be positive').nullish(),
  height: z.number().positive('Height must be positive').nullish(),
  wallHeight: z.number().positive('Wall height must be positive').optional(),
  plinthHeight: z.number().min(0, 'Plinth height must be non-negative').optional(),
  plinthThickness: z.number().min(0, 'Plinth thickness must be non-negative').optional(),
  stretchCeilingOffset: z
    .number()
    .min(0, 'Stretch ceiling offset must be non-negative')
    .optional(),
  order: z.number().int().min(0).optional(),
  posX: z.number().optional(),
  posY: z.number().optional(),
  floorType: floorTypeEnum.optional(),
  wallColor: z.string().max(20).optional(),
  wallFinish: wallFinishEnum.optional(),
  outletWidth: z.number().positive('Outlet width must be positive').optional(),
  outletHeight: z.number().positive('Outlet height must be positive').optional(),
});

export const updateRoomSchema = z.object({
  name: z.string().min(1, 'Name is required').max(255).optional(),
  shape: z.array(pointSchema).optional(),
  width: z.number().positive('Width must be positive').nullish(),
  height: z.number().positive('Height must be positive').nullish(),
  wallHeight: z.number().positive('Wall height must be positive').optional(),
  plinthHeight: z.number().min(0, 'Plinth height must be non-negative').optional(),
  plinthThickness: z.number().min(0, 'Plinth thickness must be non-negative').optional(),
  stretchCeilingOffset: z
    .number()
    .min(0, 'Stretch ceiling offset must be non-negative')
    .optional(),
  order: z.number().int().min(0).optional(),
  posX: z.number().optional(),
  posY: z.number().optional(),
  floorType: floorTypeEnum.optional(),
  wallColor: z.string().max(20).optional(),
  wallFinish: wallFinishEnum.optional(),
  outletWidth: z.number().positive('Outlet width must be positive').optional(),
  outletHeight: z.number().positive('Outlet height must be positive').optional(),
});

export type CreateRoomInput = z.infer<typeof createRoomSchema>;
export type UpdateRoomInput = z.infer<typeof updateRoomSchema>;
