import { z } from 'zod';
import { PricingMode } from '@prisma/client';
import { INDIAN_STATES } from '../../lib/india';

/**
 * The details that describe a property — shared by two callers: a hostel admin
 * adding a property, and a super admin creating a tenant's first one. Keeping
 * one shape means the two forms cannot drift apart.
 *
 * Everything but the name is optional. A hostel that only knows its name today
 * should still be able to start, and fill in the rest later.
 */
export const propertyDetailsSchema = z.object({
  city: z.string().min(2).max(80).optional(),
  // Constrained to the canonical list; the city it contains is not (see india.ts).
  state: z.enum(INDIAN_STATES).optional(),
  address: z.string().max(300).optional(),
  /// S3 key from the presign flow, not a URL the client invented.
  logoUrl: z.string().max(500).optional(),
  floors: z.coerce.number().int().min(1).max(200).optional(),
  /// How many rooms the building has. Guidance for setup, not a limit — an
  /// admin who typed 20 and builds 22 has updated their plans, not broken a rule.
  plannedRooms: z.coerce.number().int().min(1).max(5000).optional(),
  /// How this property charges: per bed (default) or per room as a unit.
  pricingMode: z.nativeEnum(PricingMode).optional(),
  /// Move-out policy (Sprint 4): notice a student must give, and days after the last day the refund is promised by.
  noticeDays: z.coerce.number().int().min(0).max(180).optional(),
  refundDays: z.coerce.number().int().min(0).max(90).optional(),
  /// Gate (Sprint 5).
  guardianApprovesPasses: z.boolean().optional(),
  curfewTime: z.string().regex(/^([01]\d|2[0-3]):[0-5]\d$/, 'curfewTime must be HH:MM').optional(),
  /// Dormitory: standard check-in and check-out times and the early / late charges.
  checkInTime: z.string().regex(/^([01]\d|2[0-3]):[0-5]\d$/, 'checkInTime must be HH:MM').optional(),
  checkOutTime: z.string().regex(/^([01]\d|2[0-3]):[0-5]\d$/, 'checkOutTime must be HH:MM').optional(),
  earlyCheckInCharge: z.coerce.number().int().min(0).max(100_000).optional(),
  lateCheckOutCharge: z.coerce.number().int().min(0).max(100_000).optional(),
  /// Public vacancy page (Sprint 6). The slug is the whole address of the page.
  publicEnabled: z.boolean().optional(),
  publicSlug: z.string().trim().toLowerCase().regex(/^[a-z0-9](?:[a-z0-9-]{1,58}[a-z0-9])$/, 'Use 3–60 letters, digits and dashes').nullable().optional(),
  publicDescription: z.string().trim().max(1200).nullable().optional(),
  publicPhone: z.string().trim().max(20).nullable().optional(),
  amenities: z.array(z.string().trim().min(1).max(40)).max(30).optional(),
  photos: z.array(z.string().max(500)).max(12).optional(),
});

export type PropertyDetailsInput = z.infer<typeof propertyDetailsSchema>;
