import { m } from '@paraglide/messages';
import z from 'zod';

export const baseHouse = z.object({
  id: z.string().nonempty(m.houses_page_message_house_not_found()),
});

export const houseListSchema = z.object({
  page: z.coerce.number().min(1).default(1),
  limit: z.coerce.number().min(10).max(100).default(10),
  keyword: z.string().optional(),
});

export const houseForSelectSchema = z.object({
  keyword: z.string().optional(),
});

export const houseCreateSchema = z.object({
  name: z
    .string()
    .nonempty(m.common_is_required({ field: m.houses_page_form_name() })),
  userId: z
    .string()
    .nonempty(m.common_is_required({ field: m.houses_page_form_user() })),
  color: z
    .string()
    .nonempty(m.common_is_required({ field: m.houses_page_form_color() })),
});

export const houseCreateBESchema = houseCreateSchema.extend({
  slug: z.string().nonempty(m.common_is_required({ field: 'Slug' })),
});

export const houseCreateByUserBESchema = houseCreateBESchema.omit({
  userId: true,
});

export const houseEditSchema = baseHouse.extend({
  name: z
    .string()
    .nonempty(m.common_is_required({ field: m.houses_page_form_name() })),
  color: z
    .string()
    .nonempty(m.common_is_required({ field: m.houses_page_form_color() })),
});

export const houseEditBESchema = houseEditSchema.extend({
  slug: z.string().nonempty(m.common_is_required({ field: 'Slug' })),
});

export const RoleHouseEnum = z.enum(
  ['owner', 'admin', 'member'],
  m.users_page_message_role_select(),
);

const baseInvitation = z.object({
  id: z.string().nonempty(m.invitation_not_found()),
});

const invitationCreateSchema = z.object({
  email: z
    .string()
    .nonempty(m.common_is_required({ field: m.houses_page_form_user() })),
  houseId: z.string().nonempty(m.houses_page_message_house_not_found()),
});

export const invitationCreateFESchema = invitationCreateSchema.extend({
  role: z.string().nonempty(m.users_page_message_role_select()),
});

export const invitationCreateBESchema = invitationCreateSchema.extend({
  role: RoleHouseEnum,
});

export const actionInvitationSchema = baseInvitation.extend({
  notificationId: z.string().nonempty(m.notification_page_notify_not_found()),
});

export const removeMemberSchema = z.object({
  memberId: z.string().nonempty(m.users_page_message_user_not_found()),
  houseId: z.string().nonempty(m.houses_page_message_house_not_found()),
});
