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

APP="${CGHG_APP:-/home/cghggujarat/laravel_apps/cghg_portal}"
OLD_WEB="${CGHG_OLD_WEB:-/home/cghggujarat/public_html/website}"
PHP82="/opt/cpanel/ea-php82/root/usr/bin/php"
[ -x "$PHP82" ] || PHP82="$(command -v php)"
OWNER="cghggujarat:cghggujarat"
STAMP="$(date +%Y%m%d-%H%M%S)"
BACKUP="$APP/_migration_backups/website-logo-fit-v4-$STAMP"

say(){ printf '\n\033[1;36m%s\033[0m\n' "$1"; }
fail(){ printf '\n\033[1;31mERROR: %s\033[0m\n' "$1" >&2; exit 1; }

[ -d "$APP" ] || fail "Laravel app not found: $APP"
[ -f "$APP/artisan" ] || fail "artisan not found"
[ -f "$APP/resources/views/website/layout.blade.php" ] || fail "Website layout not found"
[ -f "$APP/resources/views/website/home.blade.php" ] || fail "Website home view not found"

cd "$APP"
mkdir -p "$BACKUP" public/assets/website
/bin/cp -a resources/views/website/layout.blade.php "$BACKUP/layout.blade.php.before"
/bin/cp -a resources/views/website/home.blade.php "$BACKUP/home.blade.php.before"
[ -f public/assets/website/logo.png ] && /bin/cp -a public/assets/website/logo.png "$BACKUP/logo.png.before" || true

cat > "$BACKUP/ROLLBACK.sh" <<ROLLBACK
#!/usr/bin/env bash
set -e
APP="$APP"
BACKUP="$BACKUP"
/bin/cp -af "\$BACKUP/layout.blade.php.before" "\$APP/resources/views/website/layout.blade.php"
/bin/cp -af "\$BACKUP/home.blade.php.before" "\$APP/resources/views/website/home.blade.php"
if [ -f "\$BACKUP/logo.png.before" ]; then
  /bin/cp -af "\$BACKUP/logo.png.before" "\$APP/public/assets/website/logo.png"
fi
chown cghggujarat:cghggujarat "\$APP/resources/views/website/layout.blade.php" "\$APP/resources/views/website/home.blade.php" 2>/dev/null || true
chown cghggujarat:cghggujarat "\$APP/public/assets/website/logo.png" 2>/dev/null || true
cd "\$APP"
runuser -u cghggujarat -- env HOME=/home/cghggujarat /opt/cpanel/ea-php82/root/usr/bin/php artisan optimize:clear || true
echo "ROLLBACK COMPLETE"
ROLLBACK
chmod 750 "$BACKUP/ROLLBACK.sh"

rollback_on_error(){
  code=$?
  echo
  echo "PATCH FAILED - restoring previous website files..."
  bash "$BACKUP/ROLLBACK.sh" || true
  exit "$code"
}
trap rollback_on_error ERR

say "1/5  Ensuring original Home Guards logo asset"
if [ ! -s public/assets/website/logo.png ]; then
  SRC=""
  if [ -s "$OLD_WEB/logo.png" ]; then
    SRC="$OLD_WEB/logo.png"
  else
    SRC="$(find "$OLD_WEB" -maxdepth 3 -type f \( -iname 'logo.png' -o -iname '*home*guard*logo*.png' -o -iname '*logo*.png' -o -iname '*logo*.jpg' -o -iname '*logo*.jpeg' \) 2>/dev/null | head -1 || true)"
  fi
  [ -n "$SRC" ] && [ -s "$SRC" ] || fail "Original Home Guards logo could not be found under $OLD_WEB"
  /bin/cp -af "$SRC" public/assets/website/logo.png
  echo "LOGO_SOURCE=$SRC"
else
  echo "LOGO_SOURCE=existing Laravel asset"
fi

say "2/5  Fitting logo in header, hero and footer"
python3 - <<'PY'
from pathlib import Path

layout = Path('resources/views/website/layout.blade.php')
home = Path('resources/views/website/home.blade.php')

s = layout.read_text()

# Header: replace generated HG seal with the original Home Guards logo.
s = s.replace(
    '<span class="brand-mark" aria-hidden="true"><span>HG</span></span>',
    '<span class="brand-mark brand-logo-shell"><img src="{{ asset(\'assets/website/logo.png\') }}" alt="Home Guards Gujarat Logo"></span>'
)

