/**
 * Onboarding analytics types — track step completion and funnel conversion.
 *
 * Cosmos container: `onboarding_events` (partitioned by `/productId`)
 */

import { z } from 'zod';

// ── Onboarding Event Doc ──

export interface OnboardingEventDoc {
  id: string;
  productId: string;
  userId: string;
  stepName: string;
  stepIndex: number;
  completedAt: string;
  metadata?: Record<string, unknown>;
}

export interface OnboardingCompletionDoc {
  id: string;
  productId: string;
  userId: string;
  completedAt: string;
  totalSteps: number;
  durationMs?: number;
}

// ── Schemas ──

export const TrackStepSchema = z.object({
  userId: z.string().min(1),
  stepName: z.string().min(1).max(128),
  stepIndex: z.number().int().min(0).max(100),
  metadata: z.record(z.unknown()).optional(),
});

export const TrackCompletionSchema = z.object({
  userId: z.string().min(1),
  totalSteps: z.number().int().min(1).max(100).optional().default(1),
  durationMs: z.number().int().min(0).optional(),
});

export type TrackStepInput = z.infer<typeof TrackStepSchema>;
export type TrackCompletionInput = z.infer<typeof TrackCompletionSchema>;

// ── Funnel types ──

export interface FunnelStep {
  stepName: string;
  stepIndex: number;
  uniqueUsers: number;
  conversionRate: number;
}

export interface OnboardingFunnel {
  productId: string;
  totalStarted: number;
  totalCompleted: number;
  completionRate: number;
  steps: FunnelStep[];
  avgDurationMs?: number;
}
