import { IS_DEV } from '../utils/config';

export type AccountProvider =
  | 'whatsapp'
  | 'telegram'
  | 'linkedin'
  | 'slack'
  | 'discord'
  | 'google-meet'
  | 'zoom'
  | 'browserscan';

// Status lifecycle for an embedded webview account:
//   'pending'  — openWebviewAccount invoked, Rust-side add_child not yet confirmed
//   'loading'  — CEF child webview spawned off-screen, waiting for first page-loaded
//                signal; WebviewHost shows its spinner
//   'timeout'  — initial load watchdog elapsed; keep overlay visible and let user retry
//   'open'     — page loaded, webview_account_reveal completed, webview on-screen
//   'closed'   — webview destroyed
//   'error'    — open/reveal failed (lastError populated)
export type AccountStatus = 'pending' | 'loading' | 'timeout' | 'open' | 'error' | 'closed';

export interface Account {
  id: string;
  provider: AccountProvider;
  label: string;
  createdAt: string;
  status: AccountStatus;
  lastError?: string;
}

export interface IngestedMessage {
  id: string;
  from?: string | null;
  body?: string | null;
  unread?: number;
  ts?: number;
}

export interface AccountsState {
  accounts: Record<string, Account>;
  order: string[];
  activeAccountId: string | null;
  /**
   * Issue #1233 — most-recently-active non-agent account id, persisted
   * across sessions. Drives the on-mount prewarm of `Accounts.tsx` so the
   * first user click hits the warm-reopen branch instead of paying a
   * cold load. Updated on rail click + new-account pick. `null` until the
   * user activates a real (non-agent) account at least once.
   */
  lastActiveAccountId: string | null;
  messages: Record<string, IngestedMessage[]>;
  unread: Record<string, number>;
  logs: Record<string, AccountLogEntry[]>;
}

export interface AccountLogEntry {
  ts: number;
  level: 'info' | 'warn' | 'error' | 'debug';
  msg: string;
}

export interface ProviderDescriptor {
  id: AccountProvider;
  label: string;
  description: string;
  serviceUrl: string;
}

const BASE_PROVIDERS: ProviderDescriptor[] = [
  {
    id: 'whatsapp',
    label: 'WhatsApp Web',
    description: 'Open web.whatsapp.com inside the app and stream chat updates.',
    serviceUrl: 'https://web.whatsapp.com/',
  },
  {
    id: 'telegram',
    label: 'Telegram Web',
    description: 'Your Telegram chats, embedded and observed.',
    serviceUrl: 'https://web.telegram.org/k/',
  },
  {
    id: 'linkedin',
    label: 'LinkedIn',
    description: 'LinkedIn messaging — DMs and conversations.',
    serviceUrl: 'https://www.linkedin.com/messaging/',
  },
  {
    id: 'slack',
    label: 'Slack',
    description: 'Slack workspaces and channels.',
    serviceUrl: 'https://app.slack.com/client/',
  },
  {
    id: 'discord',
    label: 'Discord',
    description: 'Discord servers and DMs — channel list and unread counts.',
    serviceUrl: 'https://discord.com/channels/@me',
  },
  {
    id: 'google-meet',
    label: 'Google Meet',
    description: 'Join Google Meet calls and capture live captions.',
    serviceUrl: 'https://meet.google.com/',
  },
  {
    id: 'zoom',
    label: 'Zoom',
    description: 'Zoom web client — log in and join meetings from inside the app.',
    serviceUrl: 'https://zoom.us/',
  },
];

const DEV_PROVIDERS: ProviderDescriptor[] = [
  {
    id: 'browserscan',
    label: 'BrowserScan (dev)',
    description: 'Bot-detection sandbox for sanity-checking our webview fingerprint.',
    serviceUrl: 'https://www.browserscan.net/bot-detection',
  },
];

export const PROVIDERS: ProviderDescriptor[] = IS_DEV
  ? [...BASE_PROVIDERS, ...DEV_PROVIDERS]
  : BASE_PROVIDERS;
