<?php
/**
 * Emits a <script> block defining the same JS globals the original static
 * site's data.js used to (SITE, HERO_SLIDES, SERVICES, DOWNLOADS,
 * TALLY_FEATURES, TALLY_PILLARS, INDUSTRIES, TESTIMONIALS, TEAM, GALLERY) —
 * but populated live from the database. This lets assets/js/main.js's
 * existing slider/carousel/render logic keep working unchanged, now driven
 * by editable admin content instead of hardcoded data.
 *
 * Include this AFTER header.php/functions.php and BEFORE footer.php's
 * main.js <script> tag (footer.php includes it automatically — see below).
 * Set $bridgeSections to an array of keys to limit which globals are built,
 * e.g. ['site','services'] on the services page. Defaults to all.
 */
$bridgeSections = $bridgeSections ?? ['site', 'hero', 'services', 'downloads', 'tally_features', 'industries', 'testimonials', 'team', 'gallery'];
$pdo = db();
$settings = $settings ?? getSettings();
$data = [];

if (in_array('site', $bridgeSections, true)) {
    $data['SITE'] = [
        'name' => $settings['site_name'] ?? '',
        'phone' => $settings['phone'] ?? '',
        'whatsapp' => $settings['whatsapp'] ?? '',
        'email' => $settings['email'] ?? '',
        'address' => $settings['address'] ?? '',
        'mapEmbed' => $settings['map_embed_url'] ?? '',
    ];
}

if (in_array('hero', $bridgeSections, true)) {
    $slides = $pdo->query('SELECT * FROM hero_slides WHERE is_active = 1 ORDER BY sort_order ASC, id ASC')->fetchAll();
    $data['HERO_SLIDES'] = array_map(fn($s) => [
        'image' => $s['image_path'] ? assetUrl($s['image_path']) : 'https://picsum.photos/seed/hero-' . $s['id'] . '/1600/700',
        'badge' => $s['badge'],
        'title' => $s['title'],
        'subtitle' => $s['subtitle'],
        'ctaText' => $s['cta_text'],
        'ctaLink' => $s['cta_link'],
        'offer' => $s['offer'],
    ], $slides);
}

if (in_array('services', $bridgeSections, true)) {
    $services = $pdo->query('SELECT * FROM services WHERE is_active = 1 ORDER BY sort_order ASC, id ASC')->fetchAll();
    $data['SERVICES'] = array_map(fn($s) => [
        'slug' => $s['slug'],
        'icon' => $s['icon'],
        'title' => $s['title'],
        'short' => $s['short_desc'],
        'description' => $s['description'],
        'banner' => $s['banner_gradient'],
        'features' => jdecode($s['features']),
        'faq' => jdecode($s['faqs']),
    ], $services);
}

if (in_array('downloads', $bridgeSections, true)) {
    $downloads = $pdo->query('SELECT * FROM downloads WHERE is_active = 1 ORDER BY id ASC')->fetchAll();
    $data['DOWNLOADS'] = array_map(fn($d) => [
        'id' => (int) $d['id'],
        'category' => $d['category'],
        'title' => $d['title'],
        'version' => $d['version'],
        'date' => $d['release_date'],
        'size' => $d['file_size'],
        'downloads' => (int) $d['download_count'],
        'file' => $d['file_path'] ? assetUrl($d['file_path']) : 'download.php?id=' . $d['id'],
    ], $downloads);
}

if (in_array('tally_features', $bridgeSections, true)) {
    $features = $pdo->query('SELECT * FROM tally_features WHERE is_active = 1 ORDER BY sort_order ASC, id ASC')->fetchAll();
    $data['TALLY_FEATURES'] = array_map(fn($f) => [
        'icon' => $f['icon'],
        'tag' => $f['tag'],
        'title' => $f['title'],
        'desc' => $f['description'],
        'banner' => $f['banner_gradient'],
    ], $features);

    $data['TALLY_PILLARS'] = [
        ['icon' => 'bi-receipt-cutoff', 'title' => 'Smart, Professional Invoicing', 'desc' => "Create polished invoices with your logo, watermark, terms and new pre-defined templates — and let AI-powered Docs by TallyIra auto-create entries from scanned invoices, cutting manual data entry by up to 80%."],
        ['icon' => 'bi-shield-lock-fill', 'title' => 'Connected Banking, Security & Reconciliation', 'desc' => 'Make direct payments and get instant reconciliation with ICICI, Axis, SBI and Kotak — backed by stronger Tally Vault encryption and granular user-access controls.'],
        ['icon' => 'bi-graph-up-arrow', 'title' => 'Smarter Insights & Compliance', 'desc' => 'SmartFind unified search, Schedule III reporting for companies under Rs 250 Cr net worth, and sharper GST/e-invoicing insights with HSN summary breakups.'],
    ];
}

if (in_array('industries', $bridgeSections, true)) {
    $industries = $pdo->query('SELECT * FROM industries WHERE is_active = 1 ORDER BY sort_order ASC, id ASC')->fetchAll();
    $data['INDUSTRIES'] = array_map(fn($i) => ['name' => $i['name'], 'icon' => $i['icon']], $industries);
}

if (in_array('testimonials', $bridgeSections, true)) {
    $testimonials = $pdo->query('SELECT * FROM testimonials WHERE is_active = 1 ORDER BY sort_order ASC, id ASC')->fetchAll();
    $data['TESTIMONIALS'] = array_map(fn($t) => [
        'name' => $t['name'],
        'company' => $t['company'],
        'text' => $t['message'],
        'rating' => (int) $t['rating'],
    ], $testimonials);
}

if (in_array('team', $bridgeSections, true)) {
    $team = $pdo->query('SELECT * FROM team_members WHERE is_active = 1 ORDER BY sort_order ASC, id ASC')->fetchAll();
    $data['TEAM'] = array_map(fn($m) => [
        'name' => $m['name'],
        'role' => $m['role'],
        'initials' => $m['initials'],
        'photo' => $m['photo_path'] ? assetUrl($m['photo_path']) : null,
    ], $team);
}

if (in_array('gallery', $bridgeSections, true)) {
    $gallery = $pdo->query('SELECT * FROM gallery_items WHERE is_active = 1 ORDER BY sort_order ASC, id ASC')->fetchAll();
    $data['GALLERY'] = array_map(fn($g) => [
        'img' => assetUrl($g['image_path']),
        'caption' => $g['caption'],
    ], $gallery);
}
?>
<script>
<?php foreach ($data as $globalName => $value): ?>
const <?= $globalName ?> = <?= json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ?>;
<?php endforeach; ?>
</script>
