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

APP="/home/cghggujarat/laravel_apps/cghg_portal"
PHP82="/opt/cpanel/ea-php82/root/usr/bin/php"
STAMP="$(date +%Y%m%d-%H%M%S)"
BACKUP="$APP/_migration_backups/website-highlight-v8-$STAMP"
LAYOUT="$APP/resources/views/website/layout.blade.php"

fail(){ echo "ERROR: $*" >&2; exit 1; }
say(){ printf '\n===== %s =====\n' "$1"; }

[ -f "$LAYOUT" ] || fail "Website layout not found: $LAYOUT"
[ -f "$APP/artisan" ] || fail "artisan missing"

mkdir -p "$BACKUP"
cp -a "$LAYOUT" "$BACKUP/layout.blade.php.before"

cat > "$BACKUP/ROLLBACK.sh" <<ROLLBACK
#!/usr/bin/env bash
set -e
cp -af "$BACKUP/layout.blade.php.before" "$LAYOUT"
chown cghggujarat:cghggujarat "$LAYOUT" 2>/dev/null || true
cd "$APP"
runuser -u cghggujarat -- env HOME=/home/cghggujarat "$PHP82" artisan optimize:clear >/dev/null 2>&1 || true
echo "ROLLBACK COMPLETE"
ROLLBACK
chmod 750 "$BACKUP/ROLLBACK.sh"

say "1/5 INSTALLING STRONGER LIGHT HIGHLIGHTS"

python3 - <<'PY'
from pathlib import Path
p = Path("/home/cghggujarat/laravel_apps/cghg_portal/resources/views/website/layout.blade.php")
s = p.read_text()

start = "/* CGHG_EXECUTIVE_HIGHLIGHT_V8_START */"
end   = "/* CGHG_EXECUTIVE_HIGHLIGHT_V8_END */"

if start in s and end in s:
    a = s.index(start)
    b = s.index(end) + len(end)
    s = s[:a] + s[b:]

