/**
 * WhatsApp channel adapter.
 *
 * Implements the ChannelAdapter interface using whatsapp-web.js with headless Chrome.
 * QR code auth flow: prints QR to console for scanning on first run.
 * Session persistence via LocalAuth strategy with configurable data_dir.
 * Messages are chunked at 4096 chars (same as Telegram).
 */

import WhatsApp from 'whatsapp-web.js';
const { Client, LocalAuth, MessageMedia } = WhatsApp;
import type {
  Attachment,
  InboundMessage,
  OutboundMessage,
  OutboundAttachment,
  ChannelAdapter,
  ChannelStatus,
} from '../types.js';
import { splitMessage } from '../utils.js';
import type { PairingManager } from '../pairing.js';

/** Configuration for the WhatsApp channel adapter. */
export interface WhatsAppAdapterConfig {
  /** Phone numbers allowed to interact. Empty = all numbers. */
  allowedNumbers?: string[];
  /** Group IDs (without @g.us suffix) allowed to interact. Empty = no groups. */
  allowedGroupIds?: string[];
  /** Require bot mention to respond in group chats (default: true). DMs always respond. */
  requireMention?: boolean;
  /** Directory for session persistence (LocalAuth data path). */
  dataDir?: string;
  /** Optional pairing manager for DM pairing codes. */
  pairingManager?: PairingManager;
}

/** Minimal shape of a whatsapp-web.js message. */
interface WhatsAppMessage {
  id: { id: string; fromMe: boolean };
  from: string;
  body: string;
  timestamp: number;
  fromMe: boolean;
  author?: string;
  _data?: { notifyName?: string };
  /** Whether this message contains media (image, video, audio, document). */
  hasMedia?: boolean;
  /** Message type (e.g. "image", "video", "chat"). */
  type?: string;
  /** Download the media attached to this message. */
  downloadMedia?: () => Promise<{ mimetype: string; data: string; filename?: string } | null>;
}

/**
 * WhatsApp channel adapter backed by whatsapp-web.js.
 *
 * Handles QR code authentication, phone number allowlist filtering,
 * session persistence, and message chunking for 4096-char limit.
 */
export class WhatsAppAdapter implements ChannelAdapter {
  readonly name = 'whatsapp';

  private _status: ChannelStatus = 'disconnected';
  private client: InstanceType<typeof Client> | null = null;
  private messageHandler?: (msg: InboundMessage) => void;
  private config: WhatsAppAdapterConfig;
  private botId?: string;

  get status(): ChannelStatus {
    return this._status;
  }

  constructor(config: WhatsAppAdapterConfig) {
    this.config = config;
  }

  /** Register the inbound message handler. Called by the registry before connect(). */
  onMessage(handler: (msg: InboundMessage) => void): void {
    this.messageHandler = handler;
  }

  /** Create the whatsapp-web.js client, wire up event handlers, and initialize. */
  async connect(): Promise<void> {
    this._status = 'connecting';

    try {
      const authStrategy = new LocalAuth({
        dataPath: this.config.dataDir,
      });

      this.client = new Client({
        authStrategy,
        puppeteer: {
          headless: true,
          args: ['--no-sandbox', '--disable-setuid-sandbox'],
        },
      });

      // Promise that resolves on 'ready' or rejects on 'auth_failure'
      const readyPromise = new Promise<void>((resolve, reject) => {
        this.client!.on('ready', () => {
          console.log('WhatsApp bot connected');
          this._status = 'connected';
          // Capture bot's own JID for mention detection
          this.botId = (this.client as any)?.info?.wid?._serialized;
          resolve();
        });

        this.client!.on('auth_failure', (msg: string) => {
          this._status = 'error';
          reject(new Error(`WhatsApp auth failure: ${msg}`));
        });

        this.client!.on('qr', (qr: string) => {
          console.log('WhatsApp QR code received. Scan with your phone:');
          console.log(qr);
        });
      });

      // Register message event handler
      this.client.on('message', (message: unknown) => {
        this.handleMessage(message as WhatsAppMessage);
      });

      await this.client.initialize();
      await readyPromise;
    } catch (error) {
      this._status = 'error';
      throw error;
    }
  }

  /** Stop the client and clean up. */
  async disconnect(): Promise<void> {
    if (this.client) {
      try {
        await this.client.destroy();
      } catch {
        // Swallow destroy errors — cleanup must complete
      } finally {
        this.client = null;
      }
    }
    this._status = 'disconnected';
  }

  /** Send an outbound message, automatically chunking if it exceeds 4096 chars. */
  async send(peerId: string, message: OutboundMessage): Promise<void> {
    if (!this.client) {throw new Error('WhatsApp adapter not connected');}

    const text = message.text;

    if (text.length <= 4096) {
      await this.client.sendMessage(peerId, text);
    } else {
      const chunks = splitMessage(text, 4096);
      for (const chunk of chunks) {
        await this.client.sendMessage(peerId, chunk);
      }
    }

    // Send outbound attachments after text
    if (message.attachments && message.attachments.length > 0) {
      for (const attachment of message.attachments) {
        await this.sendAttachment(peerId, attachment);
      }
    }
  }