# Footer: use the same original logo.
s = s.replace(
    '<div class="footer-mark">HG</div>',
    '<div class="footer-mark footer-logo-shell"><img src="{{ asset(\'assets/website/logo.png\') }}" alt="Home Guards Gujarat Logo"></div>'
)

css = r'''
        /* CGHG_HOME_GUARD_LOGO_FIT_V4 */
        .brand-logo-shell{padding:5px;background:#fff!important;border:1px solid #dbe4ee!important;overflow:hidden}
        .brand-logo-shell:before{pointer-events:none;z-index:2}
        .brand-logo-shell img{position:relative;z-index:1;width:100%;height:100%;object-fit:contain;object-position:center;border-radius:10px;background:#fff}
        .footer-logo-shell{padding:5px!important;background:#fff!important;overflow:hidden}
        .footer-logo-shell img{width:100%;height:100%;object-fit:contain;object-position:center;border-radius:9px;background:#fff}
        .official-logo-stage{width:210px;height:210px;border-radius:32px;background:#fff;display:grid;place-items:center;padding:18px;position:relative;box-shadow:0 28px 58px rgba(0,0,0,.24);overflow:hidden}
        .official-logo-stage:before{content:"";position:absolute;inset:0;border-top:6px solid var(--saffron2);border-bottom:6px solid var(--green2);pointer-events:none}
        .official-logo-stage img{width:100%;height:100%;object-fit:contain;object-position:center;position:relative;z-index:1;filter:drop-shadow(0 10px 18px rgba(8,36,74,.12))}
        @media(max-width:720px){.official-logo-stage{width:145px;height:145px;border-radius:24px;padding:13px}.brand-logo-shell{padding:4px}.footer-logo-shell{padding:4px!important}}
'''

if 'CGHG_HOME_GUARD_LOGO_FIT_V4' not in s:
    s = s.replace('</style>', css + '\n    </style>', 1)

layout.write_text(s)

h = home.read_text()
h = h.replace(
    '<div class="hero-logo-wrap"><div class="national-seal" aria-label="Home Guards Gujarat"></div></div>',
    '<div class="hero-logo-wrap"><div class="official-logo-stage"><img src="{{ asset(\'assets/website/logo.png\') }}" alt="Home Guards Gujarat Logo"></div></div>'
)
# Also handle V2-style hero if V3 was not applied cleanly.
h = h.replace(
    '<div class="hero-logo-wrap"><img src="{{ asset(\'assets/website/logo.png\') }}" alt="Home Guards Gujarat"></div>',
    '<div class="hero-logo-wrap"><div class="official-logo-stage"><img src="{{ asset(\'assets/website/logo.png\') }}" alt="Home Guards Gujarat Logo"></div></div>'
)
home.write_text(h)

print('HEADER_LOGO=UPDATED')
print('HERO_LOGO=UPDATED')
print('FOOTER_LOGO=UPDATED')
PY

say "3/5  Ownership + permissions"
chown "$OWNER" resources/views/website/layout.blade.php resources/views/website/home.blade.php public/assets/website/logo.png
chmod 644 resources/views/website/layout.blade.php resources/views/website/home.blade.php public/assets/website/logo.png

say "4/5  Laravel validation"
"$PHP82" -l routes/web.php
runuser -u cghggujarat -- env HOME=/home/cghggujarat "$PHP82" artisan optimize:clear
runuser -u cghggujarat -- env HOME=/home/cghggujarat "$PHP82" artisan view:cache
runuser -u cghggujarat -- env HOME=/home/cghggujarat "$PHP82" artisan view:clear

grep -q 'CGHG_HOME_GUARD_LOGO_FIT_V4' resources/views/website/layout.blade.php
grep -q 'official-logo-stage' resources/views/website/home.blade.php
[ -s public/assets/website/logo.png ]

say "5/5  Complete"
echo "HOME_GUARD_LOGO=OK"
echo "HEADER=ORIGINAL_LOGO"
echo "HOME_HERO=ORIGINAL_LOGO"
echo "FOOTER=ORIGINAL_LOGO"
echo "BACKUP=$BACKUP"
echo "PREVIEW=https://cghggujarat.in/new/portalarea-laravel-preview/website"
echo "DONE"
