#!/usr/bin/perl
# SEO + artikel Next.js — Perl saja, tanpa script JS panjang.
# Head diganti in-place; footer HTML = footerText JSON (tanpa table/nav).
use strict;
use warnings;
use FindBin;
use File::Spec;

local $/;
my $html = <STDIN> // '';

my $title          = 'WISMATOTO | Link Panduan Main Slot Online Gampang Maxwin Hari ini!';
my $description    = 'Wismatoto hadirkan panduan lengkap cara dapatkan sensational pada slot online supaya profit hari ini lebih maksimal lewat penerapan teknik khusus pola harian tawaran freebet pendaftar baru link alternatif resmi paling aman.';
my $canonical      = 'https://wismabang.store/';
my $keywords       = 'wismatoto, wisma toto';
my $site_name      = 'WISMATOTO';
my $alternate_base = 'https://m.wismabang.store/';

sub json_escape {
    my ($s) = @_;
    $s = '' unless defined $s;
    $s =~ s/\\/\\\\/g;
    $s =~ s/"/\\"/g;
    $s =~ s/\r//g;
    $s =~ s/\n/\\n/g;
    return $s;
}

sub encode_next_html {
    my ($s) = @_;
    $s = '' unless defined $s;
    $s =~ s/\\/\\\\/g;
    $s =~ s/"/\\"/g;
    $s =~ s/</\\u003c/g;
    $s =~ s/>/\\u003e/g;
    $s =~ s/&/\\u0026/g;
    $s =~ s/\r\n/\\n/g;
    $s =~ s/\n/\\n/g;
    return $s;
}

my $artikel_file = File::Spec->catfile($FindBin::Bin, 'artikel.html');
open my $fh, '<:encoding(UTF-8)', $artikel_file
    or die "Gagal baca $artikel_file: $!\n";
my $artikel = do { local $/; <$fh> };
close $fh;
$artikel =~ s/^\s+|\s+$//g;
$artikel =~ s/\r\n/ /g;
$artikel =~ s/\n/ /g;
$artikel =~ s/\s{2,}/ /g;

my $footer_html = '<div class="text-common py-3"><h1 class="text-center mb-4 text-2xl text-common font-bold w-full">'
    . $title
    . '</h1><div class="title-underline mt-2 w-full"></div>'
    . $artikel
    . '</div>';

my $tjson = json_escape($title);
my $djson = json_escape($description);
my $kjson = json_escape($keywords);
my $cjson = json_escape($canonical);
my $footer_json = encode_next_html($footer_html);

my $desc_tag = '<meta name="description" content="' . $description . '">';
my $kw_tag   = '<meta name="keywords" content="' . $keywords . '">';
my $can_tag  = '<link rel="canonical" href="' . $canonical . '">';
my $extra = join '',
    '<meta property="og:title" content="' . $title . '">',
    '<meta property="og:description" content="' . $description . '">',
    '<meta property="og:url" content="' . $canonical . '">',
    '<meta property="og:site_name" content="' . $site_name . '">',
    '<link rel="amphtml" href="' . $alternate_base . '">',
    '<link rel="alternate" href="' . $alternate_base . '" hreflang="id">',
    '<link rel="alternate" href="' . $alternate_base . '" hreflang="en">',
    '<link rel="alternate" href="' . $alternate_base . '" hreflang="x-default">',
    '<link rel="alternate" hreflang="id-ID" href="' . $alternate_base . '">',
    '<link rel="alternate" hreflang="en-US" href="' . $alternate_base . '">',
    '<link rel="alternate" hreflang="en-SG" href="' . $alternate_base . '">';

# Title hanya di <head>.
$html =~ s{(<head\b[^>]*>.*?<title\b[^>]*>).*?(</title>)}{$1$title$2}is;

