<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/FreeTier.php';
require_once __DIR__ . '/includes/PricingCatalog.php';

$uiLang = dbnToolsCurrentLanguage();
$isNorwegian = $uiLang === 'no';
$isAuthed = dbnToolsIsAuthenticated();
$currentTier = $isAuthed ? dbnToolsCurrentTier() : 'free';
$surveyDone = false;
if ($isAuthed && dbnToolsIsFreeTier()) {
    $surveyDone = FreeTier::hasCompletedSurvey((int)$_SESSION['dbn_tools_sso_uid']);
}

$status = (string)($_GET['status'] ?? '');
$loginUrl = 'https://dobetternorge.no/tools-login.php?return=' . urlencode('/pricing.php');
$surveyUrl = 'https://dobetternorge.no/survey.php';
$orgUrl = 'mailto:support@dobetternorge.no?subject=DBN%20Tools%20Organisasjon';

function h(mixed $value): string
{
    return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}

function nok(int $amount): string
{
    return PricingCatalog::formatNok($amount);
}

function credits(int $amount): string
{
    return PricingCatalog::formatCredits($amount);
}

$copy = $isNorwegian ? [
    'title' => 'Priser - DBN Tools',
    'description' => 'NOK-priser, kreditter og abonnement for tools.dobetternorge.no.',
    'eyebrow' => 'NOK-priser for DBN Tools',
    'headline' => 'Kreditter som gir mening',
    'subhead' => 'Månedlige kreditter brukes først. Forhåndsbetalte kreditter legges på toppen og utløper ikke.',
    'trial' => 'Pluss har 14 dagers prøveperiode. Kort kreves, og du kan kansellere når som helst.',
    'survey_title' => 'Få 25 ekstra kreditter',
    'survey_text' => 'Svar på fem korte spørsmål om hvordan du bruker verktøyene.',
    'survey_cta' => 'Ta undersøkelsen',
    'current' => 'Din plan',
    'choose' => 'Velg',
    'login' => 'Logg inn for å velge',
    'available' => 'Tilgjengelig',
    'topups_title' => 'Ekstra kreditter',
    'topups_lead' => 'Top-ups er engangskjøp. De utløper ikke og brukes etter månedlige kreditter.',
    'buy' => 'Kjøp',
    'login_buy' => 'Logg inn for å kjøpe',
    'tool_costs' => 'Verktøykostnader',
    'tool_costs_lead' => 'Kreditter trekkes bare når verktøyet fullfører med et gyldig resultat.',
    'organisation' => 'Organisasjon',
    'organisation_price' => 'Kontakt',
    'organisation_text' => 'For rådgivere, frivillige miljøer og større familieteam som trenger flere brukere, særskilte avtaler eller onboarding.',
    'contact' => 'Snakk med oss',
    'billing_note' => 'Stripe brukes for kortbetaling, abonnement og kvitteringer. Lokale DBN-kreditter er fasiten for tilgang.',
    'status_success' => 'Betalingen er bekreftet. Kontoen oppdateres når Stripe-webhooken er behandlet.',
    'status_canceled' => 'Betalingen ble avbrutt. Ingen endringer er gjort.',
    'connecting' => 'Kobler til Stripe...',
    'checkout_error' => 'Kunne ikke starte betaling. Prøv igjen.',
] : [
    'title' => 'Pricing - DBN Tools',
    'description' => 'NOK pricing, credits, and subscriptions for tools.dobetternorge.no.',
    'eyebrow' => 'NOK pricing for DBN Tools',
    'headline' => 'Credits that make sense',
    'subhead' => 'Monthly credits are spent first. Prepaid credits sit on top and never expire.',
    'trial' => 'Plus includes a 14-day trial. Card required, cancel anytime.',
    'survey_title' => 'Get 25 extra credits',
    'survey_text' => 'Answer five short questions about how you use the tools.',
    'survey_cta' => 'Take the survey',
    'current' => 'Current plan',
    'choose' => 'Choose',
    'login' => 'Log in to choose',
    'available' => 'Available',
    'topups_title' => 'Extra credits',
    'topups_lead' => 'Top-ups are one-time purchases. They never expire and are spent after monthly credits.',
    'buy' => 'Buy',
    'login_buy' => 'Log in to buy',
    'tool_costs' => 'Tool costs',
    'tool_costs_lead' => 'Credits are charged only when a tool completes with a valid result.',
    'organisation' => 'Organisation',
    'organisation_price' => 'Contact',
    'organisation_text' => 'For advisers, volunteer groups, and larger family teams that need more users, custom terms, or onboarding.',
    'contact' => 'Talk to us',
    'billing_note' => 'Stripe handles cards, subscriptions, and receipts. Local DBN credits remain authoritative for access.',
    'status_success' => 'Payment confirmed. Your account updates when the Stripe webhook is processed.',
    'status_canceled' => 'Payment was canceled. No changes were made.',
    'connecting' => 'Connecting to Stripe...',
    'checkout_error' => 'Could not start checkout. Please try again.',
];

