export interface MenuCategory {
  id: string;
  label: string;
  icon: string;
}

export interface UserInfo {
  email: string;
  name: string;
}

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

export interface ChatMessage {
  id: string;
  role: "user" | "assistant";
  text: string;
  timestamp: number;
  user_email?: string;
}

export type AgentStatus = "thinking" | "tool_use" | "tool_done" | "tool_error" | "idle";

export interface WSMessage {
  type: "reply" | "notify" | "error" | "status";
  conversation_id?: string;
  text: string;
  timestamp: number;
  level?: string;
  status?: AgentStatus;
  tool_name?: string;
}

export interface AgentConfig {
  name: string;
  displayName: string;
  description: string;
  port: number;
  online?: boolean;
}

export interface AgentServerConfig extends AgentConfig {
  secret: string;
  channelUrl: string;
}

export interface McpServerInfo {
  name: string;
  command: string;
  args: string[];
}

export interface AgentDetails extends AgentConfig {
  claudeMd: string;
  mcpServers: McpServerInfo[];
  enabled: boolean;
  fifo: string;
  channelDir: string;
}

// ─── Skills Registry Types ───────────────────────────────────────────

export interface SkillDefinition {
  name: string;
  description: string;
  version: string;
  file: string;
  allowedTools: string[];
  agents: string[];
  fileNotFound?: boolean;
}

export interface McpServerDefinition {
  name: string;
  description: string;
  command: string;
  args: string[];
  env: Record<string, string>;
  agents: string[];
}

export interface SkillsRegistry {
  skills: Record<string, SkillDefinition>;
  mcpServers: Record<string, McpServerDefinition>;
}

export interface SubcategoryDef {
  id: string;
  label: string;
  icon: string;
}

// ─── Apps Registry Types ────────────────────────────────────────────

export interface AppHost {
  id: string;
  name: string;
  address: string;
  description?: string;
}

export interface AppCategory {
  id: string;
  name: string;
  description?: string;
}

export interface AppIcon {
  type: "lucide" | "emoji" | "upload";
  value: string;
}

export interface AppEntry {
  id: string;
  name: string;
  url: string;
  hostId: string;
  categoryId: string;
  description?: string;
  icon: AppIcon;
  color: string;
  createdAt: string;
  updatedAt: string;
}

export interface AppsRegistry {
  hosts: Record<string, AppHost>;
  categories: Record<string, AppCategory>;
  apps: Record<string, AppEntry>;
  viewOrders: Record<string, string[]>;
}

// iMessage Bridge types
export interface ImessageContact {
  id: string;
  name: string;
  identifier: string;
}

export interface ImessageBridgeSettings {
  polling_mode: "frequent" | "balanced" | "relaxed";
  send_delay_seconds: number;
  contacts: ImessageContact[];
}

export interface ImessageBridgeHealth {
  status: string;
  polling_mode: string;
  send_delay_seconds: number;
  contacts_count: number;
}

export interface ImessageHistoryMessage {
  id: number;
  contact: string;
  direction: "inbound" | "outbound";
  text: string;
  timestamp: number;
}

// ─── Backup Service Types ────────────────────────────────────────────

export interface LocalConfig {
  path: string;
}

export interface NasConfig {
  protocol: "smb" | "nfs";
  sharePath: string;
  mountPoint: string;
  username?: string;
  password?: string;
}

export interface SftpConfig {
  host: string;
  port: number;
  username: string;
  authMethod: "key" | "password";
  keyPath?: string;
  password?: string;
  remotePath: string;
}

export interface IscsiConfig {
  targetIqn: string;
  portalAddress: string;
  port: number;
  mountPoint: string;
}

export interface BackupDestination {
  id: string;
  name: string;
  type: "local" | "nas" | "sftp" | "iscsi";
  config: LocalConfig | NasConfig | SftpConfig | IscsiConfig;
}

export interface BackupSchedule {
  preset: "disabled" | "hourly" | "daily" | "weekly" | "monthly";
  time: string;
  dayOfWeek?: number;
  dayOfMonth?: number;
  backupType: "full" | "incremental" | "differential";
  destinationId: string;
}

export interface RetentionPolicy {
  dailyCount: number;
  weeklyCount: number;
  monthlyCount: number;
}

export interface BackupSettings {
  schedule: BackupSchedule;
  retention: RetentionPolicy;
  defaultDestinationId: string;
}

export interface BackupHealth {
  status: "online" | "offline";
  uptime: number;
  version: string;
  lastBackup: BackupHistoryEntry | null;
  nextScheduledBackup: string | null;
}

export interface BackupStatus {
  state: "idle" | "running";
  currentBackupId?: string;
  startedAt?: string;
  backupType?: "full" | "incremental" | "differential";
  destinationId?: string;
}

export interface BackupHistoryEntry {
  id: string;
  type: "full" | "incremental" | "differential";
  timestamp: string;
  parentBackupId: string | null;
  fileCount: number;
  sizeBytes: number;
  destination: string;
  status: "completed" | "failed";
  error: string | null;
}

export interface BackupRunRequest {
  backupType: "full" | "incremental" | "differential";
  destinationId?: string;
}

// ─── Tools Menu Registry Types ──────────────────────────────────────

export interface ToolType {
  id: string;
  name: string;
  description?: string;
  icon: AppIcon;
}

export interface ToolConfigField {
  key: string;
  label: string;
  type: "text" | "select" | "number" | "password" | "textarea";
  options?: string[];
  placeholder?: string;
  required?: boolean;
}

