import { z } from 'zod';
import { DEFAULT_VALUES } from '../../constant/shared';
import { imageSchema } from '../shared/shared.dto';

export const createOwnerSchema = z.object({
  username: z
    .string()
    .min(3, { message: 'Username must be at least 3 characters long' })
    .regex(/^[a-zA-Z0-9_]+$/, {
      message: 'Username can only contain letters, numbers, and underscores',
    }),
  name: z
    .string()
    .min(3, { message: 'Name must be at least 3 characters long' }),
  email: z.email({ message: 'Invalid email address' }),
  password: z
    .string()
    .min(6, { message: 'Password must be at least 6 characters long' })
    .regex(/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*#?&]{6,}$/, {
      message: 'Password must contain at least one letter and one number',
    }),
});

export const signinOwnerSchema = z
  .object({
    username: z.string().optional(),
    email: z.email({ message: 'Invalid email address' }).optional(),
    password: z.string({ message: 'Password is required' }),
    // You can add custom validation to ensure either username or email is provided
  })
  .refine((data) => data.username || data.email, {
    message: 'Either username or email must be provided',
  });

export const resendOtpSchema = z.object({
  email: z.email({ message: 'Invalid email address' }),
});

export const updateOwnerSchema = z.object({
  username: z
    .string()
    .min(3, { message: 'Username must be at least 3 characters long' })
    .regex(/^[a-zA-Z0-9_]+$/, {
      message: 'Username can only contain letters, numbers, and underscores',
    })
    .optional(),
  name: z
    .string()
    .min(3, { message: 'Name must be at least 3 characters long' })
    .optional(),
  email: z.email({ message: 'Invalid email address' }).optional(),
  phone: z.string().optional(),
  address: z.string().optional(),
  image: z
    .object({
      file_name: z.string(),
      original_name: z.string(),
      file_size: z.number(),
      mime_type: z.string(),
    })
    .optional(),
});

// response DTO types
export const ownerResponseSchema = z.object({
  id: z.string().optional(),
  name: z.string().optional(),
  username: z.string().optional(),
  email: z.string().optional(),
  image: imageSchema.optional(),
  address: z
    .string()
    .transform((val) => {
      if (!val || val === DEFAULT_VALUES.STRING) return '';
      return val;
    })
    .optional(),
  phone: z
    .string()
    .transform((val) => {
      if (!val || val === DEFAULT_VALUES.STRING) return '';
      return val;
    })
    .optional(),
  status: z.string().optional(),
  created_at: z
    .date()
    .transform((date) => date.toISOString())
    .optional(), // Transform dates
  updated_at: z
    .date()
    .transform((date) => date.toISOString())
    .optional(),
  access_token: z.string().optional(),
  // Notice: password is NOT included here
});

// response DTO types
export const authResponseSchema = z.object({
  id: z.string().optional(),
  name: z.string().optional(),
  email: z.string().optional(),
  username: z.string().optional(),
  access_token: z.string().optional(),
  role: z.string().optional(),
  image: imageSchema.optional(),
  address: z
    .string()
    .transform((val) => {
      if (!val || val === DEFAULT_VALUES.STRING) return '';
      return val;
    })
    .optional(),
  phone: z
    .string()
    .transform((val) => {
      if (!val || val === DEFAULT_VALUES.STRING) return '';
      return val;
    })
    .optional(),
});

export const ownersResponseSchema = z.array(ownerResponseSchema);

export type createOwnerDto = z.infer<typeof createOwnerSchema>;
export type signinOwnerDto = z.infer<typeof signinOwnerSchema>;
export type resendOtpDto = z.infer<typeof resendOtpSchema>;
export type updateOwnerDto = z.infer<typeof updateOwnerSchema>;
export type ownerResponseDto = z.infer<typeof ownerResponseSchema>;
export type ownersResponseDto = z.infer<typeof ownersResponseSchema>;
export type authResponseDto = z.infer<typeof authResponseSchema>;