  /** Send a single outbound attachment via WhatsApp using MessageMedia. */
  private async sendAttachment(peerId: string, attachment: OutboundAttachment): Promise<void> {
    if (!this.client) {return;}

    try {
      if (attachment.data) {
        const media = new MessageMedia(
          attachment.mimeType,
          attachment.data,
          attachment.filename,
        );
        await this.client.sendMessage(peerId, media);
      } else if (attachment.url) {
        // Download from URL and send as MessageMedia
        const media = await MessageMedia.fromUrl(attachment.url);
        await this.client.sendMessage(peerId, media);
      }
    } catch (error) {
      console.error(
        `WhatsApp: failed to send ${attachment.mimeType} attachment:`,
        error instanceof Error ? error.message : 'Unknown error',
      );
    }
  }

  /** Internal: process an inbound WhatsApp message. */
  private async handleMessage(message: WhatsAppMessage): Promise<void> {
    if (!this.messageHandler) {return;}

    // Ignore messages from the bot itself
    if (message.fromMe) {return;}

    const from = message.from;

    // Group message handling
    const isGroup = from.endsWith('@g.us');
    if (isGroup) {
      // Check allowed group IDs
      const groupId = from.replace(/@g\.us$/, '');
      if (
        !this.config.allowedGroupIds ||
        this.config.allowedGroupIds.length === 0 ||
        !this.config.allowedGroupIds.includes(groupId)
      ) {
        return; // Group not allowed (empty list = no groups)
      }

      // Mention requirement in group chats
      const requireMention = this.config.requireMention ?? true;
      if (requireMention) {
        // WhatsApp mentions use @phone_number format in body
        // Also check for mentions in the message mentionedIds
        const mentionsBot = this.botId
          ? message.body?.includes(`@${this.botId.replace(/@c\.us$/, '')}`) ||
            (message as any).mentionedIds?.some((id: string) => id === this.botId)
          : false;
        if (!mentionsBot) {return;}
      }
    }

    // Check allowed numbers for DMs (strip @c.us suffix for comparison)
    if (!isGroup) {
      const phoneNumber = from.replace(/@c\.us$/, '');
      if (
        this.config.allowedNumbers &&
        this.config.allowedNumbers.length > 0 &&
        !this.config.allowedNumbers.includes(phoneNumber)
      ) {
        // Pairing fallback — check if the sender is approved or sending a valid code
        const pm = this.config.pairingManager;
        if (pm?.enabled) {
          if (pm.isApproved('whatsapp', phoneNumber)) {
            // Approved — fall through to normal message handling
          } else {
            const text = (message.body ?? '').trim();
            if (text && pm.validateCode('whatsapp', phoneNumber, text)) {
              // Code validated — send confirmation via WhatsApp
              if (this.client) {
                try {
                  await this.client.sendMessage(from, 'Pairing successful! You can now chat with Flynn.');
                } catch { /* ignore send errors */ }
              }
            }
            return;
          }
        } else {
          return;
        }
      }
    }

    // Send typing indicator
    try {
      const chat = await (message as any).getChat();
      await chat.sendStateTyping();
    } catch { /* ignore typing errors */ }

    // Strip bot mention from message body for group messages
    let text = message.body ?? '';
    if (isGroup && this.botId) {
      const botNumber = this.botId.replace(/@c\.us$/, '');
      text = text.replace(new RegExp(`@${botNumber}\\b`, 'g'), '').trim();
    }

    const senderName = message._data?.notifyName;

    // Extract media attachments if the message has media
    const attachments: Attachment[] = [];
    if (message.hasMedia) {
      try {
        const media = await (message as any).downloadMedia();
        if (media && typeof media.mimetype === 'string') {
          const mimeType = media.mimetype;
          const isAudio = mimeType.startsWith('audio/');
          const isImage = mimeType.startsWith('image/');
          const isVoice = message.type === 'ptt';

          if (isAudio || isImage || isVoice) {
            attachments.push({
              mimeType: mimeType,
              data: media.data,
              filename: media.filename,
            });
          }
        }
      } catch (error) {
        console.error(
          'Failed to download WhatsApp media:',
          error instanceof Error ? error.message : 'Unknown error',
        );
      }
    }

    // Detect reset command
    if (text === '!reset' || text === 'reset') {
      this.messageHandler({
        id: message.id.id,
        channel: 'whatsapp',
        senderId: from,
        senderName,
        text: '!reset',
        timestamp: Date.now(),
        metadata: { isCommand: true, command: 'reset' },
        ...(attachments.length > 0 ? { attachments } : {}),
      });
      return;
    }

    // Regular message
    this.messageHandler({
      id: message.id.id,
      channel: 'whatsapp',
      senderId: from,
      senderName,
      text,
      timestamp: Date.now(),
      ...(attachments.length > 0 ? { attachments } : {}),
    });
  }
}