export interface ToolTypeDefinition {
  typeId: string;
  configFields: ToolConfigField[];
}

export interface ToolEntry {
  id: string;
  name: string;
  typeId: string;
  address?: string;
  description?: string;
  icon: AppIcon;
  color: string;
  config: Record<string, string>;
  status: "online" | "offline";
  createdAt: string;
  updatedAt: string;
}

export interface ToolsMenuRegistry {
  types: Record<string, ToolType>;
  typeDefinitions: Record<string, ToolTypeDefinition>;
  tools: Record<string, ToolEntry>;
  viewOrders: Record<string, string[]>;
}

// --- Calendar types ---

export interface CalendarInfo {
  id: string;
  name: string;
  type: "system" | "family";
  color: string;
  created_at: string;
}

export interface CalendarEvent {
  id: string;
  calendar_id: string;
  title: string;
  description: string | null;
  notes: string | null;
  start_time: string;
  end_time: string;
  all_day: number;
  location: string | null;
  recurrence_rule: string | null;
  created_by: string;
  created_at: string;
  updated_at: string;
  people: CalendarEventPerson[];
  reminders: CalendarReminder[];
}

export interface CalendarEventPerson {
  event_id: string;
  person_name: string;
  invite_preference: "calendar_invite" | "reminder_only" | "both" | "none";
  invite_sent: number;
  reminder_sent: number;
  google_event_id: string | null;
}

export interface CalendarReminder {
  id: string;
  event_id: string;
  person_name: string;
  remind_at: string;
  sent: number;
  sent_at: string | null;
}

export interface GoogleCachedEvent {
  id: string;
  google_calendar_id: string;
  google_calendar_name: string;
  google_event_id: string;
  title: string;
  description: string | null;
  start_time: string;
  end_time: string;
  all_day: number;
  location: string | null;
  person_email: string;
  last_synced: string;
}

export interface CalendarHealth {
  status: "online" | "offline";
  uptime: number;
  version: string;
}

export type PersonFilter = string;

export const PERSON_COLORS: Record<string, string> = {
  person1: "#f59e0b",
  person2: "#ec4899",
  person3: "#8b5cf6",
};

export const SYSTEM_CALENDAR_COLOR = "#3b82f6"; // blue

// --- Status types ---

export interface StatusHostInfo {
  id: string;
  name: string;
  ip: string;
  status: "online" | "offline" | "degraded";
  cpu_percent: number;
  memory_used_bytes: number;
  memory_total_bytes: number;
  disk_used_bytes: number;
  disk_total_bytes: number;
  uptime_seconds: number;
  os_version: string;
  pending_updates: number;
  pending_security_updates: number;
  last_checked: string;
}

export interface StatusServiceInfo {
  id: string;
  name: string;
  host: string;
  status: "running" | "stopped" | "errored";
  uptime_seconds: number;
  restart_count: number;
  memory_bytes: number;
  type: "pm2" | "systemd";
  last_checked: string;
}

export interface StatusNetworkInfo {
  id: string;
  name: string;
  status: "online" | "offline" | "unknown";
  latency_ms: number | null;
  type: "internet" | "backup_internet" | "lan" | "router";
  last_checked: string;
}

export interface StatusSiteInfo {
  id: string;
  name: string;
  url: string;
  status: "up" | "down" | "degraded";
  response_time_ms: number;
  http_status_code: number;
  ssl_expiry_date: string | null;
  ssl_days_remaining: number | null;
  last_checked: string;
}

export interface StatusSnapshot {
  hosts: StatusHostInfo[];
  services: StatusServiceInfo[];
  network: StatusNetworkInfo[];
  sites: StatusSiteInfo[];
  last_updated: string;
}

export interface StatusSummaryInfo {
  hosts: { green: number; amber: number; red: number };
  services: { green: number; amber: number; red: number };
  network: { green: number; amber: number; red: number };
  sites: { green: number; amber: number; red: number };
  overall: "healthy" | "degraded" | "critical";
}

export interface StatusAlertInfo {
  category: "host" | "service" | "network" | "site";
  id: string;
  name: string;
  severity: "amber" | "red";
  message: string;
  last_checked: string;
}

// ─── Music Library Types ────────────────────────────────────────────

export interface Song {
  isrc: string;
  title: string;
  artist: string;
  album: string | null;
  duration_ms: number;
  file_path: string | null;
  artwork_url: string | null;
  captured_at: string | null;
  status: "captured" | "pending";
  playlists?: { id: number; name: string; source: string; position: number }[];
}

export interface Playlist {
  id: number;
  name: string;
  description: string | null;
  source: "apple_music" | "custom";
  source_url: string | null;
  created_at: string;
  total_songs: number;
  captured_songs: number;
  songs?: (Song & { position: number })[];
}

export interface QueueItem {
  isrc: string;
  playlist_id: number | null;
  position: number;
  status: "queued" | "capturing" | "completed" | "failed";
  queued_at: string;
  started_at: string | null;
  completed_at: string | null;
  error: string | null;
  title: string;
  artist: string;
  duration_ms: number;
  artwork_url: string | null;
  elapsed_ms?: number;
  progress_pct?: number;
}

export interface QueueStats {
  total: number;
  completed: number;
  remaining: number;
  total_duration_ms: number;
  completed_duration_ms: number;
  remaining_duration_ms: number;
}

export interface QueueState {
  current: QueueItem | null;
  songs: QueueItem[];
  completed: QueueItem[];
  stats: QueueStats;
}

