<?php
use Core\System\ThemeManager;

/* ========================
   DATA
======================== */
$footer   = $footer ?? [];
$sections = $footer['sections'] ?? [];

/**
 * ==========================================================
 * SETTINGS (Unified via ThemeManager)
 * ==========================================================
 */
$footerLogo  = ThemeManager::getSetting('footer_logo', '');
$description = ThemeManager::getSetting('site_description', '');
$siteName    = ThemeManager::getSetting('site_name', 'MTEngine');

$colors  = ThemeManager::getSetting('colors', []);
$primary = $colors['primary'] ?? '#007bff';

/* ========================
   RTL / LTR (Single Source of Truth)
======================== */
$lang = ThemeManager::getSetting('default_language', 'ar');
$lang = strtolower(trim((string)$lang));
if ($lang === '') {
    $lang = 'ar';
}

$isRtl = in_array($lang, ['ar','fa','he','ur'], true);
$dir   = $isRtl ? 'rtl' : 'ltr';
$align = $isRtl ? 'right' : 'left';

/* ========================
   FIX LOGO PATH
======================== */
$basePath = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
$logoUrl  = $footerLogo ? $basePath . $footerLogo : '';

/* ========================
   ORDER SECTIONS
======================== */
usort($sections, fn($a,$b) => ($a['position'] ?? 0) <=> ($b['position'] ?? 0));

/* تقسيم الأقسام */
$leftSections   = [];
$rightSections  = [];

foreach ($sections as $s) {
    if ($s['type'] === 'links') {
        $leftSections[] = $s;
    } else {
        $rightSections[] = $s;
    }
}
?>

<style>
.site-footer{
    background:#f8f8f8;
    padding:60px 0 30px;
    border-top:1px solid #e0e0e0;
}
.footer-container{
    max-width:1200px;
    margin:auto;
    padding:0 20px;
    display:grid;
    grid-template-columns:repeat(4,1fr);
    gap:50px;
}
.footer-col h3{
    font-size:16px;
    margin-bottom:15px;
    color:<?= $primary ?>;
    font-weight:700;
}
.footer-col ul{list-style:none;padding:0;margin:0}
.footer-col ul li{margin-bottom:8px}
.footer-col ul li a{
    color:#444;
    text-decoration:none;
    transition:.2s;
}
.footer-col ul li a:hover{color:<?= $primary ?>}

/* BRAND */
.footer-brand img{max-width:160px;margin-bottom:15px}
.footer-brand p{font-size:14px;color:#555;line-height:1.8}

/* SOCIAL */
.footer-socials a{
    font-size:20px;
    margin-inline-end:12px;
    color:#444;
    transition:.25s;
}
.footer-socials a:hover{
    color:<?= $primary ?>;
    transform:translateY(-3px);
}

/* CONTACT */
.footer-contacts a{
    display:flex;
    align-items:center;
    gap:8px;
    margin-bottom:10px;
    color:#444;
    font-size:14px;
}
.footer-contacts a.whatsapp{color:#25D366}
.footer-contacts a:hover{color:<?= $primary ?>}

/* BOTTOM */
.site-footer-bottom{
    text-align:center;
    margin-top:40px;
    padding-top:20px;
    border-top:1px solid #ddd;
    font-size:14px;
    color:#555;
}

@media(max-width:992px){
    .footer-container{grid-template-columns:repeat(2,1fr)}
}
@media(max-width:576px){
    .footer-container{grid-template-columns:1fr;text-align:center}
}
</style>

<footer class="site-footer" dir="<?= $dir ?>">

<div class="footer-container">

    <!-- COLUMN 1 : DESCRIPTION -->
    <div class="footer-col footer-brand" style="text-align:<?= $align ?>">
        <h3><?= __('footer.about_site','About the store') ?></h3>
        <?php if ($description): ?>
            <p><?= htmlspecialchars($description, ENT_QUOTES, 'UTF-8') ?></p>
        <?php endif; ?>
    </div>

    <!-- COLUMN 2 : LINKS (STACKED) -->
    <div class="footer-col" style="text-align:<?= $align ?>">
        <?php foreach (array_slice($leftSections,0,2) as $section): ?>
            <h3><?= htmlspecialchars($section['title'], ENT_QUOTES, 'UTF-8') ?></h3>
            <ul>
                <?php foreach ($section['items'] as $item): ?>
                    <li>
                        <a href="<?= htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8') ?>">
                            <?= htmlspecialchars($item['label'], ENT_QUOTES, 'UTF-8') ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endforeach; ?>
    </div>

    <!-- COLUMN 3 : LINKS / CONTACT -->
    <div class="footer-col" style="text-align:<?= $align ?>">
        <?php foreach (array_slice($rightSections,0,2) as $section): ?>

            <h3><?= htmlspecialchars($section['title'], ENT_QUOTES, 'UTF-8') ?></h3>

            <?php if ($section['type']==='social'): ?>
                <div class="footer-socials">
                    <?php foreach ($section['items'] as $item): ?>
                        <a href="<?= htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8') ?>" target="_blank" rel="noopener">
                            <i class="<?= htmlspecialchars($item['icon'], ENT_QUOTES, 'UTF-8') ?>"></i>
                        </a>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

            <?php if ($section['type']==='contact'): ?>
                <div class="footer-contacts">
                    <?php foreach ($section['items'] as $item): ?>
                        <?php if ($item['type']==='phone'): ?>
                            <a href="tel:<?= htmlspecialchars($item['value'], ENT_QUOTES, 'UTF-8') ?>">
                                <i class="bi bi-telephone"></i><?= __('footer.call_us','Call us') ?>
                            </a>
                        <?php endif; ?>
                        <?php if ($item['type']==='whatsapp'): ?>
                            <a class="whatsapp"
                               href="https://wa.me/<?= preg_replace('/\D+/','',$item['value']) ?>"
                               rel="noopener">
                                <i class="bi bi-whatsapp"></i><?= __('footer.whatsapp','WhatsApp') ?>
                            </a>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

        <?php endforeach; ?>
    </div>

    <!-- COLUMN 4 : LOGO -->
    <div class="footer-col footer-brand" style="text-align:<?= $align ?>">
        <?php if ($logoUrl): ?>
            <img src="<?= htmlspecialchars($logoUrl, ENT_QUOTES, 'UTF-8') ?>"
                 alt="<?= htmlspecialchars($siteName, ENT_QUOTES, 'UTF-8') ?>"
                 loading="lazy" decoding="async">
        <?php endif; ?>
    </div>

</div>

<div class="site-footer-bottom">
    <?= htmlspecialchars($footer['copyright'] ?? ('© '.date('Y').' '.$siteName), ENT_QUOTES, 'UTF-8') ?>
</div>

</footer>

<!-- JS (deferred) -->
<script src="/themes/matjari/assets/js/sticky.js" defer></script>
<script src="/themes/matjari/assets/js/products-slider.js" defer></script>
<script src="/themes/matjari/assets/js/hero-slider.js" defer></script>
<script src="/themes/matjari/assets/js/top-bar.js" defer></script>
<script src="/themes/matjari/assets/js/search-overlay.js" defer></script>
