#!/usr/bin/env bash
set -euo pipefail

ROOT="/home/bitnami/htdocs"
TMP_BRIDGE="$(mktemp)"
trap 'rm -f "$TMP_BRIDGE"' EXIT

cat > "$TMP_BRIDGE" <<'CSS'
/*
 * Btec local icon bridge
 * Compatibilidad local para clases Font Awesome 6 sobre assets locales FA5 + Boxicons.
 */

.fa-solid,
.fa-regular,
.fa-brands {
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  display: inline-block;
  font-style: normal;
  font-variant: normal;
  text-rendering: auto;
  line-height: 1;
}

.fa-solid {
  font-family: "Font Awesome 5 Free";
  font-weight: 900;
}

.fa-regular {
  font-family: "Font Awesome 5 Free";
  font-weight: 400;
}

.fa-brands {
  font-family: "Font Awesome 5 Brands";
  font-weight: 400;
}

@keyframes btec-fa-beat {
  0%, 100% { transform: scale(1); }
  45% { transform: scale(1.14); }
}

@keyframes btec-fa-bounce {
  0%, 100% { transform: translateY(0); }
  30% { transform: translateY(-0.3em); }
  60% { transform: translateY(0.08em); }
}

@keyframes btec-fa-fade {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

@keyframes btec-fa-beat-fade {
  0%, 100% { opacity: 1; transform: scale(1); }
  45% { opacity: 0.45; transform: scale(1.15); }
}

@keyframes btec-fa-shake {
  0%, 100% { transform: rotate(0deg); }
  20% { transform: rotate(-10deg); }
  40% { transform: rotate(8deg); }
  60% { transform: rotate(-6deg); }
  80% { transform: rotate(4deg); }
}

.fa-beat { animation: btec-fa-beat 1s ease-in-out infinite; }
.fa-bounce { animation: btec-fa-bounce 1s ease infinite; }
.fa-fade { animation: btec-fa-fade 1.3s ease-in-out infinite; }
.fa-beat-fade { animation: btec-fa-beat-fade 1.3s ease-in-out infinite; }
.fa-shake { animation: btec-fa-shake 1.15s ease-in-out infinite; }

/* Alias FA6 -> FA5 local */
.fa-magnifying-glass:before { content: "\f002"; }
.fa-xmark:before { content: "\f00d"; }
.fa-pen-to-square:before { content: "\f044"; }
.fa-triangle-exclamation:before { content: "\f071"; }
.fa-up-right-from-square:before { content: "\f35d"; }
.fa-tower-broadcast:before { content: "\f519"; }
.fa-van-shuttle:before { content: "\f5b6"; }
.fa-cart-shopping:before { content: "\f07a"; }
.fa-face-smile-wink:before { content: "\f4da"; }

/* Bridge puntual hacia Boxicons para clases legacy/custom no cubiertas bien por FA5 local */
.fa-zona:before,
.fa-hand-holding-dollar:before,
.fa-money-bill-transfer:before,
.fa-file-circle-check:before,
.fa-box-archive:before,
.fa-pencil:before,
.fa-shuffle:before,
.fa-building-flag:before,
.fa-building-circle-check:before,
.fa-sliders:before,
.fa-delete-left:before,
.fa-floppy-disk:before,
.fa-gears:before,
.fa-wand-magic-sparkles:before,
.fa-circle-exclamation:before,
.fa-person-circle-plus:before,
.fa-envelopes-bulk:before,
.fa-ticket:before,
.fa-shop-slash:before,
.fa-clock-rotate-left:before {
  font-family: "boxicons" !important;
  font-weight: normal !important;
  font-style: normal;
  line-height: 1;
}

.fa-zona:before { content: "\edbb"; }
.fa-hand-holding-dollar:before { content: "\ed10"; }
.fa-money-bill-transfer:before { content: "\eeb1"; }
.fa-file-circle-check:before { content: "\eca9"; }
.fa-box-archive:before { content: "\ee09"; }
.fa-pencil:before { content: "\ee14"; }
.fa-shuffle:before { content: "\ee71"; }
.fa-building-flag:before { content: "\ec6e"; }
.fa-building-circle-check:before { content: "\ec6e"; }
.fa-sliders:before { content: "\ee7a"; }
.fa-delete-left:before { content: "\ed96"; }
.fa-floppy-disk:before { content: "\ee57"; }
.fa-gears:before { content: "\ecd1"; }
.fa-wand-magic-sparkles:before { content: "\ec69"; }
.fa-circle-exclamation:before { content: "\ed2a"; }
.fa-person-circle-plus:before { content: "\eecb"; }
.fa-envelopes-bulk:before { content: "\ed24"; }
.fa-ticket:before { content: "\ee2e"; }
.fa-shop-slash:before { content: "\ee92"; }
.fa-clock-rotate-left:before { content: "\ed73"; }
CSS

python3 - <<'PY' "$ROOT" "$TMP_BRIDGE"
import os
import sys
from pathlib import Path

root = Path(sys.argv[1])
bridge_template = Path(sys.argv[2]).read_text(encoding='utf-8')
import_line = '@import url("icon-bridge.css");'

font_files = sorted(root.glob('**/assets/vendor/fonts/fontawesome.css'))
menu_files = sorted(root.glob('**/func/menu_modules.php'))

font_count = 0
menu_count = 0

for font_file in font_files:
    bridge_file = font_file.with_name('icon-bridge.css')
    bridge_file.write_text(bridge_template, encoding='utf-8')

    content = font_file.read_text(encoding='utf-8', errors='ignore')
    if import_line not in content:
        comment_end = content.find('*/')
        if comment_end != -1:
            content = content[:comment_end + 2] + '\n' + import_line + '\n' + content[comment_end + 2:]
        else:
            content = import_line + '\n' + content
        font_file.write_text(content, encoding='utf-8')
        font_count += 1

menu_map = {
    "'icon_class' => 'fa-solid fa-clock',": "'icon_class' => 'menu-icon tf-icons bx bx-time-five',",
    "'icon_class' => 'fa-solid fa-satellite-dish',": "'icon_class' => 'menu-icon tf-icons bx bx-broadcast',",
    "'icon_class' => 'fa-solid fa-chart-line',": "'icon_class' => 'menu-icon tf-icons bx bx-line-chart',",
    "'icon_class' => 'fa-solid fa-hand-holding-dollar',": "'icon_class' => 'menu-icon tf-icons bx bx-dollar-circle',",
    "'icon_class' => 'fa-solid fa-money-bill-transfer',": "'icon_class' => 'menu-icon tf-icons bx bx-transfer',",
    "'icon_class' => 'fa-solid fa-clipboard-list',": "'icon_class' => 'menu-icon tf-icons bx bx-clipboard',",
    "'icon_class' => 'fa-solid fa-upload',": "'icon_class' => 'menu-icon tf-icons bx bx-upload',",
    "'icon_class' => 'fa-solid fa-comments-dollar',": "'icon_class' => 'menu-icon tf-icons bx bx-money',",
    "'icon_class' => 'fa-solid fa-magnifying-glass',": "'icon_class' => 'menu-icon tf-icons bx bx-search',",
    "'icon_class' => 'fa-solid fa-file-circle-check',": "'icon_class' => 'menu-icon tf-icons bx bx-check-circle',",
    "'icon_class' => 'fa-solid fa-box-archive',": "'icon_class' => 'menu-icon tf-icons bx bx-package',",
    "'icon_class' => 'fa-solid fa-building',": "'icon_class' => 'menu-icon tf-icons bx bx-building-house',",
    "'icon_class' => 'fa-solid fa-gift',": "'icon_class' => 'menu-icon tf-icons bx bx-gift',",
    "'icon_class' => 'fa-solid fa-truck',": "'icon_class' => 'menu-icon tf-icons bx bx-car',",
}

for menu_file in menu_files:
    content = menu_file.read_text(encoding='utf-8', errors='ignore')
    original = content
    for old, new in menu_map.items():
        content = content.replace(old, new)
    if content != original:
        menu_file.write_text(content, encoding='utf-8')
        menu_count += 1

print(f'Font Awesome bridge actualizado en {len(font_files)} asset roots.')
print(f'fontawesome.css modificados: {font_count}')
print(f'menu_modules.php migrados a Boxicons: {menu_count}')
PY
