<?php
/**
 * Plugin Name: Cool B2C Product Cards
 * Description: Modern B2C WooCommerce product cards for cosmetics, skincare, and salt products.
 * Version: 1.0
 * Author: Custom Code
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * WooCommerce support and image size
 */
add_action('after_setup_theme', function () {
    add_theme_support('woocommerce');
    add_image_size('b2c_card_image', 700, 700, false);
});

/**
 * WooCommerce thumbnail setting
 */
add_filter('woocommerce_get_image_size_thumbnail', function ($size) {
    return array(
        'width'  => 700,
        'height' => 700,
        'crop'   => 0,
    );
});

/**
 * CSS design
 */
add_action('wp_enqueue_scripts', function () {
    wp_register_style('b2c-cool-style', false);
    wp_enqueue_style('b2c-cool-style');

    $css = '
    .b2c-page-wrap {
        font-family: Arial, sans-serif;
        background: linear-gradient(135deg, #fff7fb 0%, #fffdf7 45%, #f5fff9 100%);
        padding: 35px 18px;
        border-radius: 28px;
    }

    .b2c-hero {
        max-width: 1180px;
        margin: 0 auto 35px auto;
        display: grid;
        grid-template-columns: 1.2fr 0.8fr;
        gap: 28px;
        align-items: center;
        background: rgba(255,255,255,0.82);
        border: 1px solid rgba(255, 190, 220, 0.55);
        box-shadow: 0 22px 60px rgba(170, 80, 120, 0.13);
        border-radius: 34px;
        padding: 42px;
        overflow: hidden;
    }

    .b2c-hero h1 {
        font-size: clamp(32px, 5vw, 58px);
        line-height: 1.05;
        margin: 0 0 18px;
        color: #202124;
        letter-spacing: -1px;
    }

    .b2c-hero h1 span {
        color: #d64283;
    }

    .b2c-hero p {
        font-size: 18px;
        line-height: 1.7;
        color: #626262;
        margin: 0 0 24px;
        max-width: 650px;
    }

    .b2c-hero-btn {
        display: inline-block;
        background: linear-gradient(135deg, #d64283, #ff8ab8);
        color: white !important;
        text-decoration: none;
        padding: 14px 24px;
        border-radius: 999px;
        font-weight: 700;
        box-shadow: 0 12px 28px rgba(214, 66, 131, 0.28);
        transition: 0.25s ease;
    }

    .b2c-hero-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 18px 34px rgba(214, 66, 131, 0.34);
    }

    .b2c-hero-art {
        min-height: 300px;
        border-radius: 30px;
        background:
            radial-gradient(circle at 30% 25%, #ffd8e8 0 18%, transparent 19%),
            radial-gradient(circle at 70% 35%, #fff0b7 0 17%, transparent 18%),
            radial-gradient(circle at 50% 72%, #d9ffe8 0 20%, transparent 21%),
            linear-gradient(135deg, #fff, #fff2f8);
        position: relative;
        border: 1px solid rgba(255,255,255,0.9);
    }

    .b2c-hero-art:before {
        content: "Beauty • Skin Care • Salt";
        position: absolute;
        left: 24px;
        bottom: 24px;
        background: white;
        color: #d64283;
        font-weight: 800;
        border-radius: 999px;
        padding: 14px 18px;
        box-shadow: 0 12px 35px rgba(0,0,0,0.08);
    }

    .b2c-category-row {
        max-width: 1180px;
        margin: 0 auto 34px;
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 18px;
    }

    .b2c-cat-card {
        background: rgba(255,255,255,0.9);
        border-radius: 26px;
        padding: 24px;
        border: 1px solid rgba(255, 190, 220, 0.55);
        box-shadow: 0 14px 38px rgba(0,0,0,0.06);
        text-decoration: none;
        color: #222;
        transition: 0.25s ease;
    }

    .b2c-cat-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 22px 46px rgba(0,0,0,0.10);
    }

    .b2c-cat-icon {
        width: 56px;
        height: 56px;
        display: grid;
        place-items: center;
        border-radius: 18px;
        background: linear-gradient(135deg, #ffe0ec, #fff8d8);
        font-size: 27px;
        margin-bottom: 15px;
    }

    .b2c-cat-card h3 {
        margin: 0 0 8px;
        font-size: 22px;
        color: #222;
    }

    .b2c-cat-card p {
        margin: 0;
        color: #6d6d6d;
        line-height: 1.5;
    }

    .b2c-section-title {
        max-width: 1180px;
        margin: 0 auto 20px;
        display: flex;
        align-items: end;
        justify-content: space-between;
        gap: 18px;
    }

    .b2c-section-title h2 {
        margin: 0;
        font-size: 34px;
        color: #202124;
    }

    .b2c-section-title p {
        margin: 8px 0 0;
        color: #696969;
    }

    .b2c-products-grid {
        max-width: 1180px;
        margin: 0 auto;
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
        gap: 22px;
    }

    .b2c-product-card {
        position: relative;
        background: rgba(255,255,255,0.94);
        border-radius: 28px;
        overflow: hidden;
        border: 1px solid rgba(255, 190, 220, 0.45);
        box-shadow: 0 16px 44px rgba(0,0,0,0.07);
        transition: 0.28s ease;
    }

    .b2c-product-card:hover {
        transform: translateY(-6px);
        box-shadow: 0 28px 60px rgba(0,0,0,0.13);
    }

    .b2c-product-image-box {
        aspect-ratio: 1 / 1;
        background: linear-gradient(135deg, #fff3f8, #fffdf2);
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 18px;
    }

    .b2c-product-image-box img {
        width: 100%;
        height: 100%;
        object-fit: contain;
        display: block;
        transition: 0.3s ease;
    }

    .b2c-product-card:hover .b2c-product-image-box img {
        transform: scale(1.05);
    }

    .b2c-sale-badge {
        position: absolute;
        top: 14px;
        left: 14px;
        background: #d64283;
        color: white;
        font-size: 12px;
        font-weight: 800;
        padding: 8px 12px;
        border-radius: 999px;
        z-index: 2;
    }

    .b2c-product-info {
        padding: 18px 18px 20px;
    }

    .b2c-product-title {
        margin: 0 0 8px;
        font-size: 17px;
        line-height: 1.35;
        min-height: 46px;
    }

    .b2c-product-title a {
        color: #202124;
        text-decoration: none;
    }

    .b2c-product-title a:hover {
        color: #d64283;
    }

    .b2c-price {
        font-size: 18px;
        font-weight: 900;
        color: #d64283;
        margin-bottom: 14px;
    }

    .b2c-price del {
        color: #9b9b9b;
        font-weight: 500;
    }

    .b2c-card-actions {
        display: grid;
        grid-template-columns: 1fr 0.9fr;
        gap: 10px;
    }

    .b2c-card-actions a {
        text-align: center;
        text-decoration: none;
        border-radius: 999px;
        padding: 11px 12px;
        font-size: 13px;
        font-weight: 800;
        transition: 0.25s ease;
    }

    .b2c-add-cart {
        background: #202124;
        color: white !important;
    }

    .b2c-add-cart:hover {
        background: #d64283;
        color: white !important;
    }

    .b2c-view-detail {
        background: #fff0f6;
        color: #d64283 !important;
    }

    .b2c-view-detail:hover {
        background: #ffe1ee;
    }

    .b2c-empty {
        max-width: 1180px;
        margin: 0 auto;
        padding: 25px;
        background: white;
        border-radius: 20px;
        color: #666;
    }

    /* WooCommerce default shop page card style */
    .woocommerce ul.products li.product {
        background: rgba(255,255,255,0.94);
        border-radius: 26px;
        padding: 16px !important;
        border: 1px solid rgba(255, 190, 220, 0.45);
        box-shadow: 0 16px 44px rgba(0,0,0,0.07);
        transition: 0.25s ease;
        overflow: hidden;
    }

    .woocommerce ul.products li.product:hover {
        transform: translateY(-5px);
        box-shadow: 0 26px 55px rgba(0,0,0,0.12);
    }

    .woocommerce ul.products li.product img {
        aspect-ratio: 1 / 1;
        object-fit: contain;
        background: linear-gradient(135deg, #fff3f8, #fffdf2);
        border-radius: 22px;
        padding: 14px;
    }

    .woocommerce ul.products li.product .button {
        background: #202124;
        color: white;
        border-radius: 999px;
        padding: 12px 18px;
        font-weight: 800;
    }

    .woocommerce ul.products li.product .button:hover {
        background: #d64283;
        color: white;
    }

    @media (max-width: 800px) {
        .b2c-hero {
            grid-template-columns: 1fr;
            padding: 28px;
        }

        .b2c-category-row {
            grid-template-columns: 1fr;
        }

        .b2c-section-title {
            display: block;
        }

        .b2c-card-actions {
            grid-template-columns: 1fr;
        }
    }
    ';

    wp_add_inline_style('b2c-cool-style', $css);
});

/**
 * Product grid shortcode
 * Usage: [b2c_products limit="12"]
 * Usage with category: [b2c_products limit="8" category="skin-care"]
 */
add_shortcode('b2c_products', function ($atts) {
    if (!class_exists('WooCommerce')) {
        return '<div class="b2c-empty">Please install and activate WooCommerce first.</div>';
    }

    $atts = shortcode_atts(array(
        'limit'    => 12,
        'category' => '',
        'orderby' => 'date',
        'order'   => 'DESC',
    ), $atts, 'b2c_products');

    $allowed_orderby = array('date', 'title', 'menu_order', 'rand');
    $orderby = in_array($atts['orderby'], $allowed_orderby, true) ? $atts['orderby'] : 'date';

    $args = array(
        'post_type'      => 'product',
        'post_status'    => 'publish',
        'posts_per_page' => absint($atts['limit']),
        'orderby'        => $orderby,
        'order'          => sanitize_text_field($atts['order']),
    );

    if (!empty($atts['category'])) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => sanitize_title($atts['category']),
            ),
        );
    }

    $query = new WP_Query($args);

    if (!$query->have_posts()) {
        return '<div class="b2c-empty">No products found. Add products from WordPress Admin → Products → Add New.</div>';
    }

    ob_start();

    echo '<div class="b2c-products-grid">';

    while ($query->have_posts()) {
        $query->the_post();

        $product = wc_get_product(get_the_ID());

        if (!$product) {
            continue;
        }

        $product_link = get_permalink();
        $title        = get_the_title();
        $price_html   = $product->get_price_html();

        echo '<div class="b2c-product-card">';

        if ($product->is_on_sale()) {
            echo '<div class="b2c-sale-badge">SALE</div>';
        }

        echo '<a class="b2c-product-image-box" href="' . esc_url($product_link) . '">';
        echo $product->get_image('b2c_card_image', array('class' => 'b2c-product-img'));
        echo '</a>';

        echo '<div class="b2c-product-info">';
        echo '<h3 class="b2c-product-title"><a href="' . esc_url($product_link) . '">' . esc_html($title) . '</a></h3>';

        if (!empty($price_html)) {
            echo '<div class="b2c-price">' . wp_kses_post($price_html) . '</div>';
        }

        echo '<div class="b2c-card-actions">';

        if ($product->is_purchasable() && $product->is_in_stock()) {
            echo '<a href="' . esc_url($product->add_to_cart_url()) . '" 
                    data-quantity="1" 
                    data-product_id="' . esc_attr($product->get_id()) . '" 
                    class="b2c-add-cart add_to_cart_button ajax_add_to_cart">
                    Add to Cart
                  </a>';
        } else {
            echo '<a href="' . esc_url($product_link) . '" class="b2c-add-cart">View Product</a>';
        }

        echo '<a href="' . esc_url($product_link) . '" class="b2c-view-detail">Details</a>';

        echo '</div>';
        echo '</div>';
        echo '</div>';
    }

    echo '</div>';

    wp_reset_postdata();

    return ob_get_clean();
});

/**
 * Homepage shortcode
 * Usage: [b2c_homepage]
 */
add_shortcode('b2c_homepage', function () {
    ob_start();
    ?>

    <div class="b2c-page-wrap">

        <section class="b2c-hero">
            <div>
                <h1>Premium <span>Beauty, Skin Care</span> & Salt Products</h1>
                <p>
                    Discover quality cosmetics, daily skin care essentials, and natural salt products
                    in one clean and modern online store.
                </p>
                <a href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>" class="b2c-hero-btn">
                    Shop Now
                </a>
            </div>

            <div class="b2c-hero-art"></div>
        </section>

        <section class="b2c-category-row">
            <a href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>" class="b2c-cat-card">
                <div class="b2c-cat-icon">💄</div>
                <h3>Cosmetics</h3>
                <p>Modern beauty products for daily use.</p>
            </a>

            <a href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>" class="b2c-cat-card">
                <div class="b2c-cat-icon">🌸</div>
                <h3>Skin Care</h3>
                <p>Clean, fresh, and soft skin care range.</p>
            </a>

            <a href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>" class="b2c-cat-card">
                <div class="b2c-cat-icon">🧂</div>
                <h3>Salt Products</h3>
                <p>Natural salt products for home and wellness.</p>
            </a>
        </section>

        <section class="b2c-section-title">
            <div>
                <h2>Latest Products</h2>
                <p>Fresh products added from your WordPress admin panel.</p>
            </div>
        </section>

        <?php echo do_shortcode('[b2c_products limit="8"]'); ?>

    </div>

    <?php
    return ob_get_clean();
});