<?php
require_once __DIR__ . '/includes/i18n.php';
$pageTitle = t('home_page_title');
$pageDescription = t('home_meta_description');
$activePage = 'home';
?>
<!DOCTYPE html>
<html lang="<?= currentLang() ?>">
<head>
<?php include __DIR__ . '/includes/head.php'; ?>
</head>
<body>
<?php include __DIR__ . '/includes/header.php'; ?>

<main id="main-content">
    <section class="hero">
        <div class="hero-content">
            <div class="anniversary-badge">
                <span><?= t('home_anniversary_badge') ?></span>
            </div>
            <h1>
                <?= t('home_hero_title') ?>
                <span class="highlight"><?= t('home_hero_subtitle') ?></span>
            </h1>
            <p class="hero-tagline"><?= t('home_hero_tagline') ?></p>
            <div class="hero-details">
                <div class="hero-detail">
                    <svg class="hero-detail-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/>
                    </svg>
                    <div class="hero-detail-text">
                        <strong><?= t('home_hero_stat1_strong') ?></strong>
                        <?= t('home_hero_stat1_text') ?>
                    </div>
                </div>
                <div class="hero-detail">
                    <svg class="hero-detail-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                    <div class="hero-detail-text">
                        <strong><?= t('home_hero_stat2_strong') ?></strong>
                        <?= t('home_hero_stat2_text') ?>
                    </div>
                </div>
                <div class="hero-detail">
                    <svg class="hero-detail-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                    <div class="hero-detail-text">
                        <strong><?= t('home_hero_stat3_strong') ?></strong>
                        <?= t('home_hero_stat3_text') ?>
                    </div>
                </div>
            </div>
            <div class="hero-cta">
                <a href="<?= langUrl('join.php') ?>" class="btn-primary">
                    <?= t('btn_become_member') ?>
                    <svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 8l4 4m0 0l-4 4m4-4H3"/>
                    </svg>
                </a>
                <a href="<?= langUrl('about.php') ?>" class="btn-secondary"><?= t('btn_learn_more') ?></a>
            </div>
        </div>
        <div class="anniversary-seal">
            <svg class="anniversary-seal-img" viewBox="0 0 200 200">
                <circle cx="100" cy="100" r="95" fill="none" stroke="#c9a227" stroke-width="2"/>
                <circle cx="100" cy="100" r="80" fill="none" stroke="#c9a227" stroke-width="1" opacity="0.5"/>
                <text x="100" y="80" text-anchor="middle" fill="#c9a227" font-family="Cormorant Garamond" font-size="48" font-weight="600">30</text>
                <text x="100" y="108" text-anchor="middle" fill="#ffffff" font-family="Outfit" font-size="14" font-weight="400"><?= t('home_anniversary_seal_years') ?></text>
                <text x="100" y="128" text-anchor="middle" fill="#c9a227" font-family="Outfit" font-size="10" letter-spacing="2"><?= t('home_anniversary_seal_established') ?></text>
                <text x="100" y="148" text-anchor="middle" fill="#c9a227" font-family="Cormorant Garamond" font-size="16" font-weight="600">1996</text>
            </svg>
        </div>
    </section>

    <section id="latestStrip" style="display:none;padding:2.5rem 0;background:linear-gradient(135deg, #122640 0%, #0a1628 100%);border-top:2px solid #c9a227;border-bottom:2px solid #c9a227;position:relative;z-index:1;">
        <div style="max-width:1200px;margin:0 auto;padding:0 5%;">
            <div style="font-family:'Outfit',sans-serif;font-size:0.75rem;text-transform:uppercase;letter-spacing:3px;color:#c9a227;margin-bottom:1.25rem;text-align:center;"><?= t('home_latest_strip_label') ?></div>
        </div>
        <div class="latest-strip-scroll" id="latestStripScroll">
            <div class="latest-strip-track" id="latestStripGrid"></div>
        </div>
    </section>
    <style>
        .latest-strip-scroll { overflow-x: auto; scrollbar-width: thin; scrollbar-color: rgba(201,162,39,0.4) transparent; scroll-snap-type: x proximity; }
        .latest-strip-scroll::-webkit-scrollbar { height: 6px; }
        .latest-strip-scroll::-webkit-scrollbar-track { background: transparent; }
        .latest-strip-scroll::-webkit-scrollbar-thumb { background: rgba(201,162,39,0.4); border-radius:3px; }
        .latest-strip-track { display: flex; gap: 1.25rem; padding: 0 5% 0.5rem; min-width: 100%; width: max-content; margin: 0 auto; justify-content: center; box-sizing: border-box; }
        .latest-card { display:flex; flex-direction:column; flex: 0 0 300px; scroll-snap-align: start; padding:1.25rem 1.25rem 1rem; background:rgba(255,255,255,0.04); border:1px solid rgba(201,162,39,0.25); border-radius:8px; text-decoration:none; transition:background 0.2s, border-color 0.2s, transform 0.2s; }
        .latest-card:hover { background:rgba(255,255,255,0.07); border-color:rgba(201,162,39,0.5); transform:translateY(-2px); }
        .latest-card-label { font-family:'Outfit',sans-serif; font-size:0.7rem; text-transform:uppercase; letter-spacing:2px; color:#c9a227; margin-bottom:0.5rem; }
        .latest-card-title { font-family:'Cormorant Garamond',serif; font-size:1.25rem; font-weight:600; color:#fff; line-height:1.3; margin-bottom:0.5rem; }
        .latest-card-snippet { font-family:'Outfit',sans-serif; font-size:0.875rem; color:rgba(255,255,255,0.7); line-height:1.5; flex: 1; }
        .latest-card-meta { font-family:'Outfit',sans-serif; font-size:0.75rem; color:rgba(255,255,255,0.5); margin-top:0.75rem; }
    </style>

    <section class="value-prop">
        <div class="value-prop-container">
            <div class="section-label"><?= t('home_value_label') ?></div>
            <h2><?= t('home_value_title') ?></h2>
            <p class="value-prop-intro"><?= t('home_value_intro') ?></p>
            <div class="value-grid">
                <div class="value-card">
                    <svg class="value-card-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"/></svg>
                    <h3><?= t('home_value_card1_title') ?></h3>
                    <p><?= t('home_value_card1_desc') ?></p>
                </div>
                <div class="value-card">
                    <svg class="value-card-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
                    <h3><?= t('home_value_card2_title') ?></h3>
                    <p><?= t('home_value_card2_desc') ?></p>
                </div>
                <div class="value-card">
                    <svg class="value-card-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"/></svg>
                    <h3><?= t('home_value_card3_title') ?></h3>
                    <p><?= t('home_value_card3_desc') ?></p>
                </div>
                <div class="value-card">
                    <svg class="value-card-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M19 20H5a2 2 0 01-2-2V6a2 2 0 012-2h10a2 2 0 012 2v1m2 13a2 2 0 01-2-2V7m2 13a2 2 0 002-2V9a2 2 0 00-2-2h-2m-4-3H9M7 16h6M7 8h6v4H7V8z"/></svg>
                    <h3><?= t('home_value_card4_title') ?></h3>
                    <p><?= t('home_value_card4_desc') ?></p>
                </div>
                <a href="<?= langUrl('offers.php') ?>" class="value-card" style="text-decoration:none;color:inherit;display:block;">
                    <svg class="value-card-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"/></svg>
                    <h3><?= t('home_value_card5_title') ?></h3>
                    <p><?= t('home_value_card5_desc') ?> <span style="color: var(--gold);"><?= t('home_value_card5_link') ?></span></p>
                </a>
            </div>
        </div>
    </section>

    <section class="stats">
        <div class="stats-container">
            <div class="stat-item">
                <div class="stat-number">30</div>
                <div class="stat-label"><?= t('home_stat_years') ?></div>
            </div>
            <div class="stat-divider"></div>
            <div class="stat-item">
                <div class="stat-number">30,000+</div>
                <div class="stat-label"><?= t('home_stat_registrants') ?></div>
            </div>
            <div class="stat-divider"></div>
            <div class="stat-item">
                <div class="stat-number">3.8M+</div>
                <div class="stat-label"><?= t('home_stat_canadians') ?></div>
            </div>
            <div class="stat-divider"></div>
            <div class="stat-item">
                <div class="stat-number">40+</div>
                <div class="stat-label"><?= t('home_stat_members') ?></div>
            </div>
        </div>
    </section>

    <section class="cta-section">
        <div class="cta-box">
            <div class="section-label"><?= t('home_cta_label') ?></div>
            <h2><?= t('home_cta_title') ?></h2>
            <p><?= t('home_cta_text') ?></p>
            <a href="<?= langUrl('join.php') ?>" class="btn-primary">
                <?= t('home_cta_button') ?>
                <svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 8l4 4m0 0l-4 4m4-4H3"/>
                </svg>
            </a>
        </div>
    </section>
</main>

<?php include __DIR__ . '/includes/footer.php'; ?>
<?php include __DIR__ . '/includes/scripts.php'; ?>

<script>
    window.FID_HOME_I18N = {
        news_label: <?= json_encode(t('home_strip_news_label'), JSON_UNESCAPED_UNICODE) ?>,
        event_label: <?= json_encode(t('home_strip_event_label'), JSON_UNESCAPED_UNICODE) ?>,
        member_event_label: <?= json_encode(t('home_strip_member_event_label'), JSON_UNESCAPED_UNICODE) ?>,
        advocacy_label: <?= json_encode(t('home_strip_advocacy_label'), JSON_UNESCAPED_UNICODE) ?>,
        virtual: <?= json_encode(t('home_strip_virtual'), JSON_UNESCAPED_UNICODE) ?>,
        letter_to_prefix: 'Comment letter to '
    };

    (function loadLatestStrip() {
        const DAY_MS = 24 * 60 * 60 * 1000;
        const ONE_MONTH_MS = 30 * DAY_MS;
        const ONE_WEEK_MS = 7 * DAY_MS;
        const now = Date.now();
        const cards = [];
        const lang = window.FID_LANG || 'en';
        const newsHref = (slug) => (lang === 'fr' ? '/fr/news.php#' : '/news.php#') + encodeURIComponent(slug);
        const eventsHref = (slug) => (lang === 'fr' ? '/fr/events.php#' : '/events.php#') + encodeURIComponent(slug);
        const advocacyHref = () => lang === 'fr' ? '/fr/advocacy.php' : '/advocacy.php';

        function escapeHtml(s) { if (!s) return ''; const div = document.createElement('div'); div.textContent = s; return div.innerHTML; }
        function decodeEntities(s) { if (!s) return ''; const div = document.createElement('div'); div.innerHTML = s; return div.textContent; }
        function formatDate(d) {
            const locale = lang === 'fr' ? 'fr-CA' : 'en-CA';
            return new Date(d + (d.length === 10 ? 'T00:00:00' : '')).toLocaleDateString(locale, { month: 'long', day: 'numeric', year: 'numeric' });
        }
        function eventStartMs(e) { return new Date(e.event_date + 'T' + (e.start_time || '00:00:00')).getTime(); }
        function eventEndMs(e) { const endDate = e.event_end_date || e.event_date; return new Date(endDate + 'T' + (e.end_time || '23:59:59')).getTime(); }

        const newsP = fetch('/api/news-public.php?limit=10').then(r => r.json()).then(d => {
            if (!d.success || !d.articles) return;
            d.articles.forEach(a => {
                const articleDate = new Date(a.date);
                if (now - articleDate.getTime() > ONE_MONTH_MS) return;
                if (articleDate.getTime() > now + DAY_MS) return;
                const slug = a.slug || String(a.id);
                cards.push({
                    dateMs: articleDate.getTime(),
                    label: window.FID_HOME_I18N.news_label,
                    title: decodeEntities(a.title),
                    snippet: a.excerpt,
                    meta: a.date,
                    href: newsHref(slug)
                });
            });
        }).catch(() => {});

        const eventsP = fetch('/api/events-public.php?limit=20').then(r => r.json()).then(d => {
            if (!d.success || !d.events) return;
            const seen = new Set();
            d.events.forEach(e => {
                const startMs = eventStartMs(e);
                if (eventEndMs(e) < now) return;
                const untilStart = startMs - now;
                if (untilStart >= 0 && untilStart < ONE_WEEK_MS) {
                    seen.add(e.id);
                    const venue = e.venue_name || (e.is_virtual ? window.FID_HOME_I18N.virtual : '');
                    cards.push({
                        dateMs: startMs,
                        label: e.is_members_only ? window.FID_HOME_I18N.member_event_label : window.FID_HOME_I18N.event_label,
                        title: e.name,
                        snippet: formatDate(e.event_date) + (venue ? ' · ' + venue : ''),
                        href: eventsHref(e.slug || String(e.id))
                    });
                }
            });
            const nextPublic = d.events.find(e => !e.is_members_only && eventEndMs(e) >= now);
            if (nextPublic && !seen.has(nextPublic.id)) {
                const venue = nextPublic.venue_name || (nextPublic.is_virtual ? window.FID_HOME_I18N.virtual : '');
                cards.push({
                    dateMs: eventStartMs(nextPublic),
                    label: window.FID_HOME_I18N.event_label,
                    title: nextPublic.name,
                    snippet: formatDate(nextPublic.event_date) + (venue ? ' · ' + venue : ''),
                    href: eventsHref(nextPublic.slug || String(nextPublic.id))
                });
            }
        }).catch(() => {});

        const advocacyP = fetch(advocacyHref()).then(r => r.text()).then(html => {
            const m = html.match(/title:\s*"([^"]+)"[\s\S]{0,200}?date:\s*"(\d{4}-\d{2}-\d{2})"[\s\S]{0,200}?recipient:\s*"([^"]+)"/);
            if (!m) return;
            const letterDate = new Date(m[2] + 'T00:00:00');
            if (now - letterDate.getTime() > ONE_MONTH_MS) return;
            cards.push({
                dateMs: letterDate.getTime(),
                label: window.FID_HOME_I18N.advocacy_label,
                title: m[1],
                snippet: window.FID_HOME_I18N.letter_to_prefix + m[3],
                meta: formatDate(m[2]),
                href: advocacyHref()
            });
        }).catch(() => {});

        Promise.all([newsP, eventsP, advocacyP]).then(() => {
            if (cards.length === 0) return;
            cards.sort((a, b) => Math.abs(a.dateMs - now) - Math.abs(b.dateMs - now));
            const grid = document.getElementById('latestStripGrid');
            grid.innerHTML = cards.map(c => `
                <a href="${c.href}" class="latest-card">
                    <div class="latest-card-label">${escapeHtml(c.label)}</div>
                    <div class="latest-card-title">${escapeHtml(c.title)}</div>
                    ${c.snippet ? `<div class="latest-card-snippet">${escapeHtml(c.snippet)}</div>` : ''}
                    ${c.meta ? `<div class="latest-card-meta">${escapeHtml(c.meta)}</div>` : ''}
                </a>
            `).join('');
            document.getElementById('latestStrip').style.display = 'block';
        });
    })();
</script>
</body>
</html>
