import { invoke } from "@tauri-apps/api/core"
import { listen, UnlistenFn } from "@tauri-apps/api/event"

export interface themeConfig {
  name: string
  id: string
  mode: "light" | "dark"
  config: {
    tdesign: Record<string, string>
    custom: Record<string, string>
  }
}

export interface settingChange {
  key: string
  value: any
  oldValue: any
}

export interface dataUpdatedEvent {
  category?: "events" | "students" | "reasons" | "all"
  source?: string
}

export interface autoScoreTrigger {
  event: string
  value?: string | null
}

export interface autoScoreAction {
  event: string
  value?: string | null
}

export interface autoScoreExecutionConfig {
  cooldownMinutes?: number | null
  maxRunsPerDay?: number | null
  maxScoreDeltaPerDay?: number | null
  startAt?: string | null
}

export interface autoScoreExecutionBatch {
  id: string
  ruleId: number
  ruleName: string
  runAt: string
  affectedStudents: number
  affectedStudentNames: string[]
  createdEventIds: number[]
  addedStudentTagIds: number[]
  rewardRedemptionIds?: number[]
  scoreDeltaTotal: number
  settled: boolean
  rolledBack: boolean
  rollbackAt?: string | null
}

export interface autoScoreBackfillItem {
  ruleId: number
  runs: number
}

export interface autoScoreBackfillResult {
  appliedRules: number
  appliedRuns: number
  affectedStudents: number
  createdEvents: number
  scoreDeltaTotal: number
}

export interface autoScoreRule {
  id: number
  name: string
  enabled: boolean
  studentNames: string[]
  triggers: autoScoreTrigger[]
  triggerTree?: any | null
  actions: autoScoreAction[]
  execution?: autoScoreExecutionConfig
  lastExecuted?: string | null
}

export type settingsKey =
  | "is_wizard_completed"
  | "log_level"
  | "window_zoom"
  | "search_keyboard_layout"
  | "disable_search_keyboard"
  | "font_family"
  | "themes_custom"
  | "auto_score_enabled"
  | "auto_score_rules"
  | "auto_score_batches"
  | "current_theme_id"
  | "dashboards_config"
  | "pg_connection_string"
  | "pg_connection_status"
  | "mobile_bottom_nav_items"

export interface settingsSpec {
  is_wizard_completed: boolean
  log_level: string
  window_zoom: number
  search_keyboard_layout: "t9" | "qwerty26"
  disable_search_keyboard: boolean
  font_family: string
  themes_custom: themeConfig[]
  auto_score_enabled: boolean
  auto_score_rules: autoScoreRule[]
  auto_score_batches: autoScoreExecutionBatch[]
  current_theme_id: string
  dashboards_config: any[]
  pg_connection_string: string
  pg_connection_status: {
    connected: boolean
    type: "sqlite" | "postgresql"
    error?: string
  }
  mobile_bottom_nav_items: string[]
}

export interface pluginRuntimeModule {
  id: string
  name: string
  version: string
  description?: string | null
  author?: string | null
  main: string
  code: string
  permissions: string[]
}

