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

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

export const changePasswordFormSchema = z
  .object({
    currentPassword: z.string().nonempty(
      m.common_is_required({
        field: m.change_password_form_current_password(),
      }),
    ),
    newPassword: z.string().nonempty(
      m.common_is_required({
        field: m.change_password_form_new_password(),
      }),
    ),
    confirmPassword: z.string().nonempty(
      m.common_is_required({
        field: m.change_password_form_confirm_password(),
      }),
    ),
  })
  .superRefine((data, ctx) => {
    if (data.newPassword !== data.confirmPassword) {
      ctx.addIssue({
        path: ['confirmPassword'],
        code: z.ZodIssueCode.custom,
        message: m.change_password_messages_password_not_match(),
      });
    }
  });

export const changePasswordBESchema = z.object({
  currentPassword: z.string().nonempty(
    m.common_is_required({
      field: m.change_password_form_current_password(),
    }),
  ),
  newPassword: z.string().nonempty(
    m.common_is_required({
      field: m.change_password_form_new_password(),
    }),
  ),
});

export type ChangePassword = z.infer<typeof changePasswordFormSchema>;

export const userListSchema = 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 userForSelectSchema = z.object({
  keyword: z.string().optional(),
  noself: z.boolean().optional().default(false),
});

export const userSetPasswordSchema = baseUser.extend({
  password: z
    .string()
    .min(5, m.users_page_message_user_min())
    .regex(/[A-Z]/, m.users_page_message_contain_uppercase())
    .regex(/[a-z]/, m.users_page_message_contain_lowercase())
    .regex(/[0-9]/, m.users_page_message_contain_number())
    .regex(/[^a-zA-Z0-9]/, m.users_page_message_contain_special()),
});

export const userUpdateInfoSchema = baseUser.extend({
  name: z.string().nonempty(
    m.common_is_required({
      field: m.profile_form_name(),
    }),
  ),
});

export const RoleEnum = z.enum(
  ['admin', 'user'],
  m.users_page_message_role_select(),
);

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

export const userUpdateRoleBESchema = baseUser.extend({
  role: RoleEnum,
});

export const userBanSchema = baseUser.extend({
  banReason: z.string().nonempty(
    m.common_is_required({
      field: m.users_page_ui_form_ban_reason(),
    }),
  ),
  banExp: z.number().int().min(1, m.users_page_message_select_min_one_day()),
});

const userCreateBaseSchema = z.object({
  email: z
    .string()
    .nonempty(m.common_is_required({ field: m.login_page_form_email() }))
    .email(m.login_page_messages_email_invalid()),
  password: z
    .string()
    .min(5, m.users_page_message_user_min())
    .regex(/[A-Z]/, m.users_page_message_contain_uppercase())
    .regex(/[a-z]/, m.users_page_message_contain_lowercase())
    .regex(/[0-9]/, m.users_page_message_contain_number())
    .regex(/[^a-zA-Z0-9]/, m.users_page_message_contain_special()),
  name: z.string().nonempty(
    m.common_is_required({
      field: m.profile_form_name(),
    }),
  ),
});

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

export const userCreateBESchema = userCreateBaseSchema.extend({
  role: RoleEnum,
});
