/**
 * Electron API 类型定义
 * 从 preload/index.ts 提取的类型，用于独立前端项目
 */

export type TabId = 'A' | 'B' | 'C'

export interface Account {
    email: string
    pwd: string
    group?: string
}

export interface AutomationConfig {
    aiReplyEnabled: boolean
    isGreetFirst: boolean
    prologueList: Record<string, string[]>
    sleepTime: number
    needTranslate: boolean
    inviteThreshold: number
    accounts: Account[]
    tenantId: string
    token: string
    filters: {
        minFans: number
        maxFans: number
        minCoins: number
        maxCoins: number
        minOnlineFans: number
        maxOnlineFans: number
        hostsLevelList: number[]
        gold: boolean
        ordinary: boolean
        maxAnchorCount?: number
        hostListGateEnabled?: boolean
        hostListGateMinCount?: number
    }
}

export interface AutomationLog {
    viewId: number
    level: 'info' | 'warn' | 'error'
    message: string
}

export interface RotationStatus {
    enabled: boolean
    currentActiveGroup: string
    modeStartTime: number
    instanceModes: { viewId: number; email: string; group: string; mode: 'active' | 'background' }[]
}

export interface UpdateInfo {
    version: string
    releaseDate?: string
    releaseNotes?: string
}

export interface UpdateProgress {
    percent: number
    bytesPerSecond: number
    transferred: number
    total: number
}

export interface ViewStats {
    viewId: number
    group: string
    greeting: number
    invite: number
    reply: number
    unread: number
}

export interface GreetingStats {
    greetingCount: number
    inviteCount: number
    replyCount: number
    unreadCount: number
    details: ViewStats[]
}

export interface StandaloneTikTokAutomationOptions {
    greetingMessages?: string[]
    prologueList?: Record<string, string[]>
    needTranslate?: boolean
    replyMessages?: string[]
    replyUnreadMessages?: boolean
    dataPoolSource?: 'anchor_hosts' | 'brother_info' | '/api/anchor/hosts/getAll' | '/api/gifters/brotherInfo/getAll'
    groupSwitchMinutes?: number
    groupViewCounts?: number[]
    initialFullGroupNumber?: number
    continuousMode?: boolean
    runMode?: string
    homeUrl?: string
    waitForManualLogin?: boolean
    searchKeyword?: string
}

export interface ViewProxyConfig {
    mode: 'fixed_servers' | string
    proxyRules: string
    username?: string
    password?: string
}

export interface ElectronAPI {
    // 基础视图控制
    hideViews: () => Promise<{ success: boolean }>
    showViews: () => Promise<{ success: boolean }>
    warmUpViews: () => Promise<{ success: boolean; error?: string }>
    switchTab: (tab: TabId) => Promise<{ success: boolean; currentTab?: TabId; error?: string }>
    switchToView: (viewId: number) => Promise<{ success: boolean; currentViewId?: number; error?: string }>
    getCurrentTab: () => Promise<TabId>
    getCurrentViewId: () => Promise<number>
    runAutomation: (viewId: number) => Promise<{ success: boolean; message: string }>
    loadUrl: (viewId: number, url: string) => Promise<{ success: boolean; error?: string }>
    getViewsInfo: () => Promise<{ id: number; url: string }[]>

    // 登录
    login: (credentials: { tenantName: string; username: string; password: string }) =>
        Promise<{ success: boolean; user?: unknown; error?: string }>
    logout: () => Promise<{ success: boolean; error?: string }>

    // TikTok 自动化
    startTikTokAutomation: (viewId: number, account: Account) => Promise<{ success: boolean; message?: string; error?: string }>
    stopTikTokAutomation: (viewId: number) => Promise<{ success: boolean; message?: string; error?: string }>
    startStandaloneTikTokAutomationAll: (
        options: StandaloneTikTokAutomationOptions
    ) => Promise<{ success: boolean; error?: string }>
    stopStandaloneTikTokAutomationAll: () => Promise<{ success: boolean; error?: string }>
    getViewProxy: (viewId: number) => Promise<{ success: boolean; proxyConfig?: ViewProxyConfig | null; error?: string }>
    setViewProxy: (
        viewId: number,
        proxyConfig: ViewProxyConfig,
        reloadCurrentView?: boolean
    ) => Promise<{ success: boolean; warning?: string; error?: string }>
    clearViewProxy: (viewId: number, reloadCurrentView?: boolean) => Promise<{ success: boolean; error?: string }>
    updateAutomationConfig: (config: Partial<AutomationConfig>) => Promise<{ success: boolean }>
    getAutomationConfig: () => Promise<AutomationConfig>

    // 清空缓存
    clearAllCache: () => Promise<void>

