import { z } from 'zod/v4';
import { HouseholdRole } from '../enums/roles.enums.js';

export const HouseholdSettingsSchema = z.object({
  timezone: z.string().default('UTC'),
  currency: z.string().max(3).default('USD'),
  language: z.string().max(5).default('en'),
});

export const CreateHouseholdSchema = z.object({
  name: z.string().min(1).max(100).trim(),
  settings: HouseholdSettingsSchema.optional(),
});

export const UpdateHouseholdSchema = z.object({
  name: z.string().min(1).max(100).trim().optional(),
  settings: HouseholdSettingsSchema.partial().optional(),
});

export const JoinHouseholdSchema = z.object({
  inviteCode: z.string().min(1),
});

export const HouseholdMemberSchema = z.object({
  userId: z.string().min(1),
  role: z.nativeEnum(HouseholdRole),
  joinedAt: z.date(),
});

// Response schema for API output — dates are ISO strings after JSON serialization.
export const HouseholdResponseSchema = z.object({
  _id: z.string(),
  name: z.string(),
  ownerUserId: z.string(),
  members: z.array(
    z.object({
      userId: z.string(),
      role: z.string(),
      joinedAt: z.string(),
    }),
  ),
  inviteCode: z.string(),
  settings: z.object({
    timezone: z.string(),
    currency: z.string(),
    language: z.string(),
  }),
  createdAt: z.string(),
  updatedAt: z.string(),
});

export type CreateHouseholdInput = z.infer<typeof CreateHouseholdSchema>;
export type UpdateHouseholdInput = z.infer<typeof UpdateHouseholdSchema>;
export type JoinHouseholdInput = z.infer<typeof JoinHouseholdSchema>;
