/**
 * Shared TypeScript interfaces matching backend API responses.
 */

export interface MediaCounts {
	movies: number;
	series: number;
	episodes: number;
}

export interface LibraryCount {
	library: string;
	type: string;
	movies: number;
	series: number;
	episodes: number;
	total: number;
}

export interface UserDirectoryItem {
	jellyfin_id: string;
	username: string;
	display_name: string;
	email: string;
	email_source: string;
	avatar: string;
	avatar_source: string;
	contactable: boolean;
	source: string;
	source_summary: string;
	name_source: string;
	access_source: string;
	jellyseerr_user_id: number | null;
	jellyseerr_username: string;
	user_type: number | null;
	user_type_label: string;
	role: string;
	permissions: number;
	permissions_label: string;
	request_count: number | null;
}

export interface UserDirectoryResponse {
	items: UserDirectoryItem[];
	total: number;
	jellyseerr_configured: boolean;
	jellyseerr_available: boolean;
	jellyseerr_error: string;
	jellyseerr_jellyfin_user_count: number;
	jellyseerr_user_count: number;
	enriched_count: number;
}

export interface UserMessageResponse {
	status: string;
	request_id: string;
	subject: string;
	from_address: string;
	recipient_count: number;
	attachment_count: number;
	recipient_labels: string[];
	skipped: Array<{ jellyfin_id: string; reason: string }>;
}

export interface UserMessageQueueStatus {
	state: "idle" | "busy" | "error" | "stopped";
	worker_running: boolean;
	stop_requested: boolean;
	pending_count: number;
	active_request_id: string | null;
	last_request_id: string | null;
	last_result: string | null;
	last_error: string;
	last_error_at: number | null;
	last_success_at: number | null;
	last_activity_at: number | null;
	sent_count: number;
	failed_count: number;
}

export interface NowPlayingSession {
	user: string;
	title: string;
	type: string;
	state: string;
	transcoding: string;
	transcoding_type: string;
	device: string;
	session_id: string;
}

export interface SSHKey {
	id: string;
	name: string;
	private_key_set: boolean;
	passphrase_set: boolean;
	public_key: string;
	fingerprint: string;
	usage_count: number;
	notes: string;
}

export interface SSHKeyInput {
	id?: string | null;
	name: string;
	private_key: string;
	passphrase: string;
	public_key: string;
	fingerprint: string;
	notes: string;
}

export interface SSHKeyGenerated {
	name: string;
	private_key: string;
	passphrase: string;
	notes: string;
	public_key: string;
	fingerprint: string;
	usage_count: number;
}

export interface SavedTask {
	id: string;
	name: string;
	task_type: "shell" | "python";
	content: string;
	enabled: boolean;
	default_service_id: string;
	notes: string;
	created_at: number;
	updated_at: number;
}

export interface SavedTaskInput {
	id?: string | null;
	name: string;
	task_type: "shell" | "python";
	content: string;
	enabled: boolean;
	default_service_id: string;
	notes: string;
}

export interface SavedTaskRun {
	id: string;
	task_id: string;
	service_id: string;
	status: "success" | "failure" | "error" | "timeout" | string;
	exit_status: number | null;
	created_at: number;
	duration_ms: number;
	stdout_tail: string;
	stderr_tail: string;
	error: string;
}

export interface MonitoringMachine {
	id: string;
	name: string;
	mode: "local" | "ssh";
	enabled: boolean;
	services: string[];
	host: string;
	port: number;
	username: string;
	key_directory: string;
	key_name: string;
	ssh_key_id: string;
	ssh_private_key_set: boolean;
	ssh_private_key_passphrase_set: boolean;
	password_set: boolean;
	notes: string;
}

export interface MonitoringMachineInput {
	id?: string | null;
	name: string;
	mode: "local" | "ssh";
	enabled: boolean;
	services: string[];
	host: string;
	port: number;
	username: string;
	key_directory: string;
	key_name: string;
	ssh_key_id: string;
	ssh_private_key: string;
	ssh_private_key_passphrase: string;
	password: string;
	notes: string;
}

export interface ResetLocalDatabaseInput {
	confirm_phrase: string;
	acknowledge_settings_loss: boolean;
	acknowledge_media_index_loss: boolean;
	acknowledge_irreversible: boolean;
}

export interface ResetLocalDatabaseResponse {
	status: string;
	settings_db_removed: boolean;
	media_index_removed: boolean;
	settings_files: string[];
	media_index_files: string[];
}

export interface SSHValidationResult {
	status: string;
	message: string;
	host: string;
	port: number;
	known_hosts_updated: boolean;
}

export interface AppVersionInfo {
	app: string;
	backend_version: string;
	backend_build: string;
	backend_label: string;
}

export interface MediaIndexStatus {
	exists: boolean;
	item_count: number;
	updated_at: number | null;
	updated_at_label: string;
	build_duration_seconds: number | null;
	build_running: boolean;
	build_stage: string;
	build_message: string;
	build_progress: number | null;
	build_items_processed: number;
	build_items_total: number;
	build_current_library: string;
	build_library_index: number;
	build_libraries_total: number;
	build_library_progress: number | null;
	build_library_items_processed: number;
	build_library_items_total: number;
	build_elapsed_seconds: number | null;
	build_eta_seconds: number | null;
	build_library_elapsed_seconds: number | null;
	build_library_eta_seconds: number | null;
	build_cancel_requested: boolean;
	build_pid: number | null;
	build_error: string;
}

