/**
 * Experiment Types for Admin Dashboard
 * Re-export of types from platform-service for client-side use.
 */

export type ExperimentStatus = 'draft' | 'running' | 'paused' | 'stopped' | 'completed';

export type AllocationStrategy = 'random' | 'thompson' | 'epsilon_greedy' | 'ucb';

export type MetricType = 'conversion' | 'count' | 'duration' | 'revenue' | 'custom';

export interface ExperimentVariant {
  id: string;
  experimentId: string;
  name: string;
  description?: string;
  isControl: boolean;
  flagConfig: Record<string, unknown>;
  currentAllocationPercent: number;
  stats?: {
    participants: number;
    events: number;
    primaryMetricValue: number;
    primaryMetricStdDev?: number;
    conversions?: number;
    conversionRate?: number;
    betaAlpha?: number;
    betaBeta?: number;
  };
  bayesianResults?: {
    probabilityBeatsControl: number;
    probabilityBeatsAll: number;
    expectedLiftPercent: number;
    expectedLoss: number;
    credibleInterval: {
      lower: number;
      mean: number;
      upper: number;
    };
  };
  createdAt: string;
  updatedAt: string;
}

export interface PrimaryMetric {
  name: string;
  type: MetricType;
  eventName: string;
  aggregation: 'sum' | 'mean' | 'count' | 'unique';
  direction: 'increase' | 'decrease';
  minimumDetectableEffect: number;
}

export interface TargetingConfig {
  platforms?: string[];
  appVersions?: { min: string; max?: string };
  regions?: string[];
  userSegments?: string[];
  userProperties?: Record<string, string | number | boolean>;
}

export interface GuardrailsConfig {
  minSampleSizePerVariant: number;
  maxDurationDays: number;
  autoStopEnabled: boolean;
  winnerThreshold: number;
  requireApprovalFor: 'none' | 'revenue' | 'all';
}

export interface ExperimentDoc {
  id: string;
  productId: string;
  name: string;
  description: string;
  hypothesis: string;
  aiGeneratedHypothesis?: boolean;
  status: ExperimentStatus;
  controlVariantId: string;
  variantIds: string[];
  allocationStrategy: AllocationStrategy;
  targetPercent: number;
  targeting: TargetingConfig;
  primaryMetric: PrimaryMetric;
  secondaryMetrics: Array<{
    name: string;
    type: MetricType;
    eventName: string;
  }>;
  guardrails: GuardrailsConfig;
  startAt?: string;
  endAt?: string;
  totalParticipants: number;
  totalEvents: number;
  createdAt: string;
  updatedAt: string;
  startedAt?: string;
  completedAt?: string;
  variants?: ExperimentVariant[];
}

export type VariantDoc = ExperimentVariant;

export interface ExperimentResult {
  experimentId: string;
  status: 'in_progress' | 'winner_found' | 'no_winner' | 'stopped';
  totalParticipants: number;
  totalEvents: number;
  daysRunning: number;
  winnerVariantId?: string;
  winnerProbability?: number;
  variantResults: Array<{
    variantId: string;
    variantName: string;
    isControl: boolean;
    participants: number;
    primaryMetricValue: number;
    probabilityBeatsControl: number;
    expectedLiftPercent: number;
    credibleInterval: {
      lower: number;
      mean: number;
      upper: number;
    };
  }>;
  statisticalSummary: {
    probabilityAnyBeatsControl: number;
    expectedLossIfShipped: number;
    recommendedAction: 'ship' | 'rollback' | 'continue' | 'stop';
  };
  earlyStopped: boolean;
  stopReason?: string;
  generatedAt: string;
}

export interface GeneratedHypothesis {
  primary: string;
  alternatives: string[];
  expectedEffectSize: number;
  successMetric: string;
  riskAssessment: 'low' | 'medium' | 'high';
  impactScore: number;
  difficultyScore: number;
  powerPrediction: number;
}

export interface ExperimentSuggestion {
  id: string;
  hypothesis: GeneratedHypothesis;
  suggestedVariants: Array<{ name: string; description: string }>;
  suggestedMetrics: PrimaryMetric[];
  suggestedDuration: number;
  suggestedSampleSize: number;
  priority: number;
  aiGenerated: boolean;
  createdAt: string;
}

export interface CreateExperimentInput {
  name: string;
  description: string;
  hypothesis: string;
  variants: Array<{
    key: string;
    name: string;
    description?: string;
    isControl?: boolean;
    flagConfig?: Record<string, unknown>;
  }>;
  allocationStrategy: AllocationStrategy;
  targetPercent: number;
  targeting: TargetingConfig;
  primaryMetric: PrimaryMetric;
  secondaryMetrics: Array<{
    name: string;
    type: MetricType;
    eventName: string;
  }>;
  guardrails: GuardrailsConfig;
  startAt?: string;
}