# Description / keywords / canonical — ganti atau sisip sekali.
$html =~ s{<meta\b(?=[^>]*\bname\s*=\s*["']description["'])[^>]*>}{$desc_tag}gis;
if ($html =~ /<meta\b(?=[^>]*\bname\s*=\s*["']keywords["'])/i) {
    $html =~ s{<meta\b(?=[^>]*\bname\s*=\s*["']keywords["'])[^>]*>}{$kw_tag}gis;
} else {
    $html =~ s/\Q$desc_tag\E/$desc_tag$kw_tag/;
}

if ($html =~ /<link\b(?=[^>]*\brel\s*=\s*["']canonical["'])/i) {
    $html =~ s{<link\b(?=[^>]*\brel\s*=\s*["']canonical["'])[^>]*?/?>}{$can_tag}gis;
} else {
    $html =~ s{</head>}{$can_tag</head>}i;
}

# Hapus amp/hreflang lama (body + head), pasang satu paket setelah canonical.
$html =~ s{<link\b(?=[^>]*\brel\s*=\s*["']amphtml["'])[^>]*?/?>}{}gis;
$html =~ s{<link\b(?=[^>]*\brel\s*=\s*["']alternate["'])(?=[^>]*\bhreflang\s*=)[^>]*?/?>}{}gis;
$html =~ s{<meta\b(?=[^>]*\bproperty\s*=\s*["']og:(?:title|description|url|site_name)["'])[^>]*>}{}gis;

unless ($html =~ /rel=["']amphtml["']/i) {
    $html =~ s/\Q$can_tag\E/$can_tag$extra/
        or $html =~ s{</head>}{$extra</head>}i;
}

# Footer: buang menu/tabel dari HTML; ganti artikel.
unless ($html =~ /\Q$footer_html\E/) {
    my $ok = $html =~ s{
        (?:<link\b(?=[^>]*\brel\s*=\s*["']amphtml["'])[^>]*?/?>\s*)?
        <style\b[^>]*>.*?\.menu-close-button.*?</style>
        \s*<nav\b[^>]*class=["'][^"']*\bmenu\b[^"']*["'][^>]*>.*?</nav>
        \s*<div\b[^>]*class=["'][^"']*text-common[^"']*py-3[^"']*["'][^>]*>\s*<table\b.*?</table>\s*</div>
        \s*<div\b[^>]*class=["'][^"']*text-common[^"']*py-3[^"']*["'][^>]*>
        \s*<h1\b[^>]*>.*?</h1>
        \s*<div\b[^>]*title-underline[^>]*>\s*</div>
        (?:\s*<p\b[^>]*>.*?</p>)*
        \s*</div>
    }{$footer_html}six;

    unless ($ok) {
        $html =~ s{<style\b[^>]*>.*?\.menu-close-button.*?</style>}{}gis;
        $html =~ s{<nav\b[^>]*class=["'][^"']*\bmenu\b[^"']*["'][^>]*>.*?</nav>}{}gis;
        $html =~ s{
            <div\b[^>]*class=["'][^"']*text-common[^"']*py-3[^"']*["'][^>]*>
            \s*<table\b.*?</table>
            \s*</div>
        }{}gisx;
        $html =~ s{
            <div\b[^>]*class=["'][^"']*text-common[^"']*py-3[^"']*["'][^>]*>
            \s*<h1\b[^>]*>.*?</h1>
            \s*<div\b[^>]*title-underline[^>]*>\s*</div>
            (?:\s*<p\b[^>]*>.*?</p>)*
            \s*</div>
        }{$footer_html}six;
    }
}

# Samakan payload Next.js (ini yang mencegah Next nulis ulang nilai lama).
$html =~ s/"websiteTitle"\s*:\s*"(?:\\.|[^"\\])*"/"websiteTitle":"$tjson"/g;
$html =~ s/"websiteDescription"\s*:\s*"(?:\\.|[^"\\])*"/"websiteDescription":"$djson"/g;
$html =~ s/"websiteKeyword"\s*:\s*"(?:\\.|[^"\\])*"/"websiteKeyword":"$kjson"/g;
$html =~ s/"canonicalUrl"\s*:\s*"(?:\\.|[^"\\])*"/"canonicalUrl":"$cjson"/g;
# websiteName biarkan (WISMA TOTO) — dipakai copyright footer.
$html =~ s/"footerText"\s*:\s*"(?:\\.|[^"\\])*"/"footerText":"$footer_json"/g;

print $html;
