'use strict';

const PROVIDER_LABELS = Object.freeze({
  anthropic: 'Anthropic',
  openai: 'OpenAI',
  google: 'Google Gemini',
  gemini: 'Google Gemini',
  moonshot: 'Moonshot / Kimi',
});

function multimodalUnavailable(message) {
  const err = new Error(message);
  err.code = 'MULTIMODAL_NOT_AVAILABLE';
  return err;
}

function toInternalAttachmentBlock(attachment) {
  const baseSrc = {
    type: 'base64',
    media_type: attachment.mediaType,
    data: attachment.base64,
  };

  if (attachment.type === 'image') {
    return { type: 'image', source: baseSrc };
  }

  if (attachment.type === 'pdf') {
    return {
      type: 'document',
      source: { ...baseSrc, media_type: 'application/pdf' },
    };
  }

  if (attachment.type === 'audio') {
    const fmt = String(attachment.mediaType || '').split('/')[1] || 'mp3';
    return { type: 'audio', source: baseSrc, format: fmt };
  }

  throw multimodalUnavailable(`Unknown attachment kind "${attachment.type}"`);
}

function buildOpenAiCompatibleAttachmentBlock(providerType, attachment) {
  const label = PROVIDER_LABELS[providerType] || providerType || 'This provider';

  if (attachment.type === 'pdf') {
    throw multimodalUnavailable(`${label} chat does not natively support PDF attachments. Switch to Anthropic or Google Gemini for PDF support.`);
  }

  if (attachment.type === 'audio' && providerType === 'moonshot') {
    throw multimodalUnavailable('Moonshot / Kimi chat does not natively support audio attachments. Switch to OpenAI or Google Gemini for audio.');
  }

  return toInternalAttachmentBlock(attachment);
}

/**
 * Convert parsed Wall-E attachments into the internal Anthropic-shaped content
 * blocks used between chat.js and the provider adapters.
 *
 * The adapter layer owns the provider-native conversion:
 * - OpenAI and Moonshot use `image_url` with base64 data URLs.
 * - Google/Gemini uses inline data.
 * - Anthropic accepts image/document base64 blocks directly.
 */
function buildAttachmentBlocks(providerType, attachments) {
  if (!attachments || attachments.length === 0) return null;

  const provider = String(providerType || '').toLowerCase();

  if (provider === 'anthropic') {
    return attachments.map((attachment) => {
      if (attachment.type === 'audio') {
        throw multimodalUnavailable('Anthropic chat does not accept audio attachments. Switch to OpenAI or Google Gemini for audio.');
      }
      return toInternalAttachmentBlock(attachment);
    });
  }

  if (provider === 'openai' || provider === 'moonshot') {
    return attachments.map((attachment) => buildOpenAiCompatibleAttachmentBlock(provider, attachment));
  }

  if (provider === 'google' || provider === 'gemini') {
    return attachments.map(toInternalAttachmentBlock);
  }

  throw multimodalUnavailable(
    `Attachments not supported on provider "${providerType}". Switch to a configured Anthropic, OpenAI, Google Gemini, or Moonshot/Kimi provider for this turn.`
  );
}

module.exports = {
  buildAttachmentBlocks,
  toInternalAttachmentBlock,
};
