<?php
declare(strict_types=1);

require_once __DIR__ . '/lib/tenant.php';
require_once __DIR__ . '/lib/media.php';
require_once __DIR__ . '/lib/products.php';

$tenant = resolve_tenant_from_host();
if (!$tenant) {
    http_response_code(404);
    echo 'Shop not found.';
    exit;
}
$tenantId = (int) $tenant['id'];
$fonts = font_pairing_css((string) $tenant['font_pairing']);
$slug = trim((string) ($_GET['slug'] ?? ''));
$tenantParam = tenant_param();

$stmt = db()->prepare('SELECT * FROM products WHERE tenant_id = ? AND slug = ? AND is_active = 1 LIMIT 1');
$stmt->execute([$tenantId, $slug]);
$p = $stmt->fetch();
if (!$p) {
    http_response_code(404);
    echo 'Product not found.';
    exit;
}

$ps = db()->prepare('SELECT business_name FROM page_settings WHERE tenant_id = ?');
$ps->execute([$tenantId]);
$business = shop_company_name($ps->fetch() ?: [], $tenant);
$img = media_public_url($p['image_path'] ?? null);
cart_boot();
$cartCount = array_sum(array_map('intval', $_SESSION['cart'] ?? []));

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $qty = max(1, (int) ($_POST['qty'] ?? 1));
    cart_add((int) $p['id'], $qty);
    header('Location: ' . APP_BASE . '/cart.php?tenant=' . urlencode($tenantParam));
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= htmlspecialchars($p['name']) ?> · <?= htmlspecialchars($business) ?></title>
  <link href="<?= htmlspecialchars($fonts['import']) ?>" rel="stylesheet">
  <link rel="stylesheet" href="<?= APP_BASE ?>/assets/public.css">
  <link rel="stylesheet" href="<?= APP_BASE ?>/assets/shop.css">
  <style>:root{--primary:<?= htmlspecialchars((string)$tenant['primary_color']) ?>;--secondary:<?= htmlspecialchars((string)$tenant['secondary_color']) ?>;--font-heading:<?= $fonts['heading'] ?>;--font-body:<?= $fonts['body'] ?>;}</style>
</head>
<body>
<header class="site-header">
  <a class="logo" href="<?= APP_BASE ?>/?tenant=<?= urlencode($tenantParam) ?>"><?= htmlspecialchars($business) ?></a>
  <nav>
    <a href="<?= APP_BASE ?>/shop.php?tenant=<?= urlencode($tenantParam) ?>">Shop</a>
    <a href="<?= APP_BASE ?>/cart.php?tenant=<?= urlencode($tenantParam) ?>" class="nav-cta">Cart (<?= (int) $cartCount ?>)</a>
  </nav>
</header>

<section class="section product-detail">
  <div class="product-detail-grid">
    <div class="product-detail-media">
      <?php if ($img): ?>
        <img src="<?= htmlspecialchars($img) ?>" alt="">
      <?php else: ?>
        <div class="product-ph large"><?= htmlspecialchars(strtoupper(substr($p['name'], 0, 1))) ?></div>
      <?php endif; ?>
    </div>
    <div>
      <span class="pill dark"><?= htmlspecialchars((string) $p['category']) ?></span>
      <h1><?= htmlspecialchars($p['name']) ?></h1>
      <p class="lede"><?= htmlspecialchars((string) $p['short_description']) ?></p>
      <p class="price">$<?= number_format((float) $p['price'], 2) ?></p>
      <p><?= nl2br(htmlspecialchars((string) $p['description'])) ?></p>
      <p class="muted"><?= (int) $p['stock'] > 0 ? (int) $p['stock'] . ' in stock' : 'Out of stock' ?></p>
      <?php if ((int) $p['stock'] > 0): ?>
        <form method="post" class="add-form">
          <label>Qty</label>
          <input type="number" name="qty" min="1" max="<?= (int) $p['stock'] ?>" value="1">
          <button class="btn" type="submit">Add to cart</button>
        </form>
      <?php endif; ?>
      <p style="margin-top:1rem"><a href="<?= APP_BASE ?>/shop.php?tenant=<?= urlencode($tenantParam) ?>">← Back to catalog</a></p>
    </div>
  </div>
</section>
<footer class="site-footer sn-footer-only">
  <?php require_once __DIR__ . '/lib/sn_credit.php'; sn_credit_render(); ?>
</footer>
</body>
</html>
