/** Shared TypeScript interfaces for API entities. */

export interface ServiceProvider {
	id: number;
	type: string;
	name: string;
	icon: string;
	config: Record<string, any>;
	webhook_token: string;
	created_at: string;
}

export interface MatrixBot {
	id: number;
	name: string;
	icon: string;
	homeserver_url: string;
	access_token: string;
	display_name: string;
	created_at: string;
}

export interface EmailBot {
	id: number;
	name: string;
	icon: string;
	email: string;
	smtp_host: string;
	smtp_port: number;
	smtp_username: string;
	smtp_password: string;
	smtp_use_tls: boolean;
	created_at: string;
}

export interface TelegramBot {
	id: number;
	name: string;
	icon: string;
	bot_username: string;
	bot_id: number;
	webhook_path_id: string;
	commands_config: Record<string, any>;
	token_preview: string;
	update_mode?: string;
	created_at: string;
}

export interface TelegramChat {
	id: number;
	chat_id: string;
	title: string;
	type: string;
	username: string;
	language_code?: string;
	language_override?: string;
	commands_enabled: boolean;
	discovered_at: string;
}

export interface TrackerTarget {
	id: number;
	target_id: number;
	target_name: string | null;
	target_type: string | null;
	target_icon: string | null;
	tracking_config_id: number | null;
	template_config_id: number | null;
	enabled: boolean;
	quiet_hours_start: string | null;
	quiet_hours_end: string | null;
	commands_config: Record<string, any> | null;
	created_at: string;
}

export interface Tracker {
	id: number;
	name: string;
	icon: string;
	provider_id: number;
	collection_ids: string[];
	scan_interval: number;
	adaptive_max_skip: number | null;
	default_tracking_config_id: number | null;
	default_template_config_id: number | null;
	enabled: boolean;
	filters?: Record<string, any>;
	tracker_targets: TrackerTarget[];
	created_at: string;
}

export interface TargetReceiver {
	id: number;
	target_id: number;
	name: string;
	config: Record<string, any>;
	receiver_key: string;
	enabled: boolean;
	chat_name?: string;
	created_at?: string;
}

export interface NotificationTarget {
	id: number;
	type: string;
	name: string;
	icon: string;
	config: Record<string, any>;
	chat_action?: string | null;
	chat_name?: string;
	receiver_count: number;
	receivers: TargetReceiver[];
	/** Broadcast targets only: resolved child target summaries. */
	child_targets?: { id: number; name: string; type: string; icon: string }[];
	created_at: string;
}

export interface TrackingConfig {
	id: number;
	provider_type: string;
	name: string;
	icon: string;
	track_assets_added: boolean;
	track_assets_removed: boolean;
	track_collection_renamed: boolean;
	track_collection_deleted: boolean;
	track_sharing_changed: boolean;
	track_images: boolean;
	track_videos: boolean;
	// Gitea-specific
	track_push?: boolean;
	track_issue_opened?: boolean;
	track_issue_closed?: boolean;
	track_issue_commented?: boolean;
	track_pr_opened?: boolean;
	track_pr_closed?: boolean;
	track_pr_merged?: boolean;
	track_pr_commented?: boolean;
	track_release_published?: boolean;
	// Planka-specific
	track_card_created?: boolean;
	track_card_updated?: boolean;
	track_card_moved?: boolean;
	track_card_deleted?: boolean;
	track_card_commented?: boolean;
	track_comment_updated?: boolean;
	track_board_created?: boolean;
	track_board_updated?: boolean;
	track_board_deleted?: boolean;
	track_list_created?: boolean;
	track_list_updated?: boolean;
	track_list_deleted?: boolean;
	track_attachment_created?: boolean;
	track_card_label_added?: boolean;
	track_task_completed?: boolean;
	// Scheduler-specific
	track_scheduled_message?: boolean;
	// NUT-specific
	track_ups_online?: boolean;
	track_ups_on_battery?: boolean;
	track_ups_low_battery?: boolean;
	track_ups_battery_restored?: boolean;
	track_ups_comms_lost?: boolean;
	track_ups_comms_restored?: boolean;
	track_ups_replace_battery?: boolean;
	track_ups_overload?: boolean;
	// Webhook-specific
	track_webhook_received?: boolean;
	notify_favorites_only: boolean;
	include_tags: boolean;
	include_asset_details: boolean;
	max_assets_to_show: number;
	assets_order_by: string;
	assets_order: string;
	periodic_enabled: boolean;
	periodic_interval_days: number;
	periodic_start_date: string;
	periodic_times: string;
	scheduled_enabled: boolean;
	scheduled_times: string;
	scheduled_collection_mode: string;
	scheduled_limit: number;
	scheduled_favorite_only: boolean;
	scheduled_asset_type: string;
	scheduled_min_rating: number;
	scheduled_order_by: string;
	scheduled_order: string;
	memory_enabled: boolean;
	memory_source: string;
	memory_times: string;
	memory_collection_mode: string;
	memory_limit: number;
	memory_favorite_only: boolean;
	memory_asset_type: string;
	memory_min_rating: number;
	quiet_hours_enabled: boolean;
	quiet_hours_start: string | null;
	quiet_hours_end: string | null;
	created_at: string;
}

