/**
 * PHP hook snippet generators for developer documentation.
 */

export type LayoutHookType = "html" | "css" | "js";

const HOOK_NAMES: Record<LayoutHookType, string> = {
  html: "oppb_layout_html",
  css: "oppb_layout_css",
  js: "oppb_layout_js",
};

const UNSAVED_LAYOUT_ID_PLACEHOLDER = "YOUR_LAYOUT_ID";

/**
 * PHP guard that scopes a hook callback to one layout.
 * Uses the numeric ID when saved; otherwise a placeholder to replace after saving.
 */
export function getLayoutIdGuard(returnVar: string, layoutId: number | null): string {
  const id = layoutId ?? UNSAVED_LAYOUT_ID_PLACEHOLDER;
  const comment =
    layoutId === null
      ? "\n    // Replace YOUR_LAYOUT_ID with your saved layout ID (shown in the editor after saving)."
      : "";

  return `\n    if ( $layout_id !== ${id} ) {\n        return $${returnVar};\n    }${comment}\n`;
}

function getInlineLayoutIdGuard(returnVar: string, layoutId: number | null): string {
  const id = layoutId ?? UNSAVED_LAYOUT_ID_PLACEHOLDER;
  const comment =
    layoutId === null ? "\n    // Replace YOUR_LAYOUT_ID with your saved layout ID after saving." : "";

  return `if ( $layout_id !== ${id} ) {
        return $${returnVar};
    }${comment}`;
}

/**
 * Generate a ready-to-paste add_filter snippet for a layout hook.
 */
export function generateLayoutHookSnippet(
  hookType: LayoutHookType,
  layoutId: number | null,
  elementId?: string,
): string {
  const hookName = HOOK_NAMES[hookType];
  const layoutCheck = getLayoutIdGuard(hookType, layoutId);
  const elementComment = elementId ? `\n    // Target element: ${elementId}` : "";

  const paramName = hookType === "html" ? "html" : hookType === "css" ? "css" : "js";

  return `add_filter( '${hookName}', function ( $${paramName}, $layout_id, $context ) {${layoutCheck}${elementComment}
    // Your custom ${hookType.toUpperCase()} here

    return $${paramName};
}, 10, 3 );`;
}

/**
 * Generate snippets for all three layout hooks.
 */
export function generateAllLayoutHookSnippets(layoutId: number | null): Record<LayoutHookType, string> {
  return {
    html: generateLayoutHookSnippet("html", layoutId),
    css: generateLayoutHookSnippet("css", layoutId),
    js: generateLayoutHookSnippet("js", layoutId),
  };
}

/**
 * Generate a combined PHP file header with all three hooks.
 */
export function generateCombinedHookSnippet(layoutId: number | null): string {
  const snippets = generateAllLayoutHookSnippets(layoutId);
  return `<?php
/**
 * Custom layout code for Opus Price Builder
 * Add to your theme's functions.php or a custom plugin.
 */

${snippets.html}

${snippets.css}

${snippets.js}
`;
}

/**
 * Generate AI result as filter snippets (for copy from AI Helper).
 */
export function generateAiFilterSnippets(
  layoutId: number | null,
  customHtml: string,
  customCss: string,
  customJs: string,
): { html: string; css: string; js: string; combined: string } {
  const layoutCheckHtml = getInlineLayoutIdGuard("html", layoutId);
  const layoutCheckCss = getInlineLayoutIdGuard("css", layoutId);
  const layoutCheckJs = getInlineLayoutIdGuard("js", layoutId);

  const escapedHtml = customHtml.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
  const escapedCss = customCss.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
  const escapedJs = customJs.replace(/\\/g, "\\\\").replace(/'/g, "\\'");

  const htmlSnippet = `add_filter( 'oppb_layout_html', function ( $html, $layout_id, $context ) {
    ${layoutCheckHtml}
    return '${escapedHtml}';
}, 10, 3 );`;

  const cssSnippet = `add_filter( 'oppb_layout_css', function ( $css, $layout_id, $context ) {
    ${layoutCheckCss}
    $css .= '\\n${escapedCss}';
    return $css;
}, 10, 3 );`;

  const jsSnippet = customJs
    ? `add_filter( 'oppb_layout_js', function ( $js, $layout_id, $context ) {
    ${layoutCheckJs}
    $js .= '\\n${escapedJs}';
    return $js;
}, 10, 3 );`
    : "";

  return {
    html: htmlSnippet,
    css: cssSnippet,
    js: jsSnippet,
    combined: `<?php\n\n${htmlSnippet}\n\n${cssSnippet}${jsSnippet ? `\n\n${jsSnippet}` : ""}\n`,
  };
}
