<?php
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/cart-functions.php';

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

// Rastrear visita
$track_pid = null;
if (basename($_SERVER['PHP_SELF']) === 'producto.php' && isset($_GET['slug'])) {
    // Intentar obtener el ID del producto por el slug para el tracking
    $stmt_t = db()->prepare('SELECT id FROM products WHERE slug = :slug LIMIT 1');
    $stmt_t->execute(['slug' => $_GET['slug']]);
    $track_pid = $stmt_t->fetchColumn() ?: null;
}
track_page_view($track_pid);

$siteTitle = $meta_title ?? 'BICHOTTA | Boutique femenina premium';
$siteDescription = $meta_description ?? 'Bichotta.cl ofrece lencería, ropa de dama y accesorios elegantes, sensuales y modernos con envío discreto.';
$siteUrl = site_url('');
$logo = site_url('assets/img/logo.png');
$is_customer_logged = !empty($_SESSION['customer_logged']) && $_SESSION['customer_logged'] === true;
$customer_name = $_SESSION['customer_name'] ?? '';
?>
<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?php echo sanitize($siteTitle); ?></title>
  <meta name="description" content="<?php echo sanitize($siteDescription); ?>">
  <!-- SEO & Social Meta -->
  <meta property="og:title" content="<?php echo sanitize($siteTitle); ?>">
  <meta property="og:description" content="<?php echo sanitize($siteDescription); ?>">
  <meta property="og:type" content="website">
  <meta property="og:url" content="<?php echo sanitize($siteUrl); ?>">
  <meta property="og:image" content="<?php echo sanitize($logo); ?>">
  <meta name="theme-color" content="#D81B60">
  <link rel="icon" type="image/png" href="assets/img/icon-192.png">
  <link rel="manifest" href="<?php echo asset_url('manifest.json'); ?>">
  <link rel="apple-touch-icon" href="assets/img/icon-192.png">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="default">
  <meta name="apple-mobile-web-app-title" content="BICHOTTA">
  <!-- Fonts -->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700;800&family=Playfair+Display:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="<?php echo asset_url('assets/css/style.css'); ?>">
  <script src="<?php echo asset_url('assets/js/app.js'); ?>" defer></script>
</head>
<body class="pwa-loaded">

<!-- BARRA SUPERIOR NEGRA -->
<div style="background: #000; color: #FFF; padding: 10px 0; text-align: center; font-size: 0.7rem; font-weight: 800; letter-spacing: 2px; text-transform: uppercase;">
  <div class="container">
    ✨ Envío Gratis en compras sobre $40.000 · Calidad Premium Garantizada ✨
  </div>
</div>

<header class="site-header">
  <div class="container header-grid">
    <div class="header-logo">
      <a href="index.php" class="brand">
        <img src="<?php echo site_url('assets/img/logofondoblanco.png'); ?>" alt="BICHOTTA" onerror="this.src='assets/img/logosinfondo.png'">
      </a>
    </div>

    <div class="header-search">
      <form action="tienda.php" method="get" style="position: relative; display: flex; align-items: center;">
        <input type="search" name="q" placeholder="Buscar en Bichotta..." aria-label="Buscar">
        <button type="submit" style="position: absolute; right: 15px; background: none; border: none; cursor: pointer; color: var(--text-muted); display: flex; align-items: center;">
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>
        </button>
      </form>
    </div>

    <div class="header-actions">
      <nav class="desktop-nav">
        <a href="index.php">Inicio</a>
        <a href="tienda.php">Tienda</a>
      </nav>
      
      <div class="header-icons" style="display: flex; align-items: center; gap: 18px;">
        <a href="mi-cuenta.php" title="Mi Cuenta" class="action-icon">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>
        </a>
        <a href="carrito.php" title="Carrito" class="action-icon">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M6 2L3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4z"></path><line x1="3" y1="6" x2="21" y2="6"></line><path d="M16 10a4 4 0 0 1-8 0"></path></svg>
          <span class="cart-badge"><?php echo cart_count(); ?></span>
        </a>
      </div>
    </div>
  </div>
</header>
<main>