block = r'''
<style>
/* CGHG_EXECUTIVE_HIGHLIGHT_V8_START */

.hero{
    background:
        radial-gradient(circle at 10% 18%,rgba(255,153,51,.18),transparent 31%),
        radial-gradient(circle at 92% 76%,rgba(19,136,8,.14),transparent 32%),
        linear-gradient(135deg,#fff8ef 0%,#ffffff 47%,#f1faf3 100%) !important;
}
.hero:before{width:230px!important;height:7px!important}
.hero:after{width:230px!important;height:7px!important}

.eyebrow{
    background:#fff7ed!important;
    border:1px solid rgba(242,139,47,.30)!important;
    color:#c75f08!important;
    box-shadow:0 8px 20px rgba(242,139,47,.10)!important;
}

.hero h1{
    position:relative;
    display:inline-block;
    padding-bottom:8px;
}
.hero h1:after{
    content:"";
    position:absolute;
    left:0;
    bottom:0;
    width:122px;
    height:6px;
    border-radius:999px;
    background:linear-gradient(90deg,#f28b2f 0 52%,#138a45 52%);
}
.hero h1 .guj{
    display:inline-block!important;
    background:linear-gradient(90deg,#fff1df,#eef9f1)!important;
    padding:4px 13px 6px!important;
    border-radius:10px!important;
    border-left:4px solid #f28b2f!important;
    border-right:4px solid #138a45!important;
    margin-top:16px!important;
}

.hero p{
    background:rgba(255,255,255,.74);
    border-left:3px solid rgba(242,139,47,.55);
    padding:10px 13px;
    border-radius:0 10px 10px 0;
}
.hero .guj-copy{border-left-color:rgba(19,138,69,.55)!important}

.btn-primary{
    border:1px solid rgba(214,109,16,.18)!important;
    box-shadow:0 12px 28px rgba(242,139,47,.24)!important;
}
.btn-green{box-shadow:0 12px 28px rgba(19,138,69,.22)!important}
.btn-light{
    border:1px solid rgba(19,138,69,.25)!important;
    box-shadow:0 8px 20px rgba(19,138,69,.08)!important;
}

.hero-panel{
    border:1px solid rgba(19,138,69,.16)!important;
    box-shadow:0 26px 60px rgba(36,78,47,.13)!important;
}
.hero-panel small{
    display:inline-block!important;
    padding:6px 9px!important;
    background:#fff5e9!important;
    color:#b95808!important;
    border-radius:8px!important;
}
.hero-logo-wrap{
    border:1px solid #edf1ed;
    box-shadow:inset 0 0 0 1px rgba(255,255,255,.7);
}

.section-head{
    position:relative;
    padding-bottom:16px;
}
.section-head:after{
    content:"";
    position:absolute;
    left:0;
    bottom:0;
    width:100%;
    height:1px;
    background:linear-gradient(90deg,rgba(242,139,47,.55),rgba(242,139,47,.08) 34%,rgba(19,138,69,.08) 66%,rgba(19,138,69,.55));
}
.section-head .kicker{
    display:inline-flex!important;
    align-items:center;
    min-height:28px;
    padding:4px 10px!important;
    background:#fff3e6!important;
    border:1px solid rgba(242,139,47,.20)!important;
    border-radius:999px!important;
    color:#c75f08!important;
}
.section-head h2{color:#1e2c22!important}
.section-accent{
    width:124px!important;
    height:6px!important;
    box-shadow:0 2px 8px rgba(19,138,69,.08);
}

.mission{
    border:1px solid rgba(19,138,69,.12)!important;
    box-shadow:0 12px 30px rgba(36,75,44,.08)!important;
    transition:.2s ease;
}
.mission:hover{
    transform:translateY(-4px);
    box-shadow:0 20px 38px rgba(36,75,44,.12)!important;
}
.mission:nth-child(odd){
    background:linear-gradient(145deg,#fff5e8,#fff 65%)!important;
    border-color:rgba(242,139,47,.22)!important;
}
.mission:nth-child(even){
    background:linear-gradient(145deg,#eef9f1,#fff 65%)!important;
    border-color:rgba(19,138,69,.20)!important;
}
.mission:before{width:6px!important}
.mission-no{
    width:46px!important;
    height:46px!important;
    border:1px solid rgba(242,139,47,.18);
    box-shadow:0 8px 18px rgba(242,139,47,.10);
}
.mission:nth-child(even) .mission-no{
    border-color:rgba(19,138,69,.16);
    box-shadow:0 8px 18px rgba(19,138,69,.09);
}
.mission h3{color:#243127!important;font-weight:800!important}

.leadership-shell{
    position:relative;
    padding:30px!important;
    border:1px solid rgba(19,138,69,.16)!important;
    box-shadow:0 22px 48px rgba(36,75,44,.10)!important;
}
.leadership-shell:before{
    content:"";
    position:absolute;
    left:28px;
    right:28px;
    top:0;
    height:6px;
    border-radius:0 0 999px 999px;
    background:linear-gradient(90deg,#f28b2f 0 33.33%,#fff 33.33% 66.66%,#138a45 66.66%);
}
.leader{
    border:1px solid rgba(19,138,69,.13)!important;
    box-shadow:0 11px 28px rgba(36,75,44,.08)!important;
    transition:.2s ease;
}
.leader:hover{
    transform:translateY(-4px);
    box-shadow:0 18px 36px rgba(36,75,44,.13)!important;
}
.leader-body{background:linear-gradient(135deg,#ffffff,#f8fbf8)!important}
.leader:nth-child(odd) .leader-body{background:linear-gradient(135deg,#fffaf4,#ffffff)!important}
.leader h3{font-weight:800!important;color:#1f2c22!important}

.quick-card{box-shadow:0 12px 30px rgba(36,75,44,.08)!important}
.quick-card:nth-child(1){
    background:linear-gradient(145deg,#fff5e8,#fff)!important;
    border-color:rgba(242,139,47,.20)!important;
}
.quick-card:nth-child(2){
    background:linear-gradient(145deg,#f2faf4,#fff)!important;
    border-color:rgba(19,138,69,.18)!important;
}
.quick-card:nth-child(3){
    background:linear-gradient(145deg,#fff9f2,#f5fbf6)!important;
    border-color:rgba(19,138,69,.14)!important;
}

.page-hero{
    background:
        radial-gradient(circle at 10% 20%,rgba(242,139,47,.12),transparent 25%),
        radial-gradient(circle at 90% 75%,rgba(19,138,69,.10),transparent 28%),
        #fff!important;
}
.page-hero h1{
    display:inline-block;
    position:relative;
    padding-bottom:8px;
}
.page-hero h1:after{
    content:"";
    position:absolute;
    left:0;
    bottom:0;
    width:110px;
    height:5px;
    border-radius:999px;
    background:linear-gradient(90deg,#f28b2f 0 50%,#138a45 50%);
}

.contact-card,.content-card,.side-card,.app-panel{
    box-shadow:0 14px 32px rgba(36,75,44,.08)!important;
}
.contact-card{background:linear-gradient(145deg,#fffaf4,#fff)!important}
.contact-card:nth-child(even){background:linear-gradient(145deg,#f2faf4,#fff)!important}

nav a.active{
    background:#eaf8ee!important;
    box-shadow:inset 0 0 0 1px rgba(19,138,69,.10);
}
.officer-login{box-shadow:0 12px 28px rgba(19,138,69,.24)!important}
.brand-logo{box-shadow:0 10px 24px rgba(36,75,44,.10)!important}

footer{position:relative}
footer:after{
    content:"";
    position:absolute;
    left:0;
    right:0;
    bottom:0;
    height:5px;
    background:linear-gradient(90deg,#ff9933 0 33.33%,#fff 33.33% 66.66%,#138808 66.66%);
}

/* CGHG_EXECUTIVE_HIGHLIGHT_V8_END */
</style>
'''

if "</head>" not in s:
    raise SystemExit("ERROR: </head> not found")

s = s.replace("</head>", block + "\n</head>", 1)
p.write_text(s)
print("V8 highlight CSS inserted")
PY

say "2/5 OWNERSHIP"
chown cghggujarat:cghggujarat "$LAYOUT"
chmod 644 "$LAYOUT"

say "3/5 VALIDATION"
grep -q "CGHG_EXECUTIVE_HIGHLIGHT_V8_START" "$LAYOUT"
grep -q "leadership-shell:before" "$LAYOUT"
grep -q "hero h1:after" "$LAYOUT"

say "4/5 CLEAR + REBUILD VIEW CACHE"
cd "$APP"
runuser -u cghggujarat -- env HOME=/home/cghggujarat "$PHP82" artisan optimize:clear
runuser -u cghggujarat -- env HOME=/home/cghggujarat "$PHP82" artisan view:cache

say "5/5 DONE"
echo "LIGHT_THEME=KEPT"
echo "STRONGER_HIGHLIGHTS=OK"
echo "MISSION_HIGHLIGHT=OK"
echo "LEADERSHIP_HIGHLIGHT=OK"
echo "ALL_LEADERSHIP_CARDS=UNIFORM"
echo "OFFICER_LOGIN=UNCHANGED"
echo "BACKUP=$BACKUP"
echo "ROLLBACK=bash $BACKUP/ROLLBACK.sh"
echo "PREVIEW=https://cghggujarat.in/new/portalarea-laravel-preview/website"
echo "CGHG WEBSITE HIGHLIGHT POLISH V8 INSTALLED"
