/**
 * Prop shapes that match the CauseWise REST payloads already returned by
 * @razorsoft/causewise. This package does not import that SDK.
 *
 * Summary funnels do not include steps. Pass `steps` from funnel analytics
 * when a step list is needed. Summary recent events omit properties.
 * `conversionWindow` is seconds. Overview `trend` counts every event in each bucket.
 */

export type AnalyticsRange = "today" | "7d" | "30d";

export type ReportingPeriod = {
  range: AnalyticsRange | string;
  start: string;
  end: string;
  retentionDays?: number;
};

export type OverviewStats = {
  pageviews: number;
  sessions: number;
  sessionsNote?: string;
  visitors: number | null;
  visitorsNote?: string;
};

export type TrafficPoint = {
  label: string;
  /** Event count for that bucket, unless the host sets a different seriesLabel. */
  count: number;
};

export type TrafficSeries = {
  range?: string;
  start?: string;
  end?: string;
  trend: TrafficPoint[];
  /** Defaults to "Events". Overview trend is not a pageview or visitor series. */
  seriesLabel?: string;
};

export type TopPage = {
  path: string;
  count: number;
};

export type Referrer = {
  label: string;
  count: number;
};

export type GoalRow = {
  id: string;
  name: string;
  eventName: string;
  conversions: number;
  trackingStatus: string;
  lastMatchingAt: string | null;
};

export type GoalsSection =
  | {
      available: true;
      activeGoalCount?: number;
      totalConversions?: number;
      conversionRate?: number | null;
      conversionRateDefinition?: string;
      truncated?: boolean;
      goals: GoalRow[];
    }
  | { available: false; reason: string };

export type FunnelStep = {
  stepId: string;
  name: string;
  position: number;
  visitors: number;
  continuedPct: number | null;
  dropoffCount: number | null;
  dropoffPct: number | null;
};

export type LargestDropOff = {
  stepId?: string;
  name: string;
  position?: number;
  dropoffCount: number | null;
  dropoffPct: number | null;
};

export type FunnelData = {
  id: string;
  name: string;
  isActive?: boolean;
  displayState?: string;
  entryVisitors: number;
  completionCount: number;
  overallConversionRate: number | null;
  overallConversionRateDefinition?: string;
  /** Conversion window in seconds. */
  conversionWindow?: number;
  largestDropOff?: LargestDropOff | null;
  steps?: FunnelStep[];
  medianConversionTime?: string | null;
};

export type FunnelsSection =
  | { available: true; truncated?: boolean; funnels: FunnelData[] }
  | { available: false; reason: string };

export type RecentEvent = {
  id: string;
  type: string;
  path: string;
  timestamp: string;
};

export type StatusNotice = {
  code?: string;
  message: string;
};

export type ViewState = "ready" | "loading" | "error";

export type ThemeName = "light" | "dark" | "system";

export type DashboardSections = {
  period?: boolean;
  stats?: boolean;
  traffic?: boolean;
  topPages?: boolean;
  goals?: boolean;
  funnels?: boolean;
  recentEvents?: boolean;
};