$plans = PricingCatalog::plans();
$topups = PricingCatalog::topups();
$planFeaturesNo = [
    'free' => ['30 kreditter per måned', 'Verktøy på innlimt tekst', 'Juridisk korpussøk', 'Ingen Min Sak-lagring'],
    'plus' => ['250 kreditter per måned', '500 MB Min Sak-lagring', '1 bruker', '14 dagers prøveperiode'],
    'pro' => ['900 kreditter per måned', '5 GB Min Sak-lagring', '3 brukere', 'Full Azure-modellrute'],
];
$planFeaturesEn = [
    'free' => ['30 credits per month', 'Tools on pasted text', 'Legal corpus search', 'No My Case storage'],
    'plus' => ['250 credits per month', '500 MB My Case storage', '1 user', '14-day trial'],
    'pro' => ['900 credits per month', '5 GB My Case storage', '3 users', 'Full Azure model route'],
];
$planFeatures = $isNorwegian ? $planFeaturesNo : $planFeaturesEn;

$toolCostRows = [
    ['0', 'search, corpus-search, clarify-only gates'],
    ['1', 'ask, extract, summarize, translate, korrespond_refine'],
    ['2', 'timeline, redact'],
    ['3', 'barnevernet, advocate, korrespond, legal-analysis'],
    ['4', 'discrepancy'],
    ['6', 'deep-research'],
    [$isNorwegian ? 'variabel' : 'variable', $isNorwegian ? 'transcribe: 1 kreditt per startet lydminutt, minst 5' : 'transcribe: 1 credit per started audio minute, minimum 5'],
];
?>
<!doctype html>
<html lang="<?= h($uiLang) ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= h($copy['title']) ?></title>
    <meta name="description" content="<?= h($copy['description']) ?>">
    <link rel="canonical" href="https://tools.dobetternorge.no/pricing.php">
    <meta name="theme-color" content="#00205B">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Crimson+Pro:wght@400;600;700&family=IBM+Plex+Sans:wght@400;500;600;700&display=swap">
    <link rel="stylesheet" href="assets/css/tools.css">
    <link rel="stylesheet" href="assets/css/dbn-tools-redesign.css">
    <style>
        :root { --dbn-navy:#00205B; --dbn-red:#BA0C2F; --dbn-ink:#111827; --dbn-muted:#5b6472; --dbn-line:#d9dee8; --dbn-soft:#f7f9fc; --dbn-green:#0f766e; }
        body { margin:0; background:#fbfcfe; color:var(--dbn-ink); font-family:'IBM Plex Sans', system-ui, sans-serif; }
        .pricing-shell { max-width:1180px; margin:0 auto; padding:28px 20px 64px; }
        .lang-bar { display:flex; justify-content:flex-end; gap:8px; font-size:0.86rem; margin-bottom:18px; }
        .lang-bar a { color:var(--dbn-muted); text-decoration:none; padding:4px 8px; border-radius:6px; }
        .lang-bar a.is-active { background:var(--dbn-navy); color:#fff; }
        .pricing-hero { display:grid; grid-template-columns:minmax(0, 1.2fr) minmax(280px, .8fr); gap:28px; align-items:end; padding:28px 0 34px; border-bottom:1px solid var(--dbn-line); }
        .eyebrow { margin:0 0 8px; color:var(--dbn-red); font-size:.82rem; font-weight:700; text-transform:uppercase; letter-spacing:.08em; }
        h1 { margin:0; font-family:'Crimson Pro', serif; font-size:clamp(2.2rem, 4vw, 4rem); line-height:1; letter-spacing:0; }
        .hero-copy { margin:14px 0 0; color:var(--dbn-muted); font-size:1.05rem; max-width:680px; }
        .hero-note { background:#fff; border:1px solid var(--dbn-line); border-left:5px solid var(--dbn-red); border-radius:8px; padding:18px 18px; color:#263244; }
        .status-pill { display:inline-flex; margin:22px 0 0; padding:9px 13px; border-radius:8px; font-size:.92rem; background:#fff7ed; color:#9a3412; border:1px solid #fed7aa; }
        .status-pill.success { background:#ecfdf5; color:#065f46; border-color:#a7f3d0; }
        .survey-banner { margin:26px 0 0; display:flex; align-items:center; justify-content:space-between; gap:16px; background:var(--dbn-navy); color:#fff; border-radius:8px; padding:18px 20px; }
        .survey-banner h2 { margin:0 0 4px; font-size:1.1rem; }
        .survey-banner p { margin:0; color:rgba(255,255,255,.82); }
        .btn, .pricing-cta { border:0; display:inline-flex; align-items:center; justify-content:center; min-height:42px; padding:0 15px; border-radius:8px; font-weight:700; text-decoration:none; cursor:pointer; line-height:1.1; }
        .btn-primary { background:var(--dbn-navy); color:#fff; }
        .btn-primary:hover { background:#001740; }
        .btn-light { background:#fff; color:var(--dbn-navy); }
        .btn-muted { background:#edf1f7; color:#263244; }
        .btn-current { background:#dcfce7; color:#166534; cursor:default; }
        .plans-grid { display:grid; grid-template-columns:repeat(4, minmax(0,1fr)); gap:16px; margin:28px 0; }
        .plan-card, .topup-card, .cost-panel { background:#fff; border:1px solid var(--dbn-line); border-radius:8px; padding:20px; }
        .plan-card { display:flex; flex-direction:column; min-height:390px; position:relative; }
        .plan-card.highlight { border-color:var(--dbn-navy); box-shadow:0 10px 30px rgba(0,32,91,.10); }
        .plan-badge { position:absolute; top:14px; right:14px; background:#e8eef8; color:var(--dbn-navy); border-radius:999px; padding:4px 9px; font-size:.74rem; font-weight:800; }
        .plan-name { margin:0; font-size:1.35rem; font-family:'Crimson Pro', serif; }
        .plan-price { margin:16px 0 4px; display:flex; align-items:baseline; gap:6px; }
        .plan-price strong { font-size:2rem; color:var(--dbn-navy); }
        .plan-meta { margin:0 0 16px; color:var(--dbn-muted); font-size:.92rem; }
        .plan-list { margin:0 0 22px; padding:0; list-style:none; flex:1; }
        .plan-list li { padding:8px 0; border-bottom:1px dashed #eef1f6; font-size:.94rem; }
        .fine-print { margin:10px 0 0; color:var(--dbn-muted); font-size:.84rem; }
        .section-head { display:flex; align-items:flex-end; justify-content:space-between; gap:16px; margin:34px 0 14px; }
        .section-head h2 { margin:0; font-family:'Crimson Pro', serif; font-size:1.9rem; }
        .section-head p { margin:0; color:var(--dbn-muted); max-width:640px; }
        .topup-grid { display:grid; grid-template-columns:repeat(3, minmax(0,1fr)); gap:16px; }
        .topup-card { display:grid; gap:10px; }
        .topup-price { color:var(--dbn-navy); font-size:1.8rem; font-weight:800; }
        .topup-credits { font-weight:700; }
        .topup-rate { color:var(--dbn-muted); font-size:.9rem; }
        .cost-panel { margin-top:16px; overflow:auto; }
        .cost-table { width:100%; border-collapse:collapse; min-width:620px; }
        .cost-table th, .cost-table td { text-align:left; padding:11px 10px; border-bottom:1px solid #edf1f7; }
        .cost-table th { color:var(--dbn-muted); font-size:.84rem; text-transform:uppercase; letter-spacing:.06em; }
        .billing-note { margin-top:18px; color:var(--dbn-muted); font-size:.92rem; }
        @media (max-width:980px) { .pricing-hero { grid-template-columns:1fr; } .plans-grid { grid-template-columns:repeat(2, minmax(0,1fr)); } }
        @media (max-width:680px) { .pricing-shell { padding-inline:14px; } .plans-grid, .topup-grid { grid-template-columns:1fr; } .survey-banner, .section-head { align-items:flex-start; flex-direction:column; } .plan-card { min-height:0; } }
    </style>
</head>
<body>
<main class="pricing-shell">
    <nav class="lang-bar" aria-label="Language">
        <?php foreach (['no', 'en'] as $lc): ?>
            <a href="?lang=<?= h($lc) ?>" class="<?= $lc === $uiLang ? 'is-active' : '' ?>"><?= h(dbnToolsLanguageLabel($lc)) ?></a>
        <?php endforeach; ?>
    </nav>

    <header class="pricing-hero">
        <div>
            <p class="eyebrow"><?= h($copy['eyebrow']) ?></p>
            <h1><?= h($copy['headline']) ?></h1>
            <p class="hero-copy"><?= h($copy['subhead']) ?></p>
        </div>
        <aside class="hero-note">
            <?= h($copy['trial']) ?>
        </aside>
    </header>

    <?php if ($status === 'success'): ?>
        <p class="status-pill success"><?= h($copy['status_success']) ?></p>
    <?php elseif ($status === 'canceled'): ?>
        <p class="status-pill"><?= h($copy['status_canceled']) ?></p>
    <?php endif; ?>

    <?php if ($isAuthed && !$surveyDone): ?>
        <section class="survey-banner">
            <div>
                <h2><?= h($copy['survey_title']) ?></h2>
                <p><?= h($copy['survey_text']) ?></p>
            </div>
            <a class="btn btn-light" href="<?= h($surveyUrl) ?>"><?= h($copy['survey_cta']) ?></a>
        </section>
    <?php endif; ?>

    <section class="plans-grid" aria-label="Plans">
        <?php foreach (['free', 'plus', 'pro'] as $sku): ?>
            <?php $plan = $plans[$sku]; ?>
            <article class="plan-card<?= $sku === 'pro' ? ' highlight' : '' ?>">
                <?php if ($sku === 'pro'): ?><span class="plan-badge">Pro</span><?php endif; ?>
                <h2 class="plan-name"><?= h($plan['name']) ?></h2>
                <p class="plan-price">
                    <strong><?= h(nok((int)$plan['price_nok'])) ?></strong>
                    <span><?= $sku === 'free' ? '' : ($isNorwegian ? '/ mnd' : '/ mo') ?></span>
                </p>
                <p class="plan-meta">
                    <?php if ($sku === 'free'): ?>
                        <?= $isNorwegian ? 'Startnivå' : 'Starter tier' ?>
                    <?php else: ?>
                        <?= h(sprintf('%.2f', (float)$plan['effective_credit_cost'])) ?> kr / <?= $isNorwegian ? 'kreditt' : 'credit' ?>
                    <?php endif; ?>
                </p>
                <ul class="plan-list">
                    <?php foreach ($planFeatures[$sku] as $feature): ?>
                        <li><?= h($feature) ?></li>
                    <?php endforeach; ?>
                    <li><?= (int)$plan['hourly_cap'] ?> <?= $isNorwegian ? 'betalte kjøringer per time' : 'paid runs per hour' ?></li>
                </ul>
                <?php if ($sku === 'free'): ?>
                    <?php if (!$isAuthed): ?>
                        <a class="pricing-cta btn-primary" href="<?= h($loginUrl) ?>"><?= h($copy['login']) ?></a>
                    <?php elseif ($currentTier === 'free'): ?>
                        <span class="pricing-cta btn-current"><?= h($copy['current']) ?></span>
                    <?php else: ?>
                        <span class="pricing-cta btn-muted"><?= h($copy['available']) ?></span>
                    <?php endif; ?>
                <?php else: ?>
                    <?php if (!$isAuthed): ?>
                        <a class="pricing-cta btn-primary" href="<?= h($loginUrl) ?>"><?= h($copy['login']) ?></a>
                    <?php elseif ($currentTier === $sku): ?>
                        <span class="pricing-cta btn-current"><?= h($copy['current']) ?></span>
                    <?php else: ?>
                        <button type="button" class="pricing-cta btn-primary" data-sku="<?= h($sku) ?>"><?= h($copy['choose'] . ' ' . $plan['name']) ?></button>
                    <?php endif; ?>
                <?php endif; ?>
            </article>
        <?php endforeach; ?>

        <article class="plan-card">
            <span class="plan-badge"><?= h($copy['organisation']) ?></span>
            <h2 class="plan-name"><?= h($copy['organisation']) ?></h2>
            <p class="plan-price"><strong><?= h($copy['organisation_price']) ?></strong></p>
            <p class="plan-meta"><?= $isNorwegian ? 'Tilpasset avtale' : 'Custom terms' ?></p>
            <ul class="plan-list">
                <li><?= $isNorwegian ? 'Flere brukere' : 'More users' ?></li>
                <li><?= $isNorwegian ? 'Tilpassede kreditter' : 'Custom credits' ?></li>
                <li><?= $isNorwegian ? 'Onboarding og støtte' : 'Onboarding and support' ?></li>
                <li><?= $isNorwegian ? 'Avtales direkte' : 'Agreed directly' ?></li>
            </ul>
            <a class="pricing-cta btn-muted" href="<?= h($orgUrl) ?>"><?= h($copy['contact']) ?></a>
            <p class="fine-print"><?= h($copy['organisation_text']) ?></p>
        </article>
    </section>

    <section>
        <div class="section-head">
            <div>
                <h2><?= h($copy['topups_title']) ?></h2>
                <p><?= h($copy['topups_lead']) ?></p>
            </div>
        </div>
        <div class="topup-grid">
            <?php foreach ($topups as $topup): ?>
                <article class="topup-card">
                    <h3 class="plan-name"><?= h($topup['name']) ?></h3>
                    <div class="topup-price"><?= h(nok((int)$topup['price_nok'])) ?></div>
                    <div class="topup-credits"><?= h(credits((int)$topup['credits'])) ?> <?= $isNorwegian ? 'kreditter' : 'credits' ?></div>
                    <div class="topup-rate"><?= h(sprintf('%.2f', (float)$topup['cost_per_credit'])) ?> kr / <?= $isNorwegian ? 'kreditt' : 'credit' ?></div>
                    <?php if ($isAuthed): ?>
                        <button type="button" class="pricing-cta btn-primary" data-sku="<?= h($topup['sku']) ?>"><?= h($copy['buy']) ?></button>
                    <?php else: ?>
                        <a class="pricing-cta btn-primary" href="<?= h($loginUrl) ?>"><?= h($copy['login_buy']) ?></a>
                    <?php endif; ?>
                </article>
            <?php endforeach; ?>
        </div>
    </section>

    <section>
        <div class="section-head">
            <div>
                <h2><?= h($copy['tool_costs']) ?></h2>
                <p><?= h($copy['tool_costs_lead']) ?></p>
            </div>
        </div>
        <div class="cost-panel">
            <table class="cost-table">
                <thead>
                    <tr>
                        <th><?= $isNorwegian ? 'Kostnad' : 'Cost' ?></th>
                        <th><?= $isNorwegian ? 'Verktøy' : 'Tools' ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($toolCostRows as $row): ?>
                    <tr>
                        <td><?= h($row[0]) ?></td>
                        <td><?= h($row[1]) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <p class="billing-note"><?= h($copy['billing_note']) ?></p>
    </section>
</main>

<script>
(function() {
    const connecting = <?= json_encode($copy['connecting'], JSON_UNESCAPED_UNICODE) ?>;
    const checkoutError = <?= json_encode($copy['checkout_error'], JSON_UNESCAPED_UNICODE) ?>;
    document.querySelectorAll('button[data-sku]').forEach((button) => {
        button.addEventListener('click', async () => {
            const sku = button.getAttribute('data-sku');
            const original = button.textContent;
            button.disabled = true;
            button.textContent = connecting;
            try {
                const response = await fetch('/api/stripe-checkout.php', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    credentials: 'same-origin',
                    body: JSON.stringify({ sku })
                });
                const data = await response.json();
                if (data.ok && data.url) {
                    window.location.href = data.url;
                    return;
                }
                alert(data.error?.message || checkoutError);
            } catch (error) {
                alert(error.message || checkoutError);
            } finally {
                button.disabled = false;
                button.textContent = original;
            }
        });
    });
})();
</script>
</body>
</html>
