// ── Composition Objects ──────────────────────────────────────────────────────

export interface ConfirmDialog {
	title: string;
	text: string;
	confirm: string;
	deny: string;
	style?: "danger";
}

// ── Elements ─────────────────────────────────────────────────────────────────

export interface ButtonElement {
	type: "button";
	action_id: string;
	label: string;
	style?: "primary" | "danger" | "secondary";
	value?: unknown;
	confirm?: ConfirmDialog;
}

export interface TextInputElement {
	type: "text_input";
	action_id: string;
	label: string;
	placeholder?: string;
	initial_value?: string;
	multiline?: boolean;
}

export interface NumberInputElement {
	type: "number_input";
	action_id: string;
	label: string;
	initial_value?: number;
	min?: number;
	max?: number;
}

export interface SelectElement {
	type: "select";
	action_id: string;
	label: string;
	options: Array<{ label: string; value: string }>;
	initial_value?: string;
	/** Plugin route that returns `{ items: Array<{ id, name }> }` to populate options dynamically */
	optionsRoute?: string;
}

export interface ToggleElement {
	type: "toggle";
	action_id: string;
	label: string;
	description?: string;
	initial_value?: boolean;
}

export interface SecretInputElement {
	type: "secret_input";
	action_id: string;
	label: string;
	placeholder?: string;
	has_value?: boolean;
}

export interface CheckboxElement {
	type: "checkbox";
	action_id: string;
	label: string;
	options: Array<{ label: string; value: string }>;
	initial_value?: string[];
}

export interface DateInputElement {
	type: "date_input";
	action_id: string;
	label: string;
	initial_value?: string;
	placeholder?: string;
}

export interface ComboboxElement {
	type: "combobox";
	action_id: string;
	label: string;
	options: Array<{ label: string; value: string }>;
	initial_value?: string;
	placeholder?: string;
}

export interface RadioElement {
	type: "radio";
	action_id: string;
	label: string;
	options: Array<{ label: string; value: string }>;
	initial_value?: string;
}

export type Element =
	| ButtonElement
	| TextInputElement
	| NumberInputElement
	| SelectElement
	| ToggleElement
	| SecretInputElement
	| CheckboxElement
	| DateInputElement
	| ComboboxElement
	| RadioElement;

// ── Form Fields (elements + optional condition) ──────────────────────────────

export type FieldCondition =
	| { field: string; eq?: unknown; neq?: never }
	| { field: string; neq?: unknown; eq?: never };

export type FormField = (
	| ButtonElement
	| TextInputElement
	| NumberInputElement
	| SelectElement
	| ToggleElement
	| SecretInputElement
	| CheckboxElement
	| DateInputElement
	| ComboboxElement
	| RadioElement
) & {
	condition?: FieldCondition;
};

// ── Block Sub-types ──────────────────────────────────────────────────────────

export interface TableColumn {
	key: string;
	label: string;
	format?: "text" | "badge" | "relative_time" | "number" | "code";
	sortable?: boolean;
}

export interface StatItem {
	label: string;
	value: string | number;
	description?: string;
	trend?: "up" | "down" | "neutral";
}

/** A single data series for a timeseries chart. */
export interface ChartSeries {
	/** Display name shown in tooltips and legends */
	name: string;
	/** Array of `[timestamp_ms, value]` tuples ordered by time */
	data: [number, number][];
	/**
	 * Hex color for this series. If omitted, an automatic categorical color
	 * from the Kumo palette is assigned based on the series index.
	 */
	color?: string;
}

/** Timeseries-specific chart configuration */
export interface TimeseriesChartConfig {
	chart_type: "timeseries";
	/** Visual style of each series. Defaults to `"line"`. */
	style?: "line" | "bar";
	/** Array of time series to display */
	series: ChartSeries[];
	/** Label for the x-axis */
	x_axis_name?: string;
	/** Label for the y-axis */
	y_axis_name?: string;
	/** Height of the chart in pixels. Defaults to 350. */
	height?: number;
	/** Render a gradient fill beneath line series */
	gradient?: boolean;
}

/** Custom chart configuration using raw ECharts options (pie, etc.) */
export interface CustomChartConfig {
	chart_type: "custom";
	/** Raw ECharts option object — passed through to `chart.setOption()` */
	options: Record<string, unknown>;
	/** Height of the chart in pixels. Defaults to 350. */
	height?: number;
}

export type ChartConfig = TimeseriesChartConfig | CustomChartConfig;

// ── Blocks ───────────────────────────────────────────────────────────────────

interface BlockBase {
	block_id?: string;
}

export interface HeaderBlock extends BlockBase {
	type: "header";
	text: string;
}

export interface SectionBlock extends BlockBase {
	type: "section";
	text: string;
	accessory?: Element;
}

export interface DividerBlock extends BlockBase {
	type: "divider";
}

export interface FieldsBlock extends BlockBase {
	type: "fields";
	fields: Array<{ label: string; value: string }>;
}

export interface TableBlock extends BlockBase {
	type: "table";
	columns: TableColumn[];
	rows: Array<Record<string, unknown>>;
	next_cursor?: string;
	page_action_id: string;
	empty_text?: string;
}

export interface ActionsBlock extends BlockBase {
	type: "actions";
	elements: Element[];
}

export interface StatsBlock extends BlockBase {
	type: "stats";
	items: StatItem[];
}

export interface FormBlock extends BlockBase {
	type: "form";
	fields: FormField[];
	submit: { label: string; action_id: string };
}

export interface ImageBlock extends BlockBase {
	type: "image";
	url: string;
	alt: string;
	title?: string;
}

export interface ContextBlock extends BlockBase {
	type: "context";
	text: string;
}

export interface ColumnsBlock extends BlockBase {
	type: "columns";
	columns: Block[][];
}

export interface ChartBlock extends BlockBase {
	type: "chart";
	config: ChartConfig;
}

export interface BannerBlock extends BlockBase {
	type: "banner";
	title?: string;
	description?: string;
	variant?: "default" | "alert" | "error";
}

export interface MeterBlock extends BlockBase {
	type: "meter";
	label: string;
	value: number;
	max?: number;
	min?: number;
	custom_value?: string;
}

export interface CodeBlock extends BlockBase {
	type: "code";
	code: string;
	language?: "ts" | "tsx" | "jsonc" | "bash" | "css";
}

export type Block =
	| HeaderBlock
	| SectionBlock
	| DividerBlock
	| FieldsBlock
	| TableBlock
	| ActionsBlock
	| StatsBlock
	| FormBlock
	| ImageBlock
	| ContextBlock
	| ColumnsBlock
	| ChartBlock
	| BannerBlock
	| MeterBlock
	| CodeBlock;

// ── Interactions ─────────────────────────────────────────────────────────────

export interface BlockAction {
	type: "block_action";
	action_id: string;
	block_id?: string;
	value?: unknown;
}

export interface FormSubmit {
	type: "form_submit";
	action_id: string;
	block_id?: string;
	values: Record<string, unknown>;
}

export interface PageLoad {
	type: "page_load";
	page: string;
}

export type BlockInteraction = BlockAction | FormSubmit | PageLoad;

// ── Response ─────────────────────────────────────────────────────────────────

export interface BlockResponse {
	blocks: Block[];
	toast?: { message: string; type: "success" | "error" | "info" };
}
