<?php

require_once __DIR__ . '/../app/config/config.php';
require_once __DIR__ . '/../app/config/database.php';
require_once __DIR__ . '/../app/helpers/functions.php';
require_once __DIR__ . '/../app/helpers/auth.php';
require_once __DIR__ . '/../app/helpers/cart.php';
require_once __DIR__ . '/../app/helpers/settings.php';

$siteName = get_setting('site_name', APP_NAME);
$shippingNote = get_setting('shipping_note', 'Shipping options and delivery messages are shown during checkout based on your region.');

$pageTitle = 'Shipping Policy | ' . $siteName;
$pageDescription = 'Read the shipping policy for ' . $siteName . '.';

$zones = $pdo->query("
    SELECT sz.*, sr.rate_name, sr.shipping_fee, sr.free_shipping_threshold
    FROM shipping_zones sz
    LEFT JOIN shipping_rates sr ON sz.id = sr.zone_id AND sr.status = 'active'
    WHERE sz.status = 'active'
    ORDER BY sz.country ASC, sr.shipping_fee ASC
")->fetchAll();

require_once __DIR__ . '/includes/header.php';
?>

<section class="shop-hero">
    <div class="container">
        <div class="eyebrow">Delivery</div>
        <h1>Shipping Policy</h1>
        <p><?= e($shippingNote); ?></p>
    </div>
</section>

<section class="section">
    <div class="container" style="max-width:900px;">
        <div class="checkout-form">
            <h2>Shipping Areas</h2>
            <p>
                Shipping options are calculated at checkout based on the selected shipping country and available
                admin-defined shipping zones.
            </p>

            <?php if ($zones): ?>
                <table class="admin-table">
                    <thead>
                        <tr>
                            <th>Country</th>
                            <th>Zone</th>
                            <th>Rate</th>
                            <th>Fee</th>
                            <th>Free Shipping</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($zones as $zone): ?>
                            <tr>
                                <td><?= e($zone['country']); ?></td>
                                <td><?= e($zone['zone_name']); ?></td>
                                <td><?= e($zone['rate_name'] ?? 'Rate confirmed at checkout'); ?></td>
                                <td>
                                    <?= isset($zone['shipping_fee']) ? e(format_price($zone['shipping_fee'])) : 'Confirmed at checkout'; ?>
                                </td>
                                <td>
                                    <?= !empty($zone['free_shipping_threshold'])
                                        ? 'Free over ' . e(format_price($zone['free_shipping_threshold']))
                                        : 'Not available'; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>

            <h2>Processing Time</h2>
            <p>
                Orders are processed after payment confirmation. Processing time may vary depending on stock availability,
                order volume, and delivery region.
            </p>

            <h2>Delivery Time</h2>
            <p>
                Estimated delivery times may vary by country, region, courier availability, and seasonal demand.
                Any delivery message shown during checkout is an estimate only.
            </p>

            <h2>Tracking</h2>
            <p>
                When tracking information is available, it will be added to your order by admin. You can use the
                order tracking page to check payment, order, shipping, and tracking status.
            </p>

            <h2>Incorrect Address</h2>
            <p>
                Customers are responsible for entering accurate delivery details. If an address is incorrect,
                delivery may be delayed or additional charges may apply.
            </p>
        </div>
    </div>
</section>

<?php require_once __DIR__ . '/includes/footer.php'; ?>