import { z } from 'zod';

export const approvalActionSchema = z.object({
  action: z.enum(['APPROVE', 'REJECT']),
  comments: z.string().optional(),
  rejectionReason: z.string().optional(),
});

// Helper to validate UUID or requestNumber format (REQ-YYYY-NNNNN)
const workflowIdValidator = z.string().refine(
  (val) => {
    // Check if it's a UUID
    const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
    if (uuidRegex.test(val)) {
      return true;
    }
    // Check if it's a requestNumber format (REQ-YYYY-NNNNN)
    const requestNumberRegex = /^REQ-\d{4}-\d+$/i;
    if (requestNumberRegex.test(val)) {
      return true;
    }
    return false;
  },
  {
    message: 'Invalid workflow ID - must be a UUID or requestNumber format (REQ-YYYY-NNNNN)'
  }
);

export const approvalParamsSchema = z.object({
  id: workflowIdValidator,
  levelId: z.string().uuid('Invalid approval level ID'),
});

export const validateApprovalAction = (data: any) => {
  return approvalActionSchema.parse(data);
};
