#!/usr/bin/perl
# SEO + artikel moneysite wismatoto.com (mobile + PC sinkron).
# Tabel/nav/style TETAP — hanya artikel diganti (anti double footer).
use strict;
use warnings;
use FindBin;
use File::Spec;

local $/;
my $html = <STDIN> // '';

# === Moneysite WISMATOTO ===
my $title          = 'WISMATOTO: Alternative Link For The Most Trusted Online Game Site 2026';
my $description    = 'WISMATOTO menyediakan link alternatif untuk login dan daftar pada platform game online terbesar dengan akses tanpa kendala dan anti nawala.';
my $canonical      = 'https://www.wismatoto.com/';
my $keywords       = 'wismatoto, wisma toto, wismatoto.com, wismatoto login, wismatoto slot';
my $site_name      = 'WISMATOTO';
my $alternate_base = 'https://wismatoto-akses.com/';
my $artikel_name   = 'artikel.html';
my $logo_url       = 'https://www.wismatoto.com/public/assets/images/icon/wisma-toto-logo.png';
# === /Moneysite ===

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_name);
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 $artikel_inner = '<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;
my $footer_html  = '<div class="text-common py-3">' . $artikel_inner . '</div>';
my $artikel_json = encode_next_html($artikel_inner);

my $tjson = json_escape($title);
my $djson = json_escape($description);
my $kjson = json_escape($keywords);
my $cjson = json_escape($canonical);

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 . '">';

my $schema_tag = '<script type="application/ld+json" data-seo-schema="1">'
    . '{"@context":"https://schema.org","@graph":['
    . '{"@type":"Organization","@id":"' . json_escape($canonical) . '#organization"'
    . ',"name":"' . json_escape($site_name) . '"'
    . ',"alternateName":"WISMA TOTO"'
    . ',"url":"' . json_escape($canonical) . '"'
    . ',"logo":{"@type":"ImageObject","url":"' . json_escape($logo_url) . '"}'
    . ',"description":"' . json_escape($description) . '"}'
    . ',{"@type":"WebSite","@id":"' . json_escape($canonical) . '#website"'
    . ',"url":"' . json_escape($canonical) . '"'
    . ',"name":"' . json_escape($site_name) . '"'
    . ',"publisher":{"@id":"' . json_escape($canonical) . '#organization"}'
    . ',"inLanguage":"id-ID"}]}'
    . '</script>';

# Title hanya di <head>.
$html =~ s{(<head\b[^>]*>.*?<title\b[^>]*>).*?(</title>)}{$1$title$2}is;

$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;
}

# Amphtml body: update href saja (jangan hapus node).
$html =~ s{(<link\b(?=[^>]*\brel\s*=\s*["']amphtml["'])[^>]*\bhref\s*=\s*["'])[^"']+}{$1$alternate_base}gi;
$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;

# OG + amp + hreflang di head (sekali).
unless ($html =~ /property=["']og:title["']/i) {
    $html =~ s/\Q$can_tag\E/$can_tag$extra/
        or $html =~ s{</head>}{$extra</head>}i;
}

unless ($html =~ /data-seo-schema=["']1["']/) {
    $html =~ s{</head>}{$schema_tag</head>}i;
}

# Artikel saja diganti. Tabel / nav / style TETAP.
unless ($html =~ /\Q$footer_html\E/) {
    $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;
}

$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;

# footerText: patch artikel saja; table/nav/style di JSON tetap.
$html =~ s{
    \\u003cdiv\s+class=\\"text-common\s+py-3\\"\\u003e
    (?:\\n|\s)*
    \\u003ch1(?:\\.|[^"])*?\\u003c/h1\\u003e
    (?:\\n|\s)*
    \\u003cdiv\s+class=\\"title-underline[^"]*\\"\\u003e\\u003c/div\\u003e
    (?:(?:\\n|\s)*\\u003cp(?:\\.|[^"])*?\\u003c/p\\u003e)*
    (?:\\n|\s)*
    \\u003c/div\\u003e
}{\\u003cdiv class=\\"text-common py-3\\"\\u003e$artikel_json\\u003c/div\\u003e}sx;

$html =~ s{\\u003clink rel=\\"amphtml\\" href=\\"[^"]*\\"}
          {encode_next_html('<link rel="amphtml" href="' . $alternate_base . '"')}ge;

print $html;
