import { __ } from '@wordpress/i18n';
import { rejectReason } from './labels';
import { isBigCommerce } from './host';

/**
 * Consignes de l'ecran « Ameliorer mes produits », dans la langue de l'administration.
 *
 * L'API envoie un code; `rejectReason` le traduit pour l'ecran Flux produit, ou « plus haut »
 * designe un champ de cet ecran-la. Ici, le meme renvoi pointait vers un champ qui n'existe pas:
 * ces codes ont donc leur phrase propre, qui renvoie vers l'ecran Flux produit.
 *
 * Les motifs d'image (`image_missing`, `image_url_invalid`) n'ont PAS de phrase propre ici: ils
 * prennent celle de l'ecran Flux produit, pour que les deux ecrans disent la meme chose du meme
 * produit (revue du 2026-09-23: « adresse https » d'un cote, « ajoutez une image » de l'autre).
 *
 * Repli: la phrase de `rejectReason` si elle existe, sinon une consigne generique traduite.
 * JAMAIS le message technique anglais (« image_url must be a valid absolute http(s) URL »).
 */
const own = (): Record<string, string> => ({
  add_image: __('Add a product image so this product can appear in ChatGPT.', 'convrail'),
  add_gtin: __(
    'Add a barcode (GTIN) or a manufacturer part number so ChatGPT matches the exact product.',
    'convrail',
  ),
  add_category: isBigCommerce()
    ? __('Put the product in a BigCommerce category so ChatGPT knows what it is.', 'convrail')
    : __('Put the product in a WooCommerce category so ChatGPT knows what it is.', 'convrail'),
  longer_description: __(
    'Write a fuller description (200 characters or more): ChatGPT recommends the products it understands.',
    'convrail',
  ),
  add_sale_price: __('Show the sale price when the product is on sale.', 'convrail'),
  add_more_images: __('Add more product photos.', 'convrail'),
  add_ratings: __(
    'Collect ratings and customer reviews: a strong trust signal for buyers.',
    'convrail',
  ),
  describe_variants: __(
    'Name your variation attributes (size, color) so ChatGPT can answer precise requests.',
    'convrail',
  ),
  add_return_window: __(
    'State your return policy on the Product feed screen: buyers who see it order with confidence.',
    'convrail',
  ),
  brand_required: __(
    'Add a brand to the product, or a default brand on the Product feed screen.',
    'convrail',
  ),
  brand_placeholder: __(
    '"Unknown" is not a brand: add the real brand, or a default brand on the Product feed screen.',
    'convrail',
  ),
  image_url_format: __('Use a JPEG or PNG image.', 'convrail'),
  url_invalid: __('This product has no public page yet: publish it on your store.', 'convrail'),
  seller_name_required: __('Add your store name on the Product feed screen.', 'convrail'),
  target_countries_invalid: __(
    'Choose the countries you sell to on the Product feed screen.',
    'convrail',
  ),
});

export function optimizeReason(code: string | undefined, message: string): string {
  const fallback = isBigCommerce()
    ? __('Complete this product’s details in BigCommerce.', 'convrail')
    : __('Complete this product’s details in WooCommerce.', 'convrail');
  if (!code) return fallback;
  const text = own()[code];
  if (text) return text;
  const shared = rejectReason(code, message);
  // `rejectReason` rend le message technique quand il ne connait pas le code: on ne l'affiche pas.
  return shared === message ? fallback : shared;
}

/** Motifs reglables sur l'ecran Flux produit: la ligne propose d'y aller. */
const FEED_SCREEN_CODES = new Set([
  'brand_required',
  'brand_placeholder',
  'add_return_window',
  'seller_name_required',
  'target_countries_invalid',
]);

export const pointsToFeedScreen = (code: string | undefined): boolean =>
  code !== undefined && FEED_SCREEN_CODES.has(code);

/** Consignes visibles d'emblee: au-dela, la ligne devient un mur de texte. */
export const VISIBLE_REASONS = 2;