export interface MediaIndexActionResponse {
	status: string;
	build_running: boolean;
	build_stage: string;
	build_message: string;
	build_progress: number | null;
	build_items_processed: number;
	build_items_total: number;
	build_current_library: string;
	build_library_index: number;
	build_libraries_total: number;
	build_library_progress: number | null;
	build_library_items_processed: number;
	build_library_items_total: number;
	build_elapsed_seconds: number | null;
	build_eta_seconds: number | null;
	build_library_elapsed_seconds: number | null;
	build_library_eta_seconds: number | null;
	build_cancel_requested: boolean;
	build_pid: number | null;
	build_error: string;
}

export interface MediaItem {
	id: string;
	title: string;
	series: string;
	season: string;
	episode: number | null;
	type: string;
	year: number | null;
	runtime_min: number | null;
	size: string;
	bitrate: string;
	hdr: string;
	video: string;
	resolution: string;
	date_added: string;
	library: string;
	path: string;
}

export interface MediaQueryResponse {
	items: MediaItem[];
	total: number;
	limit: number;
	offset: number;
}

export interface FileEntry {
	type: string;
	size: number;
	mtime: number;
	name: string;
}

export interface DirectoryListing {
	path: string;
	entries: FileEntry[];
	count: number;
}

export interface JobTemplate {
	key: string;
	name: string;
	description: string;
}

export interface JobResult {
	job_key: string;
	path: string;
	exit_status: number;
	stdout: string;
	stderr: string;
}

export interface ResolvedPath {
	original: string;
	resolved: string;
}

export interface DashboardShortcut {
	id: string;
	label: string;
	shortcut_type: "website" | "action" | "user";
	enabled: boolean;
	icon: string;
	url: string;
	task_id: string;
	machine_id: string;
	user_id: string;
	notes: string;
	created_at: number;
	updated_at: number;
}

export interface DashboardShortcutInput {
	id?: string | null;
	label: string;
	shortcut_type: "website" | "action" | "user";
	enabled: boolean;
	icon: string;
	url: string;
	task_id: string;
	machine_id: string;
	user_id: string;
	notes: string;
}

export interface AlertmanagerAlert {
	name: string;
	severity: string;
	category: string;
	job_name: string;
	summary: string;
	description: string;
	active_since: string;
	state: string;
	labels: Record<string, string>;
}

export interface AlertmanagerAlertSummary {
	total: number;
	by_severity: Record<string, number>;
	alerts: AlertmanagerAlert[];
	error?: string;
}

export interface AlertmanagerStatus {
	up: boolean;
	version: string;
	uptime: string;
	name: string;
	peers: string[];
	service_id?: string;
	error?: string | null;
}

export interface PrometheusStatus {
	up: boolean;
	version: string;
	service_id: string;
	name: string;
	error?: string | null;
}

export interface PrometheusTarget {
	labels: Record<string, string>;
	targets: string[];
}

export interface WidgetInstance {
	id: string;
	service_id: string | null;
	widget_kind: string;
	title: string;
	config: Record<string, unknown>;
	enabled: boolean;
	sort_order: number;
	created_at: number;
	updated_at: number;
}

export interface WidgetInstanceInput {
	id?: string | null;
	service_id: string | null;
	widget_kind: string;
	title: string;
	config: Record<string, unknown>;
	enabled: boolean;
	sort_order: number;
}

export interface WidgetDataResponse {
	widget_id: string;
	data: Record<string, unknown> | null;
	error: string | null;
	fetched_at: number;
}

export interface SecretFieldInfo {
	key: string;
	label: string;
	required: boolean;
	helper?: string | null;
}

export interface ServiceWidgetKindInfo {
	kind: string;
	name: string;
	description: string;
	config_schema: Record<string, unknown>;
	default_config: Record<string, unknown>;
	refresh_interval_ms: number;
}

export interface ServiceTypeInfo {
	service_type: string;
	name: string;
	description: string;
	config_schema: Record<string, unknown>;
	secret_fields: SecretFieldInfo[];
	widget_kinds: ServiceWidgetKindInfo[];
}

export interface ServiceInstance {
	id: string;
	service_type: string;
	name: string;
	config: Record<string, unknown>;
	secrets_set: Record<string, boolean>;
	enabled: boolean;
	created_at: number;
	updated_at: number;
}

export interface ServiceInstanceInput {
	id?: string | null;
	service_type: string;
	name: string;
	config: Record<string, unknown>;
	secrets: Record<string, string>;
	enabled: boolean;
}

export interface BuiltinWidgetKindInfo {
	kind: string;
	name: string;
	description: string;
	config_schema: Record<string, unknown>;
	default_config: Record<string, unknown>;
	refresh_interval_ms: number;
}