    // 调试工具
    openDevtools: (viewId: number) => Promise<{ success: boolean; error?: string }>

    // 数据存储
    saveAIConfig: (config: Record<string, unknown>) => Promise<{ success: boolean }>
    loadAIConfig: () => Promise<Record<string, unknown>>
    loadAnchorData: () => Promise<unknown[]>
    saveAnchorData: (data: unknown[]) => Promise<{ success: boolean }>
    openShop: (url: string) => Promise<{ success: boolean; error?: string }>
    hideShop: () => Promise<{ success: boolean; error?: string }>
    setSidebarWidth: (width: number) => Promise<{ success: boolean }>
    saveRunConfig: (config: Record<string, unknown>) => Promise<{ success: boolean }>
    loadRunConfig: () => Promise<Record<string, unknown> | null>

    // 翻译
    translate: (text: string, targetLang: string) => Promise<{ success: boolean; result: string; error?: string }>

    // 轮换状态
    getRotationStatus: () => Promise<RotationStatus>

    // 打招呼统计
    getGreetingStats: () => Promise<GreetingStats>
    getRepliedSessions: () => Promise<Array<{ name: string; id: string }>>

    // 获取打招呼内容
    fetchPrologue: () => Promise<{ success: boolean; data?: string[]; error?: string }>

    // 健康检查
    checkHealth: () => Promise<{ success: boolean; code?: number; message?: string; error?: string }>

    // 更新相关
    checkForUpdates: () => Promise<boolean>
    downloadUpdate: () => Promise<void>
    installUpdate: () => void
    getAppVersion: () => Promise<string>

    // 事件监听
    onAutomationLog: (callback: (log: AutomationLog) => void) => () => void
    onRotationStatusChanged: (callback: (status: RotationStatus) => void) => () => void
    onRequestSaveConfig: (callback: () => void) => () => void
    onRequestClearLogin: (callback: () => void) => () => void
    onAppClosingStart: (callback: () => void) => () => void
    onAppClosingComplete: (callback: () => void) => () => void
    onUpdateChecking: (callback: () => void) => () => void
    onUpdateAvailable: (callback: (info: UpdateInfo) => void) => () => void
    onUpdateNotAvailable: (callback: () => void) => () => void
    onUpdateProgress: (callback: (progress: UpdateProgress) => void) => () => void
    onUpdateDownloaded: (callback: (info: { version: string }) => void) => () => void
    onUpdateError: (callback: (error: { message: string }) => void) => () => void
    onUpdateManualInstall: (callback: (info: { path: string }) => void) => () => void
    onGreetingStatsChanged: (callback: (stats: GreetingStats) => void) => () => void

    tk?: {
        updateStartConfig: (config: string) => Promise<{ success: boolean; error?: string }>
        getDataCount: () => Promise<string>
        loginTikTok: () => Promise<void>
        loginBigTikTok: () => Promise<void>
        loginBackStage: (data: string) => Promise<void>
        loginBackStageCopy: (data: string) => Promise<void>
        checkBackStageLoginStatus: (account?: string) => Promise<string>
        checkBackStageLoginStatusCopy: () => Promise<string>
        stopCrawl: () => Promise<void>
        getVersion: () => Promise<string>
        checkTkLoginStatus: () => Promise<string>
        visitAnchor: (id: string) => Promise<void>
        exportData: (data: string) => Promise<void>
        controlTask: (data: string) => Promise<{ success: boolean; message?: string; error?: string }>
        controlExpertCapture: (data: string) => Promise<{ success: boolean; message?: string; error?: string }>
        controlCheckTask: (payload: { isRunning: boolean; model: boolean }) => Promise<{ success: boolean; message?: string; error?: string }>
        getBrotherInfo: () => Promise<string>
        queryBrotherInfo: (data: string) => Promise<string>
        getAllBrotherInfo: () => Promise<string>
        deleteBrotherInfo: (data: string) => Promise<string>
        findBigBrother: (data: string) => Promise<{ success: boolean }>
        storageSet: (data: string) => Promise<{ success: boolean; error?: string }>
        storageRead: (data: string) => Promise<string>
        openRoom: (id: string) => Promise<void>
        storageAccount: (data: string) => Promise<{ success: boolean; error?: string }>
        readAccount: (data: string) => Promise<string>
        setClipboard: (text: string) => Promise<{ success: boolean; error?: string }>
        startBrotherMonitor: () => Promise<{ success: boolean; error?: string }>
        getBrotherLoginStatus: () => Promise<string>
        visitGifter: (data: string) => Promise<{ success: boolean; error?: string }>
        closeAllBrowsers: () => Promise<{ success: boolean; error?: string }>
    }
}

// 声明全局类型
declare global {
    interface Window {
        electronAPI?: ElectronAPI
    }
}

export { }