export interface TemplateConfig {
	id: number;
	user_id: number;
	provider_type: string;
	name: string;
	description: string;
	icon: string;
	slots: Record<string, Record<string, string>>;
	date_format: string;
	date_only_format: string;
	created_at: string;
}

/**
 * Lifecycle marker the backend stores in ``EventLog.details.dispatch_status``
 * when a notification doesn't take the immediate-deliver happy path.
 *
 * * ``deferred`` — held back by quiet hours; ``deferred_until`` carries the
 *   UTC ISO datetime at which a drain job will fire.
 * * ``delivered_after_quiet_hours`` — a drain successfully dispatched the
 *   originally-deferred event. ``original_event_log_id`` points back at the
 *   row from when the event was first detected.
 * * ``deferred_then_dropped`` — drain time arrived but the link/target was
 *   removed, disabled, or otherwise unsendable. See ``reason`` for detail.
 * * ``deferred_then_failed`` — drain dispatched but the target returned an
 *   error; ``reason`` carries the truncated provider error.
 * * ``suppressed_quiet_hours_nondeferrable`` — wall-clock event type (e.g.
 *   ``scheduled_message``) caught by quiet hours, dropped on principle.
 */
export type DispatchStatus =
	| 'deferred'
	| 'delivered_after_quiet_hours'
	| 'deferred_then_dropped'
	| 'deferred_then_failed'
	| 'suppressed_quiet_hours_nondeferrable';

export interface EventLog {
	id: number;
	event_type: string;
	collection_id: string;
	collection_name: string;
	tracker_id?: number | null;
	tracker_name: string;
	provider_name: string;
	provider_id: number | null;
	action_id?: number | null;
	action_name?: string;
	command_tracker_id?: number | null;
	command_tracker_name?: string;
	telegram_bot_id?: number | null;
	bot_name?: string;
	assets_count: number;
	details: Record<string, any> & {
		dispatch_status?: DispatchStatus;
		deferred_until?: string;
		original_event_log_id?: number | null;
		deferred_for_seconds?: number;
	};
	created_at: string;
}

export interface WebhookPayloadLog {
	id: number;
	provider_id: number;
	method: string;
	headers: Record<string, string>;
	body: Record<string, any>;
	status: 'matched' | 'unmatched' | 'error';
	extracted_fields: Record<string, any>;
	error_message: string;
	created_at: string;
}

export interface User {
	id: number;
	username: string;
	role: string;
	created_at: string;
}

export interface CommandConfig {
	id: number;
	user_id: number;
	provider_type: string;
	name: string;
	icon: string;
	enabled_commands: string[];
	locale: string;
	response_mode: string;
	default_count: number;
	rate_limits: Record<string, number>;
	command_template_config_id: number | null;
	created_at: string;
}

export interface CommandTemplateConfig {
	id: number;
	user_id: number;
	provider_type: string;
	name: string;
	description: string;
	icon: string;
	slots: Record<string, Record<string, string>>;
	created_at: string;
}

export interface CommandTracker {
	id: number;
	user_id: number;
	name: string;
	icon: string;
	provider_id: number;
	command_config_id: number;
	enabled: boolean;
	created_at: string;
}

export interface ActionRule {
	id: number;
	action_id: number;
	name: string;
	rule_config: Record<string, any>;
	enabled: boolean;
	order: number;
	created_at: string;
}

export interface Action {
	id: number;
	name: string;
	icon: string;
	provider_id: number;
	action_type: string;
	config: Record<string, any>;
	schedule_type: string;
	schedule_interval: number;
	schedule_cron: string;
	enabled: boolean;
	last_run_at: string | null;
	last_run_status: string;
	rules: ActionRule[];
	created_at: string;
}

export interface ActionExecution {
	id: number;
	action_id: number;
	started_at: string;
	finished_at: string | null;
	status: string;
	rules_processed: number;
	rules_succeeded: number;
	rules_failed: number;
	total_items_affected: number;
	summary: Record<string, any>;
	error: string;
	trigger: string;
}

export interface ActionTypeInfo {
	key: string;
	name: string;
	description: string;
	provider_type: string;
}

export interface DashboardStatus {
	providers: number;
	trackers: { total: number; active: number };
	targets: number;
	total_events: number;
	recent_events: EventLog[];
	command_trackers?: number;
	/** Provider name → total event count across ALL events matching the
	 *  current filters (ignores pagination). Powers the "On watch" deck
	 *  when the user opts out of page-scoped stats. */
	provider_event_counts?: Record<string, number>;
}

export type ReleaseProviderKind = 'disabled' | 'gitea' | 'github';

export interface ReleaseStatus {
	provider: ReleaseProviderKind;
	current: string;
	latest: string | null;
	latest_tag: string | null;
	latest_url: string | null;
	latest_name: string | null;
	latest_body: string | null;
	latest_published_at: string | null;
	latest_prerelease: boolean;
	checked_at: string | null;
	update_available: boolean;
	error: string | null;
}

export interface ReleaseTestResult {
	ok: boolean;
	info: {
		tag: string;
		version: string;
		name: string | null;
		url: string | null;
		published_at: string | null;
		prerelease: boolean;
	} | null;
	error: string | null;
}
