import { z } from 'zod';

export const adminLoginPayloadSchema = z.object({
  username: z.string().min(1),
  password: z.string().min(1),
});

export type AdminLoginPayload = z.infer<typeof adminLoginPayloadSchema>;

export const adminLoginResponseSchema = z.object({
  token: z.string(),
});

export type AdminLoginResponse = z.infer<typeof adminLoginResponseSchema>;

export const adminProfileSchema = z.object({
  id: z.union([z.string(), z.number()]),
  username: z.string(),
  createdAt: z.string(),
});

export type AdminProfile = z.infer<typeof adminProfileSchema>;

export const adminRegisterPayloadSchema = z.object({
  username: z.string().min(3),
  password: z.string().min(8),
});

export type AdminRegisterPayload = z.infer<typeof adminRegisterPayloadSchema>;

export const adminRegisterResponseSchema = z.object({
  id: z.union([z.string(), z.number()]),
  username: z.string(),
});

export type AdminRegisterResponse = z.infer<typeof adminRegisterResponseSchema>;

export const adminPasswordChangePayloadSchema = z.object({
  currentPassword: z.string().min(1),
  newPassword: z.string().min(8),
});

export type AdminPasswordChangePayload = z.infer<typeof adminPasswordChangePayloadSchema>;

export const adminPasswordChangeResponseSchema = z.object({
  updated: z.boolean(),
});

export type AdminPasswordChangeResponse = z.infer<typeof adminPasswordChangeResponseSchema>;

export const adminDeleteResponseSchema = z.object({
  deleted: z.boolean(),
});

export type AdminDeleteResponse = z.infer<typeof adminDeleteResponseSchema>;

const optionalString = z
  .string()
  .trim()
  .transform((value) => (value.length === 0 ? undefined : value))
  .optional();

const optionalNullableString = optionalString.nullable();

const optionalNullableNumberId = z
  .union([z.string(), z.number()])
  .transform((value) => {
    const numeric = Number(value);
    return Number.isNaN(numeric) ? undefined : numeric;
  })
  .optional()
  .nullable();

export const adminServiceCreatePayloadSchema = z.object({
  title: z.string().min(1),
  slug: z.string().min(1),
  description: z.string().min(1),
  priceFrom: optionalNullableString,
  imageUrl: optionalNullableString,
  status: optionalString,
  categoryId: optionalNullableNumberId,
});

export type AdminServiceCreatePayload = z.infer<typeof adminServiceCreatePayloadSchema>;

export const adminServiceCreateResponseSchema = z.object({
  id: z.union([z.string(), z.number()]),
});

export type AdminServiceCreateResponse = z.infer<typeof adminServiceCreateResponseSchema>;

export const adminServiceUpdatePayloadSchema = z.object({
  title: z.string().min(1).optional(),
  slug: z.string().min(1).optional(),
  description: z.string().min(1).optional(),
  priceFrom: optionalNullableString,
  imageUrl: optionalNullableString,
  status: optionalString,
  categoryId: optionalNullableNumberId,
});

export type AdminServiceUpdatePayload = z.infer<typeof adminServiceUpdatePayloadSchema>;

export const adminServiceUpdateResponseSchema = z.object({
  updated: z.boolean(),
});

export type AdminServiceUpdateResponse = z.infer<typeof adminServiceUpdateResponseSchema>;

export const adminServiceCategorySchema = z.object({
  id: z.union([z.string(), z.number()]),
  name: z.string(),
  slug: z.string(),
});

export type AdminServiceCategory = z.infer<typeof adminServiceCategorySchema>;

export const adminServiceCategoryListSchema = z.array(adminServiceCategorySchema);

export type AdminServiceCategoryList = z.infer<typeof adminServiceCategoryListSchema>;

export const adminServiceCategoryCreatePayloadSchema = z.object({
  name: z.string().min(1),
  slug: z.string().min(1),
});

export type AdminServiceCategoryCreatePayload = z.infer<typeof adminServiceCategoryCreatePayloadSchema>;

export const adminServiceCategoryCreateResponseSchema = z.object({
  id: z.union([z.string(), z.number()]),
});

export type AdminServiceCategoryCreateResponse = z.infer<typeof adminServiceCategoryCreateResponseSchema>;

export const adminServiceCategoryUpdatePayloadSchema = z.object({
  name: z.string().min(1).optional(),
  slug: z.string().min(1).optional(),
});

export type AdminServiceCategoryUpdatePayload = z.infer<typeof adminServiceCategoryUpdatePayloadSchema>;

export const adminServiceCategoryUpdateResponseSchema = z.object({
  updated: z.boolean(),
});

export type AdminServiceCategoryUpdateResponse = z.infer<typeof adminServiceCategoryUpdateResponseSchema>;

export const adminNewsUpdatePayloadSchema = z.object({
  title: z.string().min(1),
  slug: z.string().min(1),
  summary: z.string().min(1),
  content: z.string().min(1),
  imageUrl: z.string().url().optional().or(z.null()),
  status: z.enum(['draft', 'published', 'archived']),
});

export type AdminNewsUpdatePayload = z.infer<typeof adminNewsUpdatePayloadSchema>;

export const adminNewsCreateResponseSchema = z.object({
  id: z.union([z.string(), z.number()]),
});

export type AdminNewsCreateResponse = z.infer<typeof adminNewsCreateResponseSchema>;

export const adminNewsCreatePayloadSchema = adminNewsUpdatePayloadSchema.extend({
  status: adminNewsUpdatePayloadSchema.shape.status.default('draft'),
});

export type AdminNewsCreatePayload = z.infer<typeof adminNewsCreatePayloadSchema>;

export const adminNewsPublishResponseSchema = z.object({
  published: z.boolean(),
});

export type AdminNewsPublishResponse = z.infer<typeof adminNewsPublishResponseSchema>;

export const adminNewsUpdateResponseSchema = z.object({
  updated: z.boolean(),
});

export type AdminNewsUpdateResponse = z.infer<typeof adminNewsUpdateResponseSchema>;