const api = {
  // Theme
  getThemes: (): Promise<{ success: boolean; data: themeConfig[] }> => invoke("theme_list"),
  getCurrentTheme: (): Promise<{ success: boolean; data: themeConfig }> => invoke("theme_current"),
  setTheme: (themeId: string): Promise<{ success: boolean }> => invoke("theme_set", { themeId }),
  saveTheme: (theme: themeConfig): Promise<{ success: boolean }> => invoke("theme_save", { theme }),
  deleteTheme: (themeId: string): Promise<{ success: boolean }> =>
    invoke("theme_delete", { themeId }),
  onThemeChanged: (callback: (theme: themeConfig) => void): Promise<UnlistenFn> => {
    return listen<themeConfig | { theme?: themeConfig }>("theme:updated", (event) => {
      const payload = event.payload as themeConfig | { theme?: themeConfig } | undefined
      const theme =
        payload && typeof payload === "object" && "theme" in payload
          ? payload.theme
          : (payload as themeConfig | undefined)
      if (theme) callback(theme)
    })
  },

  // DB - Student
  queryStudents: (params?: any): Promise<{ success: boolean; data: any[] }> =>
    invoke("student_query", { params }),
  createStudent: (data: {
    name: string
    group_name?: string
  }): Promise<{ success: boolean; data?: number; message?: string }> =>
    invoke("student_create", { data }),
  updateStudent: (id: number, data: any): Promise<{ success: boolean }> =>
    invoke("student_update", { id, data }),
  deleteStudent: (id: number): Promise<{ success: boolean }> => invoke("student_delete", { id }),
  importStudentsFromXlsx: (params: {
    names: string[]
  }): Promise<{ success: boolean; data: { inserted: number; skipped: number; total: number } }> =>
    invoke("student_import_from_xlsx", { params }),
  fetchBanYouClassrooms: (params: {
    cookie: string
  }): Promise<{
    success: boolean
    data?: {
      classrooms: Array<{
        classId: string
        classNickName: string
        invitationCode?: string | null
        masterName?: string | null
        studentsNum?: number | null
        praiseCount?: number | null
        classAvatarPath?: string | null
        classAvatarDataUrl?: string | null
        isOwn?: boolean | null
      }>
      administrativeGroups: Array<{
        classId: string
        classNickName: string
        invitationCode?: string | null
        masterName?: string | null
        studentsNum?: number | null
        praiseCount?: number | null
        classAvatarPath?: string | null
        classAvatarDataUrl?: string | null
        isOwn?: boolean | null
      }>
    }
    message?: string
  }> => invoke("student_fetch_banyou_classrooms", { params }),
  fetchBanYouClassroomDetail: (params: {
    cookie: string
    classId: string
    teamPlanId?: number
  }): Promise<{
    success: boolean
    data?: {
      medals: Array<{
        key?: string
        uid?: string
        name: string
        type?: number
        medalType?: number
        value?: number
      }>
      students: Array<{
        studentId: string
        studentName: string
        avatar?: string | null
      }>
      teams: Array<{
        teamId: string
        teamName: string
        students: Array<{
          studentId: string
          studentName: string
        }>
      }>
      ungroupedStudents: Array<{
        studentId: string
        studentName: string
      }>
      teamPlanIdUsed?: number
      teamPlans?: Array<{
        teamPlanId: number
        name?: string
      }>
      teamPlanSource?: string
    }
    message?: string
  }> => invoke("student_fetch_banyou_classroom_detail", { params }),

  // DB - Tags
  tagsGetAll: (): Promise<{ success: boolean; data: any[] }> => invoke("tags_get_all"),
  tagsGetByStudent: (studentId: number): Promise<{ success: boolean; data: any[] }> =>
    invoke("tags_get_by_student", { studentId }),
  tagsCreate: (name: string): Promise<{ success: boolean; data: any }> =>
    invoke("tags_create", { name }),
  tagsDelete: (id: number): Promise<{ success: boolean }> => invoke("tags_delete", { id }),
  tagsUpdateStudentTags: (studentId: number, tagIds: number[]): Promise<{ success: boolean }> =>
    invoke("tags_update_student_tags", { studentId, tagIds }),

  // DB - Reason
  queryReasons: (): Promise<{ success: boolean; data: any[] }> => invoke("reason_query"),
  createReason: (data: any): Promise<{ success: boolean; data?: number; message?: string }> =>
    invoke("reason_create", { data }),
  updateReason: (id: number, data: any): Promise<{ success: boolean }> =>
    invoke("reason_update", { id, data }),
  deleteReason: (id: number): Promise<{ success: boolean }> => invoke("reason_delete", { id }),

  // DB - Reward
  rewardSettingQuery: (): Promise<{ success: boolean; data: any[] }> =>
    invoke("reward_setting_query"),
  rewardSettingCreate: (data: {
    name: string
    cost_points: number
  }): Promise<{ success: boolean; data?: number; message?: string }> =>
    invoke("reward_setting_create", { data }),
  rewardSettingUpdate: (id: number, data: any): Promise<{ success: boolean; message?: string }> =>
    invoke("reward_setting_update", { id, data }),
  rewardSettingDelete: (id: number): Promise<{ success: boolean; message?: string }> =>
    invoke("reward_setting_delete", { id }),
  rewardRedeem: (data: {
    student_name: string
    reward_id: number
  }): Promise<{
    success: boolean
    data?: { redemption_id: number; remaining_reward_points: number }
    message?: string
  }> => invoke("reward_redeem", { data }),
  rewardRedemptionQuery: (params?: {
    limit?: number
  }): Promise<{ success: boolean; data: any[] }> => invoke("reward_redemption_query", { params }),

  // DB - Event
  queryEvents: (params?: { limit?: number }): Promise<{ success: boolean; data: any[] }> =>
    invoke("event_query", { params }),
  createEvent: (data: {
    student_name?: string
    reason_content?: string
    delta: number
    studentName?: string
    reasonContent?: string
  }): Promise<{ success: boolean; data?: number; message?: string }> => {
    const normalized = {
      student_name: String(data.student_name ?? data.studentName ?? "").trim(),
      reason_content: String(data.reason_content ?? data.reasonContent ?? "").trim(),
      delta: Number(data.delta),
    }
    return invoke("event_create", { data: normalized })
  },
  deleteEvent: (uuid: string): Promise<{ success: boolean }> => invoke("event_delete", { uuid }),
  queryEventsByStudent: (params: {
    student_name?: string
    limit?: number
    start_time?: string
    studentName?: string
    startTime?: string
  }): Promise<{ success: boolean; data: any[] }> =>
    invoke("event_query_by_student", {
      params: {
        student_name: String(params.student_name ?? params.studentName ?? "").trim(),
        limit: params.limit,
        start_time: params.start_time ?? params.startTime,
      },
    }),
  queryLeaderboard: (params: {
    range: "today" | "week" | "month"
  }): Promise<{ success: boolean; data: { startTime: string; rows: any[] } }> =>
    invoke("leaderboard_query", { params }),
  boardQuerySql: (params: {
    sql: string
    limit?: number
  }): Promise<{ success: boolean; data: any[]; message?: string }> =>
    invoke("board_query_sql", { params }),
  boardGetConfigs: (): Promise<{ success: boolean; data: any[]; message?: string }> =>
    invoke("board_get_configs"),
  boardSaveConfigs: (configs: any[]): Promise<{ success: boolean; message?: string }> =>
    invoke("board_save_configs", { configs }),

  // Settlement
  querySettlements: (): Promise<{ success: boolean; data: any[] }> => invoke("db_settlement_query"),
  createSettlement: (): Promise<{ success: boolean; data: any }> => invoke("db_settlement_create"),
  querySettlementLeaderboard: (params: {
    settlementId: number
  }): Promise<{ success: boolean; data: any }> => invoke("db_settlement_leaderboard", { params }),

  // Auto Score
  autoScoreGetRules: (): Promise<{
    success: boolean
    data?: autoScoreRule[]
    message?: string
  }> => invoke("auto_score_get_rules"),
  autoScoreAddRule: (rule: {
    name: string
    enabled: boolean
    studentNames: string[]
    triggers: autoScoreTrigger[]
    triggerTree?: any | null
    actions: autoScoreAction[]
    execution?: autoScoreExecutionConfig
  }): Promise<{ success: boolean; data?: number; message?: string }> =>
    invoke("auto_score_add_rule", { rule }),
  autoScoreUpdateRule: (rule: {
    id: number
    name: string
    enabled: boolean
    studentNames: string[]
    triggers: autoScoreTrigger[]
    triggerTree?: any | null
    actions: autoScoreAction[]
    execution?: autoScoreExecutionConfig
  }): Promise<{ success: boolean; data?: boolean; message?: string }> =>
    invoke("auto_score_update_rule", { rule }),
  autoScoreDeleteRule: (
    ruleId: number
  ): Promise<{ success: boolean; data?: boolean; message?: string }> =>
    invoke("auto_score_delete_rule", { ruleId }),
  autoScoreToggleRule: (params: {
    ruleId: number
    enabled: boolean
  }): Promise<{ success: boolean; data?: boolean; message?: string }> =>
    invoke("auto_score_toggle_rule", { params }),
  autoScoreGetStatus: (): Promise<{
    success: boolean
    data?: {
      enabled: boolean
    }
    message?: string
  }> => invoke("auto_score_get_status"),
  autoScoreSortRules: (
    ruleIds: number[]
  ): Promise<{ success: boolean; data?: boolean; message?: string }> =>
    invoke("auto_score_sort_rules", { ruleIds }),
  autoScoreQueryBatches: (): Promise<{
    success: boolean
    data?: autoScoreExecutionBatch[]
    message?: string
  }> => invoke("auto_score_query_batches"),
  autoScoreRollbackBatch: (params: {
    batchId: string
  }): Promise<{ success: boolean; data?: autoScoreExecutionBatch; message?: string }> =>
    invoke("auto_score_rollback_batch", { params }),
  autoScoreApplyBackfill: (params: {
    items: autoScoreBackfillItem[]
  }): Promise<{ success: boolean; data?: autoScoreBackfillResult; message?: string }> =>
    invoke("auto_score_apply_backfill", { params }),

  // Settings & Sync
  getAllSettings: (): Promise<{ success: boolean; data: settingsSpec }> =>
    invoke("settings_get_all"),
  getSetting: <K extends settingsKey>(
    key: K
  ): Promise<{ success: boolean; data: settingsSpec[K] }> => invoke("settings_get", { key }),
  setSetting: <K extends settingsKey>(
    key: K,
    value: settingsSpec[K]
  ): Promise<{ success: boolean }> => invoke("settings_set", { key, value }),
  getSystemFonts: (): Promise<{ success: boolean; data: string[]; message?: string }> =>
    invoke("settings_get_system_fonts"),
  onSettingChanged: (callback: (change: settingChange) => void): Promise<UnlistenFn> => {
    return listen<settingChange>("settings:changed", (event) => {
      callback(event.payload)
    })
  },

  // Auth & Security
  authGetStatus: (): Promise<{
    success: boolean
    data: {
      permission: string
      hasAdminPassword: boolean
      hasPointsPassword: boolean
      hasRecoveryString: boolean
    }
  }> => invoke("auth_get_status"),
  authLogin: (
    password: string
  ): Promise<{ success: boolean; data?: { permission: string }; message?: string }> =>
    invoke("auth_login", { password }),
  authLogout: (): Promise<{ success: boolean; data: { permission: string } }> =>
    invoke("auth_logout"),
  authSetPasswords: (payload: {
    adminPassword?: string | null
    pointsPassword?: string | null
  }): Promise<{ success: boolean; data?: { recoveryString: string }; message?: string }> =>
    invoke("auth_set_passwords", payload),
  authGenerateRecovery: (): Promise<{ success: boolean; data: { recoveryString: string } }> =>
    invoke("auth_generate_recovery"),
  authResetByRecovery: (
    recoveryString: string
  ): Promise<{ success: boolean; data?: { recoveryString: string }; message?: string }> =>
    invoke("auth_reset_by_recovery", { recoveryString }),
  authClearAll: (): Promise<{ success: boolean }> => invoke("auth_clear_all"),

  // OAuth
  oauthGetAuthorizationUrl: (
    platformId: string,
    callbackUrl: string,
    state?: string
  ): Promise<{
    success: boolean
    data: {
      url: string
      state: string
      code_verifier: string
    }
    message?: string
  }> => invoke("oauth_get_authorization_url", { platformId, callbackUrl, state }),
  oauthExchangeCode: (
    code: string,
    platformId: string,
    callbackUrl: string,
    codeVerifier: string
  ): Promise<{
    success: boolean
    data: {
      access_token: string
      refresh_token: string
      token_type: string
      expires_in: number
    }
    message?: string
  }> => invoke("oauth_exchange_code", { code, platformId, callbackUrl, codeVerifier }),
  oauthGetUserInfo: (
    accessToken: string
  ): Promise<{
    success: boolean
    data: {
      user_id: string
      email: string
      name: string
      github_username?: string
      permission: number
    }
    message?: string
  }> => invoke("oauth_get_user_info", { accessToken }),
  oauthRefreshToken: (
    refreshToken: string,
    platformId: string
  ): Promise<{
    success: boolean
    data: {
      access_token: string
      refresh_token: string
      token_type: string
      expires_in: number
    }
    message?: string
  }> => invoke("oauth_refresh_token", { refreshToken, platformId }),
  oauthRevokeToken: (
    token: string,
    tokenTypeHint: string | null,
    platformId: string
  ): Promise<{
    success: boolean
    message?: string
  }> =>
    invoke("oauth_revoke_token", {
      token,
      tokenTypeHint,
      platformId,
    }),
  oauthIntrospectToken: (
    token: string,
    platformId: string
  ): Promise<{
    success: boolean
    data: {
      active: boolean
      scope?: string
      client_id?: string
      username?: string
      token_type?: string
      exp?: number
      iat?: number
      sub?: string
      aud?: string
      iss?: string
    }
    message?: string
  }> =>
    invoke("oauth_introspect_token", {
      token,
      platformId,
    }),
  oauthStartCallbackServer: (): Promise<{
    success: boolean
    data: {
      url: string
      port: number
    }
    message?: string
  }> => invoke("oauth_start_callback_server"),
  oauthStopCallbackServer: (): Promise<{
    success: boolean
    message?: string
  }> => invoke("oauth_stop_callback_server"),
  onOAuthCallback: (
    callback: (payload: {
      code?: string | null
      state?: string | null
      error?: string | null
      error_description?: string | null
    }) => void
  ): Promise<UnlistenFn> => {
    return listen<{
      code?: string | null
      state?: string | null
      error?: string | null
      error_description?: string | null
    }>("oauth-callback", (event) => {
      callback(event.payload || {})
    })
  },
  oauthReportOnline: (
    platformId: string,
    deviceType: string,
    customData?: any
  ): Promise<{
    success: boolean
    data: {
      success: boolean
      message: string
    }
    message?: string
  }> => invoke("oauth_report_online", { platformId, deviceType, customData }),
  oauthGetDeviceUuid: (): Promise<{
    success: boolean
    data: string
    message?: string
  }> => invoke("oauth_get_device_uuid"),
  oauthGetStorageUsage: (
    accessToken: string,
    platformId: string,
    userId: string
  ): Promise<{
    success: boolean
    data: {
      used_storage: number
      used_storage_formatted: string
      total_storage: number
      total_storage_formatted: string
      available_storage: number
      available_storage_formatted: string
      percentage: number
      file_count: number
    }
    message?: string
  }> => invoke("oauth_get_storage_usage", { accessToken, platformId, userId }),
  oauthSaveLoginState: (state: {
    access_token: string
    refresh_token: string
    token_type: string
    expires_in: number
    user_id: string
    email: string
    name: string
    github_username?: string
    login_time: string
  }): Promise<{
    success: boolean
    message?: string
  }> => invoke("oauth_save_login_state", { stateData: state }),
  oauthLoadLoginState: (): Promise<{
    success: boolean
    data?: {
      access_token: string
      refresh_token: string
      token_type: string
      expires_in: number
      user_id: string
      email: string
      name: string
      github_username?: string
      login_time: string
    } | null
    message?: string
  }> => invoke("oauth_load_login_state"),
  oauthClearLoginState: (): Promise<{
    success: boolean
    message?: string
  }> => invoke("oauth_clear_login_state"),

  // Data import/export
  exportDataJson: (): Promise<{ success: boolean; data: string }> => invoke("data_export_json"),
  importDataJson: (jsonText: string): Promise<{ success: boolean }> =>
    invoke("data_import_json", { jsonText }),

  // Window
  windowMinimize: (): Promise<void> => invoke("window_minimize"),
  windowMaximize: (): Promise<boolean> => invoke("window_maximize"),
  windowClose: (): Promise<void> => invoke("window_close"),
  windowIsMaximized: (): Promise<boolean> => invoke("window_is_maximized"),
  startDraggingWindow: (): Promise<void> => invoke("window_start_dragging"),
  toggleDevTools: (): Promise<void> => invoke("toggle_devtools"),
  windowResize: (width: number, height: number): Promise<void> =>
    invoke("window_resize", { width, height }),
  windowSetResizable: (resizable: boolean): Promise<void> =>
    invoke("window_set_resizable", { resizable }),
  onWindowMaximizedChanged: (callback: (maximized: boolean) => void): Promise<UnlistenFn> => {
    return listen<boolean>("window:maximized-changed", (event) => {
      callback(event.payload)
    })
  },
  onNavigate: (callback: (route: string) => void): Promise<UnlistenFn> => {
    return listen<string>("app:navigate", (event) => {
      callback(event.payload)
    })
  },
  onDataUpdated: (callback: (payload: dataUpdatedEvent) => void): Promise<UnlistenFn> => {
    return listen<dataUpdatedEvent>("ss:data-updated", (event) => {
      callback(event.payload || {})
    })
  },
  onDeepLink: (callback: (url: string) => void): Promise<UnlistenFn> => {
    return listen<string>("deep-link://new-url", (event) => {
      callback(event.payload)
    })
  },

  // Logger
  queryLogs: (
    input?: number | { lines?: number }
  ): Promise<{ success: boolean; data: string[] }> => {
    const lines = typeof input === "number" ? input : input?.lines
    return invoke("log_query", { lines })
  },
  clearLogs: (): Promise<{ success: boolean }> => invoke("log_clear"),
  setLogLevel: (level: string): Promise<{ success: boolean }> => invoke("log_set_level", { level }),
  writeLog: (payload: {
    level: string
    message: string
    meta?: any
  }): Promise<{ success: boolean }> => invoke("log_write", { payload }),

  // Database Connection
  dbTestConnection: (
    connectionString: string
  ): Promise<{ success: boolean; data: { success: boolean; error?: string } }> =>
    invoke("db_test_connection", { connectionString }),
  dbSwitchConnection: (
    connectionString: string
  ): Promise<{ success: boolean; data: { type: "sqlite" | "postgresql" } }> =>
    invoke("db_switch_connection", { connectionString }),
  dbGetStatus: (): Promise<{
    success: boolean
    data: { type: string; connected: boolean; error?: string }
  }> => invoke("db_get_status"),
  dbSync: (): Promise<{ success: boolean; data: { success: boolean; message?: string } }> =>
    invoke("db_sync"),
  dbSyncPreview: (): Promise<{
    success: boolean
    data: {
      can_sync: boolean
      need_sync: boolean
      local_only: number
      remote_only: number
      conflicts: Array<{
        table: string
        key: string
        local_summary: string
        remote_summary: string
      }>
      message?: string
    }
  }> => invoke("db_sync_preview"),
  dbSyncApply: (
    strategy: "keep_local" | "keep_remote"
  ): Promise<{
    success: boolean
    data: { success: boolean; synced_records: number; resolved_conflicts: number; message?: string }
  }> => invoke("db_sync_apply", { strategy }),

  // HTTP Server
  httpServerStart: (config?: {
    port?: number
    host?: string
    corsOrigin?: string
  }): Promise<{ success: boolean; data: { url: string; config: any } }> =>
    invoke("http_server_start", { config }),
  httpServerStop: (): Promise<{ success: boolean }> => invoke("http_server_stop"),
  httpServerStatus: (): Promise<{
    success: boolean
    data: { isRunning: boolean; config?: any; url?: string }
  }> => invoke("http_server_status"),

  // MCP Server
  mcpServerStart: (config?: {
    port?: number
    host?: string
  }): Promise<{
    success: boolean
    data?: { url: string; config: { port: number; host: string } }
  }> => invoke("mcp_server_start", { config }),
  mcpServerStop: (): Promise<{ success: boolean }> => invoke("mcp_server_stop"),
  mcpServerStatus: (): Promise<{
    success: boolean
    data?: {
      is_running: boolean
      config: { port: number; host: string }
      url?: string | null
    }
  }> => invoke("mcp_server_status"),

  // File System
  fsGetConfigStructure: (): Promise<{
    success: boolean
    data: { configRoot: string; automatic: string; script: string }
  }> => invoke("fs_get_config_structure"),
  fsReadJson: (
    relativePath: string,
    folder?: "automatic" | "script"
  ): Promise<{ success: boolean; data: any }> => invoke("fs_read_json", { relativePath, folder }),
  fsWriteJson: (
    relativePath: string,
    data: any,
    folder?: "automatic" | "script"
  ): Promise<{ success: boolean }> => invoke("fs_write_json", { relativePath, data, folder }),
  fsReadText: (
    relativePath: string,
    folder?: "automatic" | "script"
  ): Promise<{ success: boolean; data: string }> =>
    invoke("fs_read_text", { relativePath, folder }),
  fsWriteText: (
    content: string,
    relativePath: string,
    folder?: "automatic" | "script"
  ): Promise<{ success: boolean }> => invoke("fs_write_text", { content, relativePath, folder }),
  fsDeleteFile: (
    relativePath: string,
    folder?: "automatic" | "script"
  ): Promise<{ success: boolean }> => invoke("fs_delete_file", { relativePath, folder }),
  fsListFiles: (folder?: "automatic" | "script"): Promise<{ success: boolean; data: any[] }> =>
    invoke("fs_list_files", { folder }),
  fsFileExists: (
    relativePath: string,
    folder?: "automatic" | "script"
  ): Promise<{ success: boolean; data: boolean }> =>
    invoke("fs_file_exists", { relativePath, folder }),
  fsOpenPath: (
    relativePath: string,
    folder?: "automatic" | "script"
  ): Promise<{ success: boolean; message?: string }> =>
    invoke("fs_open_path", { relativePath, folder }),

  // App
  registerUrlProtocol: (): Promise<{
    success: boolean
    data?: { registered: boolean }
    message?: string
  }> => invoke("register_url_protocol"),
  checkUrlProtocolStatus: (): Promise<{
    success: boolean
    data?: {
      registered: boolean
      protocol: string
      platform: string
      details: string
    }
    message?: string
  }> => invoke("check_url_protocol_status"),
  unregisterUrlProtocol: (): Promise<{
    success: boolean
    data?: { registered: boolean }
    message?: string
  }> => invoke("unregister_url_protocol"),
  checkElevation: (): Promise<{
    success: boolean
    data?: { is_elevated: boolean; platform: string; can_request_elevation: boolean }
    message?: string
  }> => invoke("check_elevation"),
  requestElevation: (): Promise<{
    success: boolean
    message?: string
  }> => invoke("request_elevation"),
  appQuit: (): Promise<void> => invoke("app_quit"),
  appRestart: (): Promise<void> => invoke("app_restart"),

  // Plugin
  pluginGetAll: (): Promise<{
    success: boolean
    data?: any[]
    message?: string
  }> => invoke("plugin_get_all"),
  pluginGet: (
    pluginId: string
  ): Promise<{
    success: boolean
    data?: any
    message?: string
  }> => invoke("plugin_get", { pluginId }),
  pluginGetStats: (): Promise<{
    success: boolean
    data?: {
      total_plugins: number
      enabled_plugins: number
      disabled_plugins: number
    }
    message?: string
  }> => invoke("plugin_get_stats"),
  pluginToggle: (
    pluginId: string,
    enabled: boolean
  ): Promise<{
    success: boolean
    message?: string
  }> => invoke("plugin_toggle", { pluginId, enabled }),
  pluginInstall: (
    manifest: any,
    pluginDir: string
  ): Promise<{
    success: boolean
    data?: any
    message?: string
  }> => invoke("plugin_install", { manifest, pluginDir }),
  pluginUninstall: (
    pluginId: string
  ): Promise<{
    success: boolean
    message?: string
  }> => invoke("plugin_uninstall", { pluginId }),
  pluginLoadManifest: (
    path: string
  ): Promise<{
    success: boolean
    data?: any
    message?: string
  }> => invoke("plugin_load_manifest", { path }),
  pluginGetDir: (
    pluginId: string
  ): Promise<{
    success: boolean
    data?: string
    message?: string
  }> => invoke("plugin_get_dir", { pluginId }),
  pluginGetList: (): Promise<{
    success: boolean
    data?: any[]
    message?: string
  }> => invoke("plugin_get_list"),
  pluginGetRuntimeModules: (): Promise<{
    success: boolean
    data?: pluginRuntimeModule[]
    message?: string
  }> => invoke("plugin_get_runtime_modules"),

  // Generic invoke wrapper for backward compatibility with callers using `api.invoke`
  invoke: async (channel: string): Promise<any> => {
    switch (channel) {
      default:
        throw new Error(`Unsupported legacy invoke channel: ${channel}`)
    }
  },
}

export default api
export { api }
