<?php
/**
 * Tom's Java Jive — Google Merchant Center Product Feed (RSS 2.0 / Atom)
 * URL: https://tomsjavajive.com/merchant-feed.php
 * Submit this URL in Merchant Center → Products → Feeds
 */
require_once __DIR__ . '/includes/functions.php';

header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex');

$base = 'https://tomsjavajive.com';

$products = db()->fetchAll(
    "SELECT p.*, pt.name AS type_name
     FROM products p
     LEFT JOIN product_types pt ON p.product_type_id = pt.type_id
     WHERE p.is_active = 1
     ORDER BY p.name, p.category"
);

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">' . "\n";
echo '<channel>' . "\n";
echo '  <title>Tom\'s Java Jive</title>' . "\n";
echo '  <link>' . $base . '</link>' . "\n";
echo '  <description>Premium artisan flavored coffee beans, freshly roasted and delivered to your door.</description>' . "\n";

foreach ($products as $p) {
    $images   = json_decode($p['images'] ?? '[]', true);
    $imageUrl = !empty($images) ? $base . $images[0] : $base . '/assets/images/placeholder-product.svg';

    $categoryLabel = $p['category'] === 'Bean' ? 'Whole Bean' : 'Ground';
    $grindNote     = $p['category'] === 'Bean'
        ? 'Available as whole bean so you can grind fresh for maximum flavor.'
        : 'Pre-ground to a versatile medium grind, ready to brew right out of the bag.';
    $title         = htmlspecialchars($p['name']);
    $rawDesc       = $p['description'] ?? '';
    if (!empty($rawDesc)) {
        $desc = htmlspecialchars(
            strip_tags($rawDesc) . ' ' . $grindNote . ' 12 oz bag. Ships from Weatherford, TX.'
        );
    } else {
        $desc = htmlspecialchars(
            "Tom's Java Jive {$p['name']} flavored artisan coffee, freshly roasted in small batches in Weatherford, Texas. " .
            "All-natural flavoring with no artificial additives. {$grindNote} " .
            "12 oz bag. Free shipping on orders over \$50."
        );
    }
    $link          = htmlspecialchars($base . '/product.php?id=' . $p['product_id']);
    $imageLink     = htmlspecialchars($imageUrl);
    $price         = number_format((float)($p['sale_price'] ?? $p['price']), 2, '.', '');
    $salePrice     = $p['sale_price'] ? number_format((float)$p['sale_price'], 2, '.', '') : null;
    $avail         = (int)$p['stock'] > 0 ? 'in stock' : 'out of stock';
    $id            = htmlspecialchars($p['product_id']);
    $updatedAt     = substr($p['updated_at'] ?? date('Y-m-d'), 0, 10);

    // Google product category for flavored coffee
    // https://www.google.com/basepages/producttype/taxonomy-with-ids.en-US.txt
    // 2273 = Food, Beverages & Tobacco > Beverages > Coffee
    $gCategory = 'Food, Beverages &amp; Tobacco &gt; Beverages &gt; Coffee';

    echo "  <item>\n";
    echo "    <g:id>{$id}</g:id>\n";
    echo "    <g:title>{$title}</g:title>\n";
    echo "    <g:description>{$desc}</g:description>\n";
    echo "    <g:link>{$link}</g:link>\n";
    echo "    <g:image_link>{$imageLink}</g:image_link>\n";
    echo "    <g:condition>new</g:condition>\n";
    echo "    <g:availability>{$avail}</g:availability>\n";
    echo "    <g:price>{$price} USD</g:price>\n";
    if ($salePrice) {
        echo "    <g:sale_price>{$salePrice} USD</g:sale_price>\n";
    }
    echo "    <g:brand>Tom's Java Jive</g:brand>\n";
    echo "    <g:google_product_category>{$gCategory}</g:google_product_category>\n";
    echo "    <g:product_type>Coffee &gt; Flavored Coffee &gt; {$categoryLabel}</g:product_type>\n";
    echo "    <g:identifier_exists>false</g:identifier_exists>\n";
    echo "    <g:custom_label_0>{$categoryLabel}</g:custom_label_0>\n";
    echo "    <g:shipping>\n";
    echo "      <g:country>US</g:country>\n";
    echo "      <g:service>Standard</g:service>\n";
    echo "      <g:price>5.99 USD</g:price>\n";
    echo "      <g:min_handling_time>1</g:min_handling_time>\n";
    echo "      <g:max_handling_time>2</g:max_handling_time>\n";
    echo "      <g:min_transit_time>3</g:min_transit_time>\n";
    echo "      <g:max_transit_time>7</g:max_transit_time>\n";
    echo "    </g:shipping>\n";
    echo "    <g:return_policy_label>default</g:return_policy_label>\n";
    echo "  </item>\n";
}

echo '</channel>' . "\n";
echo '</rss>';
