<?php
/** Expects $pricing (from Core\Pricing::resolvePublicPlans()) in scope. */
require_once ROOT_PATH . '/modules/marketing/views/partials/icons.php';
$activeNav = 'pricing';

$defaultFeatures = [
    'Full access to Projects, Contractors & Suppliers',
    'Invoices, budgets & expense tracking',
    'Daily progress reports with photos',
    'Role-based permissions & 2FA',
    'Unlimited team members',
    'Email & in-app notifications',
];

$countryNames = [
    'NG' => 'Nigeria', 'US' => 'United States', 'GB' => 'United Kingdom', 'GH' => 'Ghana',
    'KE' => 'Kenya', 'ZA' => 'South Africa', 'CA' => 'Canada', 'IN' => 'India', 'DE' => 'Germany',
    'FR' => 'France', 'AE' => 'United Arab Emirates',
];
$countryLabel = $countryNames[$pricing['country'] ?? ''] ?? null;

ob_start();
?>
<?php require __DIR__ . '/partials/header.php'; ?>

<section class="hx-hero" style="padding-bottom:96px;">
    <div class="hx-hero-glow hx-hero-glow-1"></div>
    <div class="hx-hero-glow hx-hero-glow-2"></div>
    <div class="hx-container" style="position:relative;z-index:1;text-align:center;">
        <span class="hx-eyebrow" style="justify-content:center;">Simple, transparent pricing</span>
        <h1 style="color:var(--hx-white);margin:0 auto 16px;max-width:640px;">One plan. Priced for where you are.</h1>
        <p class="hx-lede" style="max-width:520px;margin:0 auto 32px;">
            Start with a 7-day free trial — no credit card required. Prices are set by Helmyx and shown automatically in your local currency.
        </p>

        <div class="hx-price-region">
            <span style="width:16px;height:16px;color:var(--hx-slate-400);"><?= hxIcon('globe') ?></span>
            <span>
                <?= $countryLabel ? 'Showing prices for ' . htmlspecialchars($countryLabel) : 'Showing prices in' ?>
            </span>
            <?php if (count($pricing['availableCurrencies']) > 1): ?>
            <select id="hxCurrencySelect" aria-label="Choose currency">
                <?php foreach ($pricing['availableCurrencies'] as $code): ?>
                <option value="<?= htmlspecialchars($code) ?>" <?= $code === $pricing['currency'] ? 'selected' : '' ?>><?= htmlspecialchars($code) ?></option>
                <?php endforeach; ?>
            </select>
            <?php else: ?>
            <strong style="color:var(--hx-navy-800);"><?= htmlspecialchars($pricing['currency']) ?></strong>
            <?php endif; ?>
        </div>
    </div>
</section>

<section class="hx-section" style="padding-top:56px;">
    <div class="hx-container">

        <?php if (empty($pricing['plans'])): ?>
            <div class="hx-price-empty">
                <p style="margin-bottom:0;">Pricing isn't configured yet — check back soon, or <a href="<?= APP_URL ?>/signup" style="color:var(--hx-navy-800);font-weight:600;">start your free trial</a> and we'll let you know before your trial ends.</p>
            </div>
        <?php else: ?>
            <?php $count = count($pricing['plans']); ?>
            <div class="hx-price-grid">
                <?php foreach ($pricing['plans'] as $i => $row):
                    $plan = $row['plan'];
                    // A single plan should always read as the confident, primary choice —
                    // the muted "outline" treatment only makes sense when there's a
                    // cheaper option nearby to compare it against.
                    $isMiddleOfMultiple = $count >= 2 && $i === (int)floor(($count - 1) / 2) + ($count === 2 ? 1 : 0);
                    $isFeatured = $count === 1 || $isMiddleOfMultiple;
                    $showBadge = $isFeatured && $count >= 2;
                    $features = [];
                    $decoded = json_decode($plan['features'] ?? '', true);
                    if (is_array($decoded) && !empty($decoded)) {
                        $features = $decoded;
                    } else {
                        $features = $defaultFeatures;
                    }
                ?>
                <div class="hx-price-card <?= $isFeatured ? 'hx-featured' : '' ?>">
                    <?php if ($showBadge): ?><span class="hx-price-badge">Recommended</span><?php endif; ?>
                    <div class="hx-price-name"><?= htmlspecialchars($plan['name']) ?></div>
                    <div class="hx-price-desc"><?= htmlspecialchars($plan['description'] ?? '') ?></div>
                    <div class="hx-price-amount">
                        <span class="hx-amount"><?= htmlspecialchars(formatCurrency((float)$row['amount'], $pricing['currency'])) ?></span>
                        <span class="hx-period">/ <?= htmlspecialchars($plan['billing_type']) ?></span>
                    </div>
                    <div class="hx-price-billed">Billed <?= $plan['billing_type'] === 'annual' ? 'yearly' : 'monthly' ?> · cancel anytime</div>
                    <ul class="hx-price-features">
                        <?php foreach ($features as $feature): ?>
                        <li><?= hxIcon('check') ?> <span><?= htmlspecialchars((string)$feature) ?></span></li>
                        <?php endforeach; ?>
                    </ul>
                    <a href="<?= APP_URL ?>/signup" class="hx-btn <?= $isFeatured ? 'hx-btn-primary' : 'hx-btn-outline' ?> hx-btn-block">Start free trial</a>
                </div>
                <?php endforeach; ?>
            </div>

            <div class="hx-trial-banner" style="margin-top:40px;">
                <?= hxIcon('check') ?>
                <span>Every plan starts with a 7-day free trial — no credit card required.</span>
            </div>
        <?php endif; ?>

        <div class="hx-faq">
            <details open>
                <summary>How does regional pricing work?</summary>
                <p>We detect your approximate location from your connection and show the price Helmyx has set for your currency. You can switch currencies manually above if we got it wrong, or if you're pricing this out for a team in another country.</p>
            </details>
            <details>
                <summary>Do I need a credit card to start?</summary>
                <p>No. Every organisation gets a 7-day free trial with full access — no card required. You'll only be asked to pay once the trial ends, if you choose to continue.</p>
            </details>
            <details>
                <summary>What happens when my trial ends?</summary>
                <p>You'll see a countdown in the app for the full 7 days. Once it ends, you'll be taken to a secure payment page to subscribe and pick up right where you left off — none of your data goes anywhere in the meantime.</p>
            </details>
            <details>
                <summary>Can I cancel anytime?</summary>
                <p>Yes. There's no long-term contract — plans are billed per period and you can stop at any time.</p>
            </details>
        </div>
    </div>
</section>

<section class="hx-cta">
    <div class="hx-container">
        <h2>Ready to steer your projects?</h2>
        <p>Start your 7-day free trial today. No credit card required.</p>
        <div class="hx-cta-ctas">
            <a href="<?= APP_URL ?>/signup" class="hx-btn hx-btn-white">Start free trial</a>
        </div>
    </div>
</section>

<?php require __DIR__ . '/partials/footer.php'; ?>
<?php
$content = ob_get_clean();
$pageTitle = 'Pricing — Helmyx';
$metaDescription = 'Simple, transparent Helmyx pricing shown automatically in your local currency. 7-day free trial, no credit card required.';
require ROOT_PATH . '/modules/shared/views/layouts/marketing.php';
?>
