import { ItemCondition } from './models';
import * as z from 'zod';

export const LoginRequestSchema = z.object({
    email: z.email(),
    password: z.string(),
});

export type LoginRequest = z.infer<typeof LoginRequestSchema>;

export const RegisterRequestSchema = z.object({
    name: z.string().max(50),
    email: z.email(),
    password: z.string().max(100),
    password_confirmation: z.string().max(100),
}).superRefine(({ password, password_confirmation }, ctx) => {
    if (password !== password_confirmation) {
        ctx.addIssue({
            code: "custom",
            message: "The passwords did not match",
            path: ["password_confirmation"]
        });
    }
});

export type RegisterRequest = z.infer<typeof RegisterRequestSchema>;

export const CreateListingRequestSchema = z.object({
    title: z.string().max(100).nonoptional(),
    description: z.string().max(1000).nonoptional(),
    condition: z.enum(Object.values(ItemCondition)).optional(),
    price: z.number().nonnegative().nonoptional(),
    location: z.string().max(50).optional(),
});

export type CreateListingRequest = z.infer<typeof CreateListingRequestSchema>;

export const UpdateListingRequestSchema = z.object({
    title: z.string().max(100).optional(),
    description: z.string().max(1000).optional(),
    condition: z.enum(Object.values(ItemCondition)).optional(),
    price: z.number().nonnegative().optional(),
    location: z.string().max(50).optional(),
});

export type UpdateListingRequest = z.infer<typeof UpdateListingRequestSchema>;
