import { z } from 'zod';
import { BedStatus, RateBasis } from '@prisma/client';

export const createRoomSchema = z.object({
  propertyId: z.string().uuid(),
  number: z.string().min(1).max(20),
  floor: z.coerce.number().int().min(0).max(200).optional(),
  type: z.string().max(40).optional(),
  notes: z.string().max(500).optional(),
  // Public room creation must produce usable inventory. Internal import code
  // may stage an empty room briefly before adding its explicitly named bed.
  bedCount: z.coerce.number().int().min(1, 'Add at least one bed').max(50),
  /** How those beds are named: A, B, C… (the default) or 1, 2, 3…. */
  bedNaming: z.enum(['LETTERS', 'NUMBERS']).optional(),
  // Rent for the room as a unit — used when the property prices by ROOM.
  monthlyRent: z.coerce.number().int().min(0).max(1_000_000).optional(),
  rentDueDay: z.coerce.number().int().min(1).max(28).optional(),
  // How many months one rent invoice covers (1 monthly, 3 quarterly, 6, 12).
  feeCycleMonths: z.coerce.number().int().min(1).max(12).optional(),
  // Rent applied to every bed created here — used when pricing is by BED, so
  // the builder can set rent at creation instead of a second pass per bed.
  bedRent: z.coerce.number().int().min(0).max(1_000_000).optional(),
  // Dormitories: charge per night instead of per month. nightlyRent is the
  // room's rate (ROOM pricing); bedNightlyRent is applied to each bed (BED pricing).
  rateBasis: z.nativeEnum(RateBasis).optional(),
  nightlyRent: z.coerce.number().int().min(0).max(100_000).optional(),
  bedNightlyRent: z.coerce.number().int().min(0).max(100_000).optional(),
});

export const updateRoomSchema = z.object({
  number: z.string().min(1).max(20).optional(),
  floor: z.coerce.number().int().min(0).max(200).nullable().optional(),
  type: z.string().max(40).nullable().optional(),
  notes: z.string().max(500).nullable().optional(),
  monthlyRent: z.coerce.number().int().min(0).max(1_000_000).nullable().optional(),
  rentDueDay: z.coerce.number().int().min(1).max(28).nullable().optional(),
  feeCycleMonths: z.coerce.number().int().min(1).max(12).optional(),
  rateBasis: z.nativeEnum(RateBasis).optional(),
  nightlyRent: z.coerce.number().int().min(0).max(100_000).nullable().optional(),
  /// Who is invoiced for this room under ROOM pricing. null clears it.
  billedStudentProfileId: z.string().uuid().nullable().optional(),
});

/** A bed's name: what the desk calls it — "A", "3", "Window", "Upper-2". */
const bedLabel = z.string().trim().min(1, 'Give the bed a name').max(12, 'Keep bed names to 12 characters');

export const addBedSchema = z.object({
  roomId: z.string().uuid(),
  /** Leave out to get the room's next name (27 Oct 2026; older clients always send one). */
  label: bedLabel.optional(),
  /** Several beds at once, named in a row; ignored when a label is given. */
  count: z.coerce.number().int().min(1).max(10).optional(),
  /** Mark them as extra beds, such as mattresses put in for a group. */
  extra: z.boolean().optional(),
});

export const renameBedSchema = z.object({ label: bedLabel });

export const setBedRentSchema = z.object({
  monthlyRent: z.coerce.number().int().min(0).max(1_000_000).nullable(),
  rentDueDay: z.coerce.number().int().min(1).max(28).nullable().optional(),
  nightlyRent: z.coerce.number().int().min(0).max(100_000).nullable().optional(),
});

// Manual status change is limited to VACANT/RESERVED; OCCUPIED is set by
// assigning a student, not by hand.
export const updateBedStatusSchema = z.object({
  status: z.enum([BedStatus.VACANT, BedStatus.RESERVED]),
});

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