/**
 * Deals Pipeline Validators
 */

import { z } from 'zod';

export const createDealSchema = z.object({
  title: z.string().min(1).max(200),
  customerId: z.string().optional(),
  quotationId: z.string().optional(),
  value: z.number().min(0).default(0),
  currency: z.string().length(3).default('QAR'),
  stage: z.enum(['LEAD', 'QUALIFIED', 'PROPOSAL', 'NEGOTIATION', 'CLOSED_WON', 'CLOSED_LOST']).default('LEAD'),
  probability: z.number().int().min(0).max(100).default(0),
  expectedCloseDate: z.string().datetime().optional(),
  notes: z.string().max(2000).optional(),
});

export const updateDealSchema = z.object({
  title: z.string().min(1).max(200).optional(),
  customerId: z.string().nullable().optional(),
  quotationId: z.string().nullable().optional(),
  value: z.number().min(0).optional(),
  currency: z.string().length(3).optional(),
  stage: z.enum(['LEAD', 'QUALIFIED', 'PROPOSAL', 'NEGOTIATION', 'CLOSED_WON', 'CLOSED_LOST']).optional(),
  probability: z.number().int().min(0).max(100).optional(),
  expectedCloseDate: z.string().datetime().nullable().optional(),
  lostReason: z.string().max(500).optional(),
  notes: z.string().max(2000).nullable().optional(),
});

export const moveDealSchema = z.object({
  dealId: z.string().min(1),
  stage: z.enum(['LEAD', 'QUALIFIED', 'PROPOSAL', 'NEGOTIATION', 'CLOSED_WON', 'CLOSED_LOST']),
});

export const dealFilterSchema = z.object({
  stage: z.enum(['LEAD', 'QUALIFIED', 'PROPOSAL', 'NEGOTIATION', 'CLOSED_WON', 'CLOSED_LOST']).optional(),
  customerId: z.string().optional(),
  search: z.string().optional(),
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().positive().max(100).default(20),
  sortBy: z.enum(['createdAt', 'updatedAt', 'value', 'title']).default('createdAt'),
  sortOrder: z.enum(['asc', 'desc']).default('desc'),
});

export type CreateDealInput = z.infer<typeof createDealSchema>;
export type UpdateDealInput = z.infer<typeof updateDealSchema>;
export type MoveDealInput = z.infer<typeof moveDealSchema>;
export type DealFilterInput = z.infer<typeof dealFilterSchema>;
