// =============================================================================
// FormGeneratorReport - Types
// Generic reporting component with charts, KPIs, tables, and filters
// =============================================================================

// =============================================================================
// FILTER TYPES
// =============================================================================

/** Period granularity for time-based reports */
export type ReportPeriod = 'day' | 'week' | 'month' | 'quarter' | 'year';

/** Date range with from/to */
export interface ReportDateRange {
  from: Date;
  to: Date;
}

/** Filter option for select/multiselect filters */
export interface ReportFilterOption {
  value: string;
  label: string;
}

/** A single filter definition */
export interface ReportFilterConfig {
  /** Unique key for this filter */
  key: string;
  /** Display label */
  label: string;
  /** Filter type */
  type: 'select' | 'multiselect' | 'text';
  /** Available options (for select/multiselect) */
  options?: ReportFilterOption[];
  /** Default value */
  defaultValue?: string | string[];
  /** Placeholder text */
  placeholder?: string;
}

/** Period selector configuration */
export interface ReportPeriodSelectorConfig {
  /** Available periods */
  periods: ReportPeriod[];
  /** Default period */
  defaultPeriod: ReportPeriod;
  /** Whether to show year selector */
  showYear?: boolean;
  /** Whether to show month selector (when period is 'day') */
  showMonth?: boolean;
  /** Default year */
  defaultYear?: number;
  /** Default month (1-12) */
  defaultMonth?: number;
}

/** Date range selector configuration */
export interface ReportDateRangeSelectorConfig {
  /** Whether the date range selector is enabled */
  enabled: boolean;
  /** Default from date */
  defaultFrom?: Date;
  /** Default to date */
  defaultTo?: Date;
}

/** Combined filter state passed to the data callback */
export interface ReportFilterState {
  /** Selected period */
  period?: ReportPeriod;
  /** Selected year */
  year?: number;
  /** Selected month (1-12) */
  month?: number;
  /** Date range */
  dateRange?: ReportDateRange;
  /** Custom filter values: key -> value(s) */
  filters: Record<string, string | string[]>;
}

// =============================================================================
// SECTION TYPES
// =============================================================================

/** KPI item for kpiGrid section */
export interface ReportKpiItem {
  label: string;
  value: string | number;
  subtitle?: string;
  /** Optional color (CSS variable or hex) */
  color?: string;
}

/** Column definition for table sections */
export interface ReportTableColumn {
  key: string;
  label: string;
  /** How to format the value */
  format?: 'text' | 'number' | 'currency' | 'percent' | 'date';
  /** Text alignment */
  align?: 'left' | 'center' | 'right';
  /** Custom formatter function */
  formatValue?: (value: any, row: Record<string, any>) => string;
}

/** Data point for chart sections */
export interface ReportChartDataPoint {
  /** Key/label for the data point (x-axis or category) */
  key: string;
  /** Numeric value */
  value: number;
  /** Optional secondary value */
  value2?: number;
  /** Optional color override */
  color?: string;
}

/** Time series data point */
export interface ReportTimeSeriesPoint {
  /** Date string (ISO format: "2026-02-08" or "2026-02") */
  date: string;
  /** Numeric values, keyed by series name */
  [seriesKey: string]: string | number;
}

/** Series definition for multi-series charts */
export interface ReportChartSeries {
  key: string;
  label: string;
  color?: string;
}

// =============================================================================
// SECTION DEFINITIONS
// =============================================================================

interface ReportSectionBase {
  /** Optional section title */
  title?: string;
  /** Optional description text */
  description?: string;
  /** Grid span: 'full' takes full width, 'half' takes 50% */
  span?: 'full' | 'half';
}

/** KPI grid: display metric cards */
export interface ReportSectionKpi extends ReportSectionBase {
  type: 'kpiGrid';
  items: ReportKpiItem[];
}

/** Vertical bar chart */
export interface ReportSectionBarChart extends ReportSectionBase {
  type: 'barChart';
  data: ReportChartDataPoint[];
  /** Value format for tooltips/labels */
  formatValue?: (value: number) => string;
  /** Bar color */
  color?: string;
}

/** Horizontal bar chart (for comparisons/rankings) */
export interface ReportSectionHorizontalBar extends ReportSectionBase {
  type: 'horizontalBar';
  data: ReportChartDataPoint[];
  /** Value format for tooltips/labels */
  formatValue?: (value: number) => string;
}

/** Line chart (trends over time) */
export interface ReportSectionLineChart extends ReportSectionBase {
  type: 'lineChart';
  data: ReportTimeSeriesPoint[];
  series: ReportChartSeries[];
  /** Value format for tooltips/labels */
  formatValue?: (value: number) => string;
}

/** Pie/donut chart (distribution) */
export interface ReportSectionPieChart extends ReportSectionBase {
  type: 'pieChart';
  data: ReportChartDataPoint[];
  /** Show as donut (hollow center) */
  donut?: boolean;
  /** Value format for tooltips/labels */
  formatValue?: (value: number) => string;
}

/** Simple data table */
export interface ReportSectionTable extends ReportSectionBase {
  type: 'table';
  columns: ReportTableColumn[];
  rows: Record<string, any>[];
  /** Maximum rows to display (default: all) */
  maxRows?: number;
}

/** Area chart (filled line chart) */
export interface ReportSectionAreaChart extends ReportSectionBase {
  type: 'areaChart';
  data: ReportTimeSeriesPoint[];
  series: ReportChartSeries[];
  /** Value format for tooltips/labels */
  formatValue?: (value: number) => string;
}

/** Union of all section types */
export type ReportSection =
  | ReportSectionKpi
  | ReportSectionBarChart
  | ReportSectionHorizontalBar
  | ReportSectionLineChart
  | ReportSectionPieChart
  | ReportSectionTable
  | ReportSectionAreaChart;

// =============================================================================
// MAIN COMPONENT PROPS
// =============================================================================

export interface FormGeneratorReportProps {
  /** Report title (optional) */
  title?: string;
  /** Report subtitle/description (optional) */
  subtitle?: string;

  /** Report sections to render */
  sections: ReportSection[];

  /** Loading state */
  loading?: boolean;

  /** No data message */
  noDataMessage?: string;

  // --- Filter Configuration ---

  /** Period selector config */
  periodSelector?: ReportPeriodSelectorConfig;

  /** Date range selector config */
  dateRangeSelector?: ReportDateRangeSelectorConfig;

  /** Custom filter definitions */
  filters?: ReportFilterConfig[];

  /** Called when any filter changes. Parent should reload data and update sections. */
  onFilterChange?: (filterState: ReportFilterState) => void;

  /** Currency code for formatting (default: 'CHF') */
  currencyCode?: string;

  /** Custom CSS class */
  className?: string;
}
